Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 01:15 PM
* Tong *
Guest
 
Posts: n/a
Default Best practice to pass parameters to functions


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

  #2  
Old September 28th, 2006, 02:55 PM
Michal Nazarewicz
Guest
 
Posts: n/a
Default Re: Best practice to pass parameters to functions

* Tong * <sun_tong@users.sourceforge.netwrites:
Quote:
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--
  #3  
Old September 28th, 2006, 08:25 PM
Joe Smith
Guest
 
Posts: n/a
Default Re: Best practice to pass parameters to functions

* Tong * wrote:
Quote:
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles