carbonettis has asked for the wisdom of the Perl Monks concerning the following question:
Hey Monks,
I would like to write a subroutine to look for command-line arguments such as -help, -h, and --help. I wanna call my subrutine from a main program and the thing is: if I give the program any of the named command-line arguments, when you
pass them into the subroutine it should return a true value. If this is the case, the program should print out a help message in a $USAGE variable and exit.
My try is:
But it looks like not working and I cannot find the reason. Any ideas? when I run it it returns "syntax error at ex4HW01.pl line 13, near "0;" THANKS!#!/usr/bin/perl -w $check = mycheck($#ARGV); if ($check != 1) { $usage = "Help arguments have been found"; print $usage, "\n"; } sub mycheck { my($#ARGV) = @_; $return = 0; #Variable RETURN set as false (set as 0) foreach $arg (0 .. $#ARGV) { if (($arg eq '-help') || ($arg eq '-h') || ($arg eq '--help')) + { $return = 1; #Variable return now is true exit; } else { print "No help arguments found\n"; } } return $return }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: ARGV issue
by ikegami (Patriarch) on Oct 02, 2011 at 21:57 UTC | |
Re: ARGV issue
by TomDLux (Vicar) on Oct 03, 2011 at 02:39 UTC | |
Re: ARGV issue
by jwkrahn (Abbot) on Oct 02, 2011 at 22:24 UTC | |
Re: ARGV issue
by dwm042 (Priest) on Oct 03, 2011 at 17:56 UTC |
Back to
Seekers of Perl Wisdom