LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-18-2013, 09:58 PM   #1
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Rep: Reputation: Disabled
New to Perl


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.
 
Old 02-19-2013, 12:10 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
1 members found this post helpful.
Old 02-19-2013, 04:37 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
better is:

Code:
#!/usr/bin/perl

sub onStatus {
    "ONLINE\n";
}

print onStatus;
or

Code:
#!/usr/bin/perl

sub onStatus {
    print "ONLINE\n";
}

onStatus;
 
2 members found this post helpful.
Old 02-19-2013, 03:03 PM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by soupmagnet View Post
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.
 
1 members found this post helpful.
Old 02-19-2013, 07:42 PM   #5
soupmagnet
LQ Newbie
 
Registered: Sep 2012
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
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.
 
Old 02-19-2013, 07:51 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by soupmagnet View Post
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";
 
1 members found this post helpful.
Old 02-20-2013, 01:58 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by soupmagnet View Post
I'm making the move from Bash to Perl
Incidentally, they are completely different tools.
Shell scripting is good for certain things, Perl is good for others.

I wouldn't prefer one to the exclusion of t'other.
 
1 members found this post helpful.
Old 02-20-2013, 03:18 AM   #8
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by soupmagnet View Post
So is that always the case with subroutines? Can that effect the overall outcome?
Yes.


Quote:
Originally Posted by soupmagnet View Post
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();
 
1 members found this post helpful.
Old 02-21-2013, 12:09 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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";
}
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
http://www.tizag.com/perlT/index.php
 
Old 02-21-2013, 01:25 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by chrism01 View Post
Also, Perl is compiled on the fly,
That's not strictly true chrism, it's compiled before it runs.

c.f. Tcl is compiled on the fly, which means you can run a script for a year and then suddenly
come across a syntax error.
 
Old 02-21-2013, 04:18 AM   #11
Suic
LQ Newbie
 
Registered: Feb 2013
Posts: 3

Rep: Reputation: Disabled
Code:
#!/usr/bin/perl

sub onStatus {
print "ONLINE\n";
return;
}

print onStatus();
 
Old 02-21-2013, 08:24 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Suic View Post
Code:
#!/usr/bin/perl

sub onStatus {
print "ONLINE\n";
return;
}

print onStatus();
And the recommendation to use

Code:
use strict;

is still correct - without it you are metaphorically throwing yourself on a mine filed.
 
Old 02-21-2013, 12:59 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
not to speak about
#!/usr/bin/perl -w
see http://perldoc.perl.org/warnings.html
 
Old 02-26-2013, 07:59 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
@Billy; yeah, that's what I meant. Didn't know about TCL, so didn't realise it was ambiguous.
 
Old 03-01-2013, 04:58 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
@chrism

Ok I will forgive you this one time
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
install vim via yum thinks perl is required - I build perl 5.14.2 from source rubanek Linux - General 4 05-02-2012 06:42 PM
Print output of a script to screen using Perl/Multiple installation of Perl Modules metallica1973 Linux - General 1 02-17-2011 05:59 PM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration