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

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

Thread Previous | Thread Next
From:
Jesse Luehrs
Date:
May 15, 2012 10:48
Subject:
Re: [perl #112952] use strict does not catch ${^_UNDECLARED_VAR_NAME}
Message ID:
20120515174754.GB4502@tozt.net
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. 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 $@.

-doy

Thread Previous | Thread Next


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