Best practice to pass parameters to functions

Discussion in 'Perl' started by * Tong *, Sep 28, 2006.

  1. * Tong *

    * Tong * Guest

    Hi

    Suppose there is a Perl equivalent function named 'ls' or 'sort', now what
    is a good way to pass those switches to it? I.e., I need a systematical way
    for Perl functions to handle dozens of switches like the 'ls' or 'sort'
    command does.

    please comment.

    thanks

    --
    Tong (remove underscore(s) to reply)
    http://xpt.sourceforge.net/


    --
    Posted via a free Usenet account from http://www.teranews.com
    * Tong *, Sep 28, 2006
    #1
    1. Advertising

  2. * Tong * <> writes:
    > Suppose there is a Perl equivalent function named 'ls' or 'sort', now what
    > is a good way to pass those switches to it? I.e., I need a systematical way
    > for Perl functions to handle dozens of switches like the 'ls' or 'sort'
    > command does.


    #v+
    sub foo () {
    my %opts = @_;
    if ($opts{'long'}) {
    # use long output format
    }
    my $path = $opts{'path'} || '.';
    # ...
    }

    # ...
    foo( 'long' => 1, 'path' => '/usr', ... some more options );
    #v-


    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
    Michal Nazarewicz, Sep 28, 2006
    #2
    1. Advertising

  3. * Tong *

    Joe Smith Guest

    * Tong * wrote:
    > for Perl functions to handle dozens of switches like the 'ls' or 'sort'
    > command does.


    It depends on whether you're talking about perl functions or
    perl commands.

    For a perl function, arrange it so that one of the arguments
    to the function is a reference to a hash, and put all the options
    and values in that hash.

    For a perl command, which needs to parse options from the
    command line, "use Getopt::Std;" is one module that will work.

    Example:

    use Getopt::Std;
    #options: -s = number of seconds (or minutes) to between checks (default 3s)
    my %opts; getopts('c:dnrs:t:z',\%opts);
    my $debug = $opts{d} || 0;
    my $sleep = $opts{s} || DefaultSleep; $sleep = m2s($sleep);
    my $timecount = $opts{c} || DefaultCount;
    $timecount = m2s($opts{t}) / $sleep if $opts{t};
    my $showzero = $opts{z} || (@ARGV == 1 and -f $ARGV[0]);
    my $check_r = $opts{r} || 0; # rsync/restore
    test_num(-3..19) if $opts{n}; # -n for debugging num()


    -Joe
    Joe Smith, Sep 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Colonel
    Replies:
    6
    Views:
    4,397
    Hans Kesting
    Jun 7, 2006
  2. tshad

    Passing parameters best practice

    tshad, Aug 9, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,846
    sloan
    Aug 9, 2006
  3. Replies:
    1
    Views:
    341
    Martin Eisenberg
    Nov 11, 2008
  4. Matt Hawley
    Replies:
    2
    Views:
    183
    Fred Hirschfeld
    Apr 25, 2004
  5. oldyork90
    Replies:
    1
    Views:
    154
    Jeremy J Starcher
    Sep 10, 2008
Loading...

Share This Page