Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How Constant pragma works with multi constants

by tobyink (Abbot)
on Feb 06, 2013 at 13:43 UTC ( #1017435=note: print w/ replies, xml ) Need Help??


in reply to How Constant pragma works with multi constants

constant.pm is pretty crufty by now, but here's a "clean-room implementation" of the same sort of idea...

use v5.12; use warnings; BEGIN { package MyConstant; $INC{'MyConstant.pm'} = __FILE__; sub import { my $caller = caller; my $class = shift; if (@_ == 1 and ref $_[0] eq 'HASH') { while (my ($key, $value) = each %{$_[0]}) { $class->_define($caller, $key, $value); } } else { $class->_define($caller, @_); } } sub _define { my $class = shift; my ($caller, $name, @values) = @_; my $code; if (@values == 1) { my $v = $values[0]; $code = sub () { $v }; } else { $code = sub () { @values }; } no strict 'refs'; *{"$caller\::$name"} = $code; } } use MyConstant { FOO => 4, BAR => 2, }; say FOO . BAR;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name


Comment on Re: How Constant pragma works with multi constants
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2015-07-28 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (257 votes), past polls