in reply to Re: Re: Re: Re: Passing Empty/ Undefined values to a subroutine
in thread Passing Empty/ Undefined values to a subroutine
To get a sense what your code looks like when formatted, download perltidy and run it. For discussion of why indentation matters (and virtually everything else about basic coding technique) the reference that I highly recommend is Code Complete. Don't be fooled by its age or the fact that Perl is not among the languages used as examples, its advice is timeless and very relevant.
Back to Perl. Here is your confusion. @undef1 is an array which can have 0, 1 or many elements. You initialize it to none. Perl passes arguments into a function as a single list. In particular that array will be flattened out and take up 0, 1, or many arguments. In your case the array has nothing in it and so contributes no elements to the list. However in print_vars you are assuming that it has one element. Essentially your function call boils down to:
and the arguments are interpolated into the list:print_vars("var1", undef, (), "var2");
and then you place "var1" into $var1, undef into $var3, "var2" into the array @undef1, and nothing is left to go into $var2."var1", undef, "var2"
This behaviour is described in perldata, look for the words, LISTs do automatic interpolation of sublists. Yeah, I know. It says it, but making heads or tails of it takes concentration and work. That is why people buy books like Learning Perl that say important parts of the documentation, but in an easier to read form. (Of course at some point it is important to become used to reading documentation, but take it one step at a time.)
|
|---|