
DrZed
User
Jun 6, 2000, 7:37 AM
Post #1 of 3
(99 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).]
|