Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Constants and Subclasses...

by diotalevi (Canon)
on Dec 19, 2006 at 22:18 UTC ( [id://590810]=note: print w/replies, xml ) Need Help??


in reply to Re: Constants and Subclasses...
in thread Constants and Subclasses...

You wrote:
BEGIN { my $constants = ( FOO => 'foo', BAR => 'bar', BAZ => 'baz', ); foreach my $name ( keys %constants ) { no strict 'refs'; *{$name} = sub () { $constants{$name} }; } ... }</blockquote>
If you're going to make constants, do them the proper way with the constant pragma. What you wrote is roughly how constant.pm works in versions prior to 5.9 but the next versions of perl have a different implementation for constant and you'll want your code to do the right thing regardless.

BEGIN { require constant; my %constants = ( FOO => 'foo', BAR => 'bar', BAZ => 'baz', ); foreach my $name ( keys %constants ) { constant->import( $name => $constants{$name} ); } }

You also goofed and made a variable $constants instead of %constants.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^3: Constants and Subclasses...
by t'mo (Pilgrim) on Dec 22, 2006 at 18:53 UTC

      Well sure. use constant FOO => 'bar'; is just shorthand for the following:

      BEGIN { require constant; constant->import( FOO => 'bar' ); }

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found