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.
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
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.
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.
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";
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.