Hi Monks,
I'm writing a subroutine which takes a variable number of arguments. Sometimes the 2nd argument is absent, and for simplicity/conciseness I was hoping to be able to just omit it from the sub call, but it looks as if I might need to explicitly specify '' for that argument. Is that correct? Check this code out:
The output is:test1('A1',,'A3','A4'); test1('B1','','B3','B4'); sub test1 { print "args=" . join(',',@_) . "\n"; my ($arg1, $arg2) = (shift @_, shift @_); my @argrest = @_; print "arg1=$arg1, arg2=$arg2, argrest=@argrest\n\n"; }
args=A1,A3,A4 arg1=A1, arg2=A3, argrest=A4 args=B1,,B3,B4 arg1=B1, arg2=, argrest=B3 B4
As you can see, it looks as if the 2nd argument is completely ignored, unless I explicitly supply it.
Questions:
1. Why does the 3rd argument seem to be seen as the 2nd when the 2nd is omitted? (Please don't say "because the 2nd is omitted").
2. Will I really need to supply the 2nd argument as '' or is there a simple way to get omitted arguments to be recognised?
Thanks.
In reply to Omitted subroutine arguments by tel2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |