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:??;
| [reply] [d/l] |
|
|
| [reply] |
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.
| [reply] [d/l] |
|
|
Thanks, Belgarion! Please read my reply to japhy :)
| [reply] |
|
|
| [reply] |
|
|
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. :)
| [reply] [d/l] [select] |
|
|
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
| [reply] |
|
|
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 :)
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
|
|
|
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. | [reply] |