CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Is it ok if a subroutine tries to read more arguments than were passed?

 



DrZed
User

Jun 6, 2000, 7:37 AM

Post #1 of 3 (384 views)
Is it ok if a subroutine tries to read more arguments than were passed? Can't Post

I have a subroutine that is called often, and usually uses the same arguments, but on occasion uses a differet arguement. So what I'm doing is checking to see if the arguments have a value and, if not, setting them to default.

For example:

sub routine
{
my ($a,$b,$c,$d) = @_;
if (!$a) { $a = $default{'a'}; }
if (!$b) { $a = $default{'b'}; }
if (!$c) { $a = $default{'c'}; }
if (!$d) { $a = $default{'d'}; }
# etc....
}

#Then, I call the routine with different number of arguements. Here's some examples:

&routine('One'); # set $a; default others
&routine('Two','DATE'); # set $a,$b; default others
&routine('Three','',-5); # set $a,$c; default others

So, am I taking advantage of Perl's flexability, or is this 'bad programming' which might cause unforseen problems?

It seems to work fine, so I'm hoping it's ok.

Dr. Zed

P.S. Zeros and null strings (i.e. things that would pass during an "if (!$a)...") would never be valid arguments for the subroutine, so I won't have any problems with desired values being set to default.


[This message has been edited by DrZed (edited 06-06-2000).]


japhy
Enthusiast

Jun 6, 2000, 8:49 AM

Post #2 of 3 (384 views)
Re: Is it ok if a subroutine tries to read more arguments than were passed? [In reply to] Can't Post

That's perfectly fine. It's not bad at all. However, setting default values is usually better done using the | |= operator:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$value &#0124; &#0124;= $default;
# that means
$value = $value &#0124; &#0124; $default;
</pre><HR></BLOCKQUOTE>

Anyway, here are some optional ways of doing what you want:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub foo {
my ($a,$b,$c,$d) = @_;
$a &#0124; &#0124;= $default{a};
$c &#0124; &#0124;= $default{b};
$b &#0124; &#0124;= $default{c};
$d &#0124; &#0124;= $default{d};
}

sub foo {
my $a = shift &#0124; &#0124; $default{a};
my $b = shift &#0124; &#0124; $default{b};
my $c = shift &#0124; &#0124; $default{c};
my $d = shift &#0124; &#0124; $default{d};
}
</pre><HR></BLOCKQUOTE>

Hope this helps.


DrZed
User

Jun 6, 2000, 9:47 AM

Post #3 of 3 (384 views)
Re: Is it ok if a subroutine tries to read more arguments than were passed? [In reply to] Can't Post

&#0124; &#0124;=.... nice. I love things like that. Thanks.

If I understand, shift is pulling the arguments off an argument stack. From the looks of it, the shift version would probably be optimum.

Dr. Zed


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives