LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-26-2013, 05:04 PM   #1
munkz
Member
 
Registered: Aug 2013
Location: A couch
Distribution: linux
Posts: 69

Rep: Reputation: 2
PERL scripting order.


Hi,

Can not seem to find the information I am looking for. I am trying to understand the way a perl script is read. For example, in bash it would be top down right. Where a function can not be called if it is defined AFTER the call. Like

Code:
function something1(){
echo $something2
}

function something2(){
 hello
}
I think I got that right. Now, with PERL is it the same? If for example I have
Code:
my $pchains = '';
my $dlist = '';
my $mhits = '';
my $hitrate = '';
my $log = '';
GetOptions ('pchains' => \$pchains, 'dlist' => \$dlist, 'mhits' => \$mhits, 'hitrate' => \$hitrate 'log' => \$log);
Would my sub go before or after? I need to do a few things to each of arguments passed. So, I dont know where to put the sub function or how to pass argument to the sub. If I wanted a sub that just cleane up the input passed to it like
Code:
sub clean{
      chomp ($url = $something_passed);
}
Thanks for any help.

Last edited by munkz; 09-26-2013 at 05:06 PM. Reason: typo
 
Old 09-26-2013, 08:06 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
See http://perldoc.perl.org/perlsub.html
 
Old 09-26-2013, 08:33 PM   #3
munkz
Member
 
Registered: Aug 2013
Location: A couch
Distribution: linux
Posts: 69

Original Poster
Rep: Reputation: 2
I have. But thanks for the man page. I have looked at that. Reading it say a sub can go anywhere in the program

Quote:
Like many languages, Perl provides for user-defined subroutines. These may be located anywhere in the main program, loaded in from other files via the do, require, or use keywords, or generated on the fly using eval or anonymous subroutines. You can even call a function indirectly using a variable containing its name or a CODE reference.
So, I tried putting a variable after the portion that would call it and it did not work. I am just doing some tutorials. I thank you for your taking a look. However, I did read it. I asked cause I needed some help figuring it out. Like I said, I have look at the man pages. I needed something cleared up.


Thanks.
 
Old 09-26-2013, 08:37 PM   #4
munkz
Member
 
Registered: Aug 2013
Location: A couch
Distribution: linux
Posts: 69

Original Poster
Rep: Reputation: 2
An example of what is confusing me :

Code:
use strict;
use warnings;

my $test = (print "hello ");
my @array = (
    $test,
    "these",
    "strings",
    "out",
    "for",
    "me" # trailing comma is okay
);

print "array = @array\n";

Why does the above print "hello" in fron of the array contents rather than in order? Like :

Code:
perl Documents/SCRIPTS/perltest.pl
hello array = 1 these strings out for me
ello should go where it is printing 1. I am doing this because I would like to read in a file passed to the script into an array and then, if I understand it, shift out after the variable has been run through the loop.
 
Old 09-26-2013, 08:51 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Treat it like C .. declare all the subs at the top then put them wherever you like

Code:
#!/usr/bin/perl

# variables
my $msg = "Hi";

# declarations
sub foo;
sub bar;

# main
bar();

# subroutines
sub bar {
  foo();
}

sub foo {
  print $msg . "\n";
}
 
1 members found this post helpful.
Old 09-26-2013, 08:55 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Sorry, in response to your last question, the command in brackets gets executed not saved into $test, $test gets the return code.
 
1 members found this post helpful.
Old 09-28-2013, 09:40 AM   #7
Hangdog42
LQ Veteran
 
Registered: Feb 2003
Location: Maryland
Distribution: Slackware
Posts: 7,803
Blog Entries: 1

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
I know perl lets you do all sorts of shortcuts, but until you REALLY get your head wrapped around it, it is probably best to stick to clear statements. For example:

Quote:
my $test = (print "hello ");
I have absolutely no idea why you would include a print command inside a variable declaration. To my way of thinking, that is asking for confusion. Instead, if you just define your variables without added cruft, it will likely behave better:

Code:
my $test = "hello ";
my @array = (
    $test,
    "these",
    "strings",
    "out",
    "for",
    "me" # trailing comma is okay
);

print "array = @array\n";
Also, subroutines behave differently than variables in terms of where they are defined. Perl doesn't really care if the subroutine is defined before or after it is used in the main program, unlike variable that have to be declared before, or at, the time of use. kbp's suggestion doesn't hurt, especially if it helps you understand your code later on, but it isn't required by Perl.
 
1 members found this post helpful.
Old 09-28-2013, 07:40 PM   #8
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Took me a while but I worked out why I had thought declaring was needed, in my early days of learning perl I had called functions like:

Code:
foo;
.. and if the definition had not been seen before then perl would complain like so:

Code:
Bareword "foo" not allowed while "strict subs" in use at test.pl line 20.
This led to me defining subs in order of dependency, then declaring ... eventually I managed to call them properly and the issue never came up again:

Code:
foo();
A sad story of hacking your way through .. lesson to learn: RTFM!!!
 
1 members found this post helpful.
  


Reply

Tags
array, functions, perl, perlscript, scripting



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
about perl scripting` Agnihotram Srikanth Linux - Newbie 10 05-31-2013 09:36 AM
Perl to order a list Trotel Programming 2 06-13-2012 06:35 PM
[SOLVED] Perl printing to file in the wrong order druisgod Programming 4 05-02-2011 12:03 PM
LXer: Higher-Order Perl LXer Syndicated Linux News 0 09-10-2009 01:50 AM
Perl scripting Rameriez Programming 4 02-03-2003 01:01 AM

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

All times are GMT -5. The time now is 01:38 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