Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Why doesn't "my ($x) = @);" generate a syntax error?

by zork42 (Monk)
on Jun 13, 2013 at 06:45 UTC ( #1038684=perlquestion: print w/replies, xml ) Need Help??
zork42 has asked for the wisdom of the Perl Monks concerning the following question:

I accidentally typed "my ($x) = @);" instead of "my ($x) = @_;"
Shouldn't the former have generated a syntax error?
use strict; use warnings; foo(1); sub foo { my ($x) = @); # why doesn't this generate a syntax error? print $x; } # Program Output: "Use of uninitialized value $x in print at l +ine 9."
perlvar.html says there is a "$)" variable, but "@)" is not mentioned.
I suspect I've missed something obvious, but I can't see what!

Replies are listed 'Best First'.
Re: Why doesn't "my ($x) = @);" generate a syntax error?
by tobyink (Abbot) on Jun 13, 2013 at 06:50 UTC

    For any punctuation character x, if there is a magic variable called $x, then there must also be magic variables called @x and %x. That's an artefact of how current versions of the Perl parser work. So because the $) exists and does something useful, the @) variable must also exist (even if it does nothing useful!)

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Correction: for any punctuation character x except '[' and '{', there exist global variables $x, @x and %x. Those variables are not necessarily magic, and their existence doesn't depend on the fact that $x does something useful. For example, none of $}, @} and %} are magic, yet they are all global variables.

        $[, @[ and %[ work (or at least they do in 5.18.0). If ${, @{ and %{ were to work, that would break dereferencing.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      I'd have never realised that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1038684]
Approved by tobyink
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-07-27 18:11 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (419 votes). Check out past polls.