473,513 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Sep 28 '06 #1
2 5434
* Tong * <su******@users.sourceforge.netwrites:
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--
Sep 28 '06 #2
* 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
Sep 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2669
by: Johannes Unfried | last post by:
Problem Best practice needed to marshal STL data from managed code to unmanaged code & vice vers Details managed code is written in managed C++ & accesses the unmanaged code (i.e. lives in a...
12
1731
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long...
4
4003
by: MicroMoth | last post by:
Hi, I'm trying to write a update method, in which when the user clicks the update button the update method is passed 10 form fields. Then a update SQL is run to update the database. My question...
4
1675
by: tshad | last post by:
I need to pass a few parameters to my Windows Service program. The end user will be changing the parameters and settings should be saved. What is the best practice - use app.config - use .ini...
16
2774
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
17
3008
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
7
3568
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
52
3326
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of...
2
2341
by: stephen.diverdi | last post by:
I have a question about what's the best way to handle something in C++ without a lot of redundant code and void*s because I don't seem to be able to avoid them both. I've run into it before and...
0
7177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.