First a painful nit. Any experienced programmer relies on indentation to read code. Most will simply refuse to read any lengthy piece of unformatted code. itiskindoflikereadingsentenceswithnocapitalsorwhitespace,youcandoitbutitisnotfun.

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:

print_vars("var1", undef, (), "var2");
and the arguments are interpolated into the list:
"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.

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.)


In reply to Re: Re: Re: Re: Re: Passing Empty/ Undefined values to a subroutine by tilly
in thread Passing Empty/ Undefined values to a subroutine by bayruds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.