develooper Front page | perl.perl5.porters | Postings from May 2012

[perl #112952] use strict does not catch ${^_UNDECLARED_VAR_NAME}

Thread Previous
From:
Father Chrysostomos via RT
Date:
May 15, 2012 13:50
Subject:
[perl #112952] use strict does not catch ${^_UNDECLARED_VAR_NAME}
Message ID:
rt-3.6.HEAD-4610-1337114996-571.112952-15-0@perl.org
On Tue May 15 10:48:24 2012, doy@tozt.net wrote:
> On Tue, May 15, 2012 at 10:37:58AM -0700, Jim Avera wrote:
> > 'use strict' does not catch undeclared variables in the newer ${^NAME} 
> > syntax.
> > 
> > For example ${^BOGUS} and ${^_BOGUS} are silently accepted.
> > 
> > #!/usr/bin/perl
> > use strict; use warnings;
> > 
> > for my $varname ('BOGUS', '{BOGUS}', '{^_BOGUS}', '{^BOGUS}') {
> >    my $stmt = "my \$x = \$$varname;";
> >    eval $stmt;
> >    print $@;
> >    if ($@) {
> >      print "Compile error: $stmt\n";
> >    } else {
> >      print "NO      error: $stmt\n";
> >    }
> > }
> 
> Variables that begin with ^ are special to perl, and reserved. You
> shouldn't be using them in your own code anyway.

Except for ${^_...}.  perlvar says:

No
control-character name that begins with C<^_> will acquire a special
meaning in any future version of Perl; such names may therefore be
used safely in programs.  C<$^_> itself, however, I<is> reserved.

> They are like
> punctuation variables in that they are always global, and always
> reference the variable in the main:: package. For instance:
> 
>   package Foo;
>   $BAR = 1;
>   package Bar;
>   say $BAR ? "yes" : "no";
> 
> prints "no", while
> 
>   package Foo;
>   ${^BAR} = 1;
>   package Bar;
>   say ${^BAR} ? "yes" : "no";
> 
> prints "yes". This is all by design. 'strict' doesn't catch accesses
> like this for the same reason that it doesn't catch accesses to things
> like $@.

Indeed, perlvar continues:

Perl identifiers that begin with digits, control characters, or
punctuation characters are exempt from the effects of the C<package>
declaration and are always forced to be in package C<main>; they are
also exempt from C<strict 'vars'> errors.

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=112952

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About