|
Posted by Jürgen Exner on June 8, 2008, 2:59 am
Please log in for more thread options >
>I'm newbie who is looking for some online tutorial about advanced use
>of arguments for subroutines and modules.
>
>Let's say I have a script that would take 3 arguments
I am confused. Are you talking about arguments that are passed to
functions/subroutines as you said 3 lines above or arguments passed to
the script from the command line as you said in previous line?
>first - single word
>second - sentence
>third - path
>additional one that would display help
Your design has several problems which are not specific to Perl but are
generic for any comannd line parameters.
>How to :
>- declare a default value for first argument if not provided by user
In general you would do something like
if (defined $ARGV[0]) {
$myvalue = shift @ARGV;
} else {
$myvalue = 'whateverdefaultvalueyoulike'
}
(there are shorter idioms using the same idea).
However because of the way you designed your parameters there is no easy
way to recognize if the first argument is missing or not. Even counting
the arguments doesn't help because you still wouldn't know if the first
or the last argument were omitted.
My suggestion would be to rethink the way you pass arguments and use a
style like e.g.
myprog.pl -w=word -s="Whatever Sentence you want" -f=filename
>- allow input of the full sentence ; right now script takes only the
>first word from sentence
This has probably nothing to do with Perl but everything with your
shell. All shells I know of will pass individual words as separate
arguments unless you enclose them in quotes. Details vary somewhat from
shell to shell.
>- create a condition that would for example : download a certain file
>if third argument (which is a path) is provided, no argument shall not
>trigger download
Same as above for an omitted first argument.
>- display help after running for example example.pl -h
Catch a '-h' as first argument first before doing any other analysis of
the commandline.
jue
|