[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Constants as array refs



On Mar 23, John Porter said:

>>   use constant CARRAY => [1, 2, 3, 4];
>
>It is (very unfortunately) necessary to remember that constants
>defined with "use constant" are actually subroutines.
>
>	for ( @{ &CARRAY } ) { ...

More importantly, it is necessary to remember that subscripts are treated
as barewords if the subscript is a legal Perl "word" (which is any string
matching:  (note I excluded leading and trailing whitespace, because I
don't think they enter into it at all)

  m{
    \A             # beginning of string
    (?:
      [^\W\d]      # any non-digit
      \w+          # followed by any a-zA-Z0-9_ ("alphanumberscore")
      |            # or
      $VALID_NUM   # some valid numerical literal regex
    )
    \z             # end of string
  }

That's why I can say $ENV{REMOTE_HOST}, and $main::{INC}, but I can't say
$code{zone-37} -- I'd need 'zone-37', or some other quoting construct.

Thus:

  perl -wle 'use constant FOO => "a"; $this{FOO} = 10; print keys %this'
  FOO

  perl -wle 'use constant FOO => "a"; $this{+FOO} = 10; print keys %this'
  a

The unary + (which I described a couple minutes ago in an email to
FWP) de-bastardi^Wde-barewordizes ``FOO''.

Long live fun.

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy@pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe