ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm making the move from Bash to Perl and I'm trying to port a Bash script to get a better understanding of it as I go. I'm looking at subroutines right now and I want to get the output of subroutine and I know I can do something like this:
Code:
#!/usr/bin/perl
sub onStatus {
print "ONLINE\n";
}
print onStatus();
But the output is always:
ONLINE
1
What does that mean? I haven't read about anything like that anywhere and I'm wondering if it's something similar to when you have to chomp a variable to get rid of the newline or if it's something completely different.
just think about the code you wrote: there are two print statements. One of them prints the word ONLINE, and the second? You have a function call after print, so perl will print the result of the function call.
I'm making the move from Bash to Perl and I'm trying to port a Bash script to get a better understanding of it as I go. I'm looking at subroutines right now and I want to get the output of subroutine and I know I can do something like this:
Code:
#!/usr/bin/perl
sub onStatus {
print "ONLINE\n";
}
print onStatus();
But the output is always:
ONLINE
1
What does that mean? I haven't read about anything like that anywhere and I'm wondering if it's something similar to when you have to chomp a variable to get rid of the newline or if it's something completely different.
What's happening in perl subroutines is that the last statement in it also defines the sub routine's return value. So the line
Code:
print "ONLINE\n";
does two things:
1) Print the word ONLINE. (Note that if you intended to print the value of a variable named ONLINE you should write $ONLINE as in bash scripts.
2) Assign the sub's return value to 1.
Then, the line
Code:
print onStatus();
again does two things:
1) Call the sub that actually prints ONLINE (see item 1 above)
2) Print the sub routine's return value, which is 1.
What's happening in perl subroutines is that the last statement in it also defines the sub routine's return value. So the line
Code:
print "ONLINE\n";
does two things:
1) Print the word ONLINE. (Note that if you intended to print the value of a variable named ONLINE you should write $ONLINE as in bash scripts.
2) Assign the sub's return value to 1.
Then, the line
Code:
print onStatus();
again does two things:
1) Call the sub that actually prints ONLINE (see item 1 above)
2) Print the sub routine's return value, which is 1.
So is that always the case with subroutines? Can that effect the overall outcome? Obviously my example is quite minimal and based on very little knowledge Perl, but in the end what I would like to do would probably best be described with psudocode of a bash script I'm trying to emulate.
Code:
#!/bin/bash
onStatus(){
(command)
if [ $? -eq 0 ]; then
echo "ONLINE"
else
echo "OFFLINE"
fi
}
echo "Status: $(onStatus)"
Which I would then use later on in the script with something like this...
Code:
if [ "$(onStatus)" == "ONLINE" ]; then
(commands)
fi
Will the output of the Perl subroutine that includes the exit status effect how my code will eventually work?
Last edited by soupmagnet; 02-19-2013 at 07:47 PM.
So is that always the case with subroutines? Can that effect the overall outcome? Obviously my example is quite minimal and based on very little knowledge Perl, but in the end what I would like to do would probably best be described with psudocode of a bash script I'm trying to emulate.
Code:
#!/bin/bash
onStatus(){
(command)
if [ $? -eq 0 ]; then
echo "ONLINE"
else
echo "OFFLINE"
fi
}
echo "Status: $(onStatus)"
I think you are confused between the desired string output value and outputting a string value to an I/O channel.
E.g.
Code:
sub status_to_text
{
$? ? 'status is non-zero' : 'status is 0'
}
my $string_status = status_to_text;
print "\$string_status=$string_status\n";
print 'string status by calling the sub directly: ', status_to_text, "\n";
So is that always the case with subroutines? Can that effect the overall outcome?
Yes.
Quote:
Originally Posted by soupmagnet
Code:
#!/bin/bash
onStatus(){
(command)
if [ $? -eq 0 ]; then
echo "ONLINE"
else
echo "OFFLINE"
fi
}
echo "Status: $(onStatus)"
Which I would then use later on in the script with something like this...
Code:
if [ "$(onStatus)" == "ONLINE" ]; then
(commands)
fi
If you want to stay as close as possible to this snippet just assign a variable to the string ONLINE or OFFLINE rather then using the print command. The string should then become the return value.
Code:
#!/usr/bin/perl
sub onStatus {
$tmp = "ONLINE\n";
}
print onStatus();
Can i just recommend using warnings and strict; they'll save you a lot of grief later.
Also, Perl is compiled on the fly, so you can code like eg C ie you can call a fn BEFORE you've defined it (lexically)
Code:
#!/usr/bin/perl -w
use strict;
print onStatus();
sub onStatus
{
my $tmp = "ONLINE\n";
}
#### Alternate version
#!/usr/bin/perl -w
use strict;
my $tmp=onStatus();
print "$tmp\n";
sub onStatus
{
return "ONLINE";
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.