tags:

views:

113

answers:

3

I just saw some code in our code base (and it's OLD code, as in Perl 3 or Perl 4 days) that looks like this (I'm simplifying greatly):

 @array;
 push( array, $some_scalar );

Notice that the array in the push() doesn't have an @. I would assume that the code behind push knows that the first argument is supposed to be array so grabs the array from the array typeglob. Is that more or less it? If Perl is able to do that without problem, why would you need to include the @ at all?

+8  A: 

This is an old 'feature' of the parser. The @ isn't mandatory in a push if the variable is a package variable. This is considered by many as a bug that ought to be fixed though. You really shouldn't be doing this.

Leon Timmermans
Hm, it doesn't seem to actually perform the push, though. With this: `perl -e 'my @array = (); push (array, "foo"); print "@array\n";'` I get no output. DId I do something dumb?
Jefromi
Maybe it does have something to do with typeglobs then. Jefromi's sample code above doesn't output anything, but if you remove the `my`, then it does.
mobrule
Yeah, you're right. It only seems to work for package variables.
Leon Timmermans
Oh, I'm pretty sure the original array wasn't declared with my. I merely wrote that into the example as a force of habit.
Morinar
+3  A: 

This is a dubious "feature" of perl, deprecated behaviour; it should be an error, but it works.

If you turn on the warnings of the compiler (perl -W, highly recommended), it warns:

Array @aa missing the @ in argument 1 of push() at xx.pl line 2.
leonbloy
+1  A: 

Nicholas Clark explains:

That's Perl 1 syntax.

daxim