Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(ar0n) Re: Accessing Constant Hashes

by ar0n (Priest)
on Jan 16, 2002 at 02:27 UTC ( #139061=note: print w/ replies, xml ) Need Help??


in reply to Accessing Constant Hashes

keys %{+CONST}
or
keys %{CONST()}

[ ar0n -- want job (boston) ]


Comment on (ar0n) Re: Accessing Constant Hashes
Select or Download Code
Replies are listed 'Best First'.
Re: (ar0n) Re: Accessing Constant Hashes
by enoch (Chaplain) on Jan 16, 2002 at 02:32 UTC
    If I could pick your brain, is the constant pragma implemented as a sort of psuedo-subroutine? Otherwise, why would calling the constant with an open and close paren force it to resolve to a hash?

    ++ar0n and thanks,
    Jeremy

      As ar0n already replied, the constant pragma does create a subroutine. If you're curious, this is an inlined subroutine with a null prototype (one of the few places where a prototype is really useful). Looking in the constant module, the following code is where the constant is actually created:

      { no strict 'refs'; my $full_name = "${pkg}::$name"; $declared{$full_name}++; if (@_ == 1) { my $scalar = $_[0]; *$full_name = sub () { $scalar }; } elsif (@_) { my @list = @_; *$full_name = sub () { @list }; } else { *$full_name = sub () { }; } }

      What the null prototype does is allow your constant to behave like a Perl built-in:

      use constant FOO => 7; print FOO + 1;

      That prints 8, just like you would expect. Contrast that to this:

      sub FOO {7}; print FOO + 1'

      That will print 7. Why? Because Perl will interpret the +1 as being an argument to &FOO, which is silently discarded as it is not used. Yuck! With the null prototype, Perl knows that nothing coming after FOO can be an argument, so everything parses as you expect. It's also nice to note, from the docs, that as of Perl 5.004, Perl will replace all instances of your constant with the returned value, thus saving you the overhead of a subroutine call.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      It's exactly that, a subroutine.

      Calling it with a () -- or a + -- disambiguates it from %{CONST}, which is the equivalent of %CONST.

      [ ar0n -- want job (boston) ]

        ++ for disambiguates
           larryk                                          
        perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://139061]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2015-08-02 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (15 votes), past polls