Click here to get back home

Few questions about arguments and subroutines/modules

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Few questions about arguments and subroutines/modules Telemach 06-07-2008
Posted by Telemach on June 7, 2008, 6:50 pm
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

first - single word
second - sentence
third - path

additional one that would display help

How to :

- declare a default value for first argument if not provided by user
- allow input of the full sentence ; right now script takes only the
first word from sentence
- 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
- display help after running for example example.pl -h

I was browsing thru different tutorials but haven't yet found answers
to above.

- Telemach -

Posted by Ron Bergin on June 7, 2008, 9:21 pm
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
>
> first - single word
> second - sentence
> third - path
>
> additional one that would display help
>
> How to :
>
> - declare a default value for first argument if not provided by user
> - allow input of the full sentence ; right now script takes only the
> first word from sentence
> - 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
> - display help after running for example example.pl -h
>
> I was browsing thru different tutorials but haven't yet found answers
> to above.
>
> - Telemach -

The documentation for these modules should be clear enough to be used
as tutorials.

http://search.cpan.org/~jv/Getopt-Long-2.37/lib/Getopt/Long.pm
http://search.cpan.org/~rgarcia/perl-5.10.0/lib/Getopt/Std.pm
http://search.cpan.org/~rsavage/Getopt-Simple-1.49/lib/Getopt/Simple.pm

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

Posted by Telemach on June 9, 2008, 2:43 am
Please log in for more thread options
> Catch a '-h' as first argument first before doing any other analysis of
> the commandline.
>
> jue

thanks, your post helped me

I did a trap for first argument so it will display help
sentence works in " "
last argument I did optional and a special if condition applied

- Telemach -

Similar ThreadsPosted
getting arguments July 28, 2007, 4:01 pm
MASON getting arguments. June 21, 2007, 4:05 pm
passing arguments to subroutines August 28, 2004, 11:08 pm
passing arguments from template November 14, 2004, 3:48 am
passing array arguments May 19, 2005, 9:36 am
Parsing function arguments June 23, 2005, 10:07 pm
Command-Line Arguments November 12, 2005, 6:09 pm
Arguments not passed in Windows XP June 6, 2006, 2:59 pm
calling a script with arguments July 25, 2007, 10:13 am
What's the best way to test existence of arguments January 21, 2008, 12:11 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap