kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

A quick question on the following syntax:

my ($name, $dept, undef, $status) = get_info();
Is it legal to have an 'undef' together with the other named variables in that list?

Update: On the advice of TomDLux, I would like to add that I've used the above in my code before asking and it compiles nicely. However, I was curious to find out whether it's legal to have an 'undef' there despite the fact that it compiles. I mean it could be that it compiles because the interpreter is forgiving of the mistake, or whatever. My apologies for not making this explicit earlier.

Replies are listed 'Best First'.
Re: Declaring variables - is it legal to do this?
by japhy (Canon) on May 15, 2004 at 04:56 UTC
    You should try it first. (Yes, it's legal.) But you can also do:
    my ($name, $dept, $status) = (get_info())[0,1,3];
    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
      Thanks, japhy!

      Yes, I have used it in my code and there doesn't seem to be any problems but I wanted to be sure that I'm not inventing the wrong syntax :)

Re: Declaring variables - is it legal to do this?
by Belgarion (Chaplain) on May 15, 2004 at 04:57 UTC

    Yes, this is legal. Not very common, but legal. You could have found this out yourself by simply trying a small test. Example:

    my ($name, $dept, undef, $status) = qw(one two three four); print "$name, $dept, $status\n"; __OUTPUT__ one, two, four

    As you can see, the third element is lost.

      Thanks, Belgarion! Please read my reply to japhy :)

      Not very common

      Why "Not very common"? I think it is common.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

        I believe most code uses the same methods as japhy's example above. I know I've never used the (..., undef, ...) method, but I have used the (...)[0,1,4] method many times. Granted, my experience doesn't include everyone, but I don't recall seeing the undef example in any Perl documentation, while japhy's example (if I recall correctly) is shown.

        However, as always it's just a personal opinion. :)

Re: Declaring variables - is it legal to do this?
by TomDLux (Vicar) on May 15, 2004 at 06:46 UTC

    I'm confused. Why would you ask, "Is it legal Perl to do XXXX"? The obvious way to find out is to write a script with one or a few lines of code, and see how the Perl program reacts to it. Forgive me if I seem blunt, but in the time you wrote your message, you could have determined the answer.

    If what you really mean is, "This compiles, but I don't understand what it is", why not ask that question?

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      I'm a Perl newbie, and I'm very surprised by those 3 answers, that say "Try it, and you'll see". You wouldn't say that with some other languages (such as C or C++) because of undefined or unspecified behaviours: it may compile, it may work how you expected (with your compiler), but that's not enough to be sure the behaviour is guarranted. Personnaly, I would have asked such a question, even after trying it, to be sure I'm not wrong.
      I understand why people love Perl :)

        Yes, but there's only one Perl interpreter, so whatever the doncumentation says, the interpreter (perl) is always right. (Larry is always right, in other words.)

        my (undef) surprised me a bit btw.

Re: Declaring variables - is it legal to do this?
by ysth (Canon) on May 16, 2004 at 06:32 UTC
    As some say, there is only one implementation of the perl interpreter (actually, not true, there's ponie, but that IIUC uses identical parsing). The catch is that undocumented functionality is subject to change, whether purposeful or accidental. The best thing to do when you encounter a case like this where the behavior you see is reasonable is to submit a doc patch and ideally a new test to go with it.

    I note that there is an example of my(undef.,..) in perltie back in 5.005_0X.