CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [FishMonger] Using Arguments with Subroutines: Edit Log



griever92
Novice

Oct 31, 2012, 7:30 AM


Views: 2349
Re: [FishMonger] Using Arguments with Subroutines

No instructor involved, this is my own project that i'm working on, so there's no real specs, I just need something that works.

Essentially what i'm doing in the main script is handling of the switches, while subroutines handle the bulk of the other work.

The switches that I call with the script, determine which subroutines are (or are not) run.

I have 2 subroutines near the opening that essentially serve the same purpose, but produce a different end result. This is where i'm running into trouble, as these subroutines are set to handle the argument which is meant to be passed from the command line.

=====

Below, is an excerpt from the primary script block, however I have modified the variable/sub names.
$switch1 and $switch2 are both defined earlier in the code block, and are activated by means of a switch via CLI.
In this example, let's assume $switch2 is activated by 'l'.

main.pl ||| perl main.pl -l this.is.my.target

Code
if ($switch1) { 
&sub1
}
elsif ($switch2) {
&sub2( shift );
}


This would drive the script to perform &sub2

sub2.pl

Code
sub sub2 
{
my $numArgs = $#ARGV;
my $show_dir = '';
if ($numArgs == -1) {
print "Need Target Parameter\n";
exit 0;
}
foreach $numArgs ($#ARGV) {
$show_dir = $ARGV[$numArgs];
}

there's a bunch of other stuff below this excerpt, but it appears to be working.

at this point, $show_dir should equal 'this.is.my.target' and should be useable from the main script block.


(This post was edited by griever92 on Oct 31, 2012, 7:37 AM)


Edit Log:
Post edited by griever92 (Novice) on Oct 31, 2012, 7:32 AM
Post edited by griever92 (Novice) on Oct 31, 2012, 7:32 AM
Post edited by griever92 (Novice) on Oct 31, 2012, 7:33 AM
Post edited by griever92 (Novice) on Oct 31, 2012, 7:37 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives