Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How Constant pragma works with multi constants

by manorhce (Beadle)
on Feb 06, 2013 at 13:21 UTC ( #1017423=perlquestion: print w/ replies, xml ) Need Help??
manorhce has asked for the wisdom of the Perl Monks concerning the following question:

I could not understand how constant pragma works when we are providing more than one constant like below

use constant { STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3}; print STRAIGHT_LINE_ID;

Instead of providing

use constant STRAIGHT_LINE_ID => 1; use constant PRORATED_ID => 2; use constant CBP_CUSTOM_ID => 3}

my point is that while "{}" is used for hash reference but how it is taking and works

Comment on How Constant pragma works with multi constants
Select or Download Code
Re: How Constant pragma works with multi constants (UTSL)
by Anonymous Monk on Feb 06, 2013 at 13:23 UTC
Re: How Constant pragma works with multi constants
by BrowserUk (Pope) on Feb 06, 2013 at 13:26 UTC
    my point is that while "{}" is used for hash reference but how it is taking and works

    It looks inside the hash and creates one constant subroutine for each key/value pair, with the key as the name and the value as the value.

    Now what's your real question? :)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How Constant pragma works with multi constants
by toolic (Chancellor) on Feb 06, 2013 at 13:26 UTC
Re: How Constant pragma works with multi constants
by manorhce (Beadle) on Feb 06, 2013 at 13:37 UTC

    If it is making hash reference then how the value is printed without reference means only calling the name of key

    Please correct me if I am wrong, We can not access directly to memory and writing constants in this way creating a hash reference inside the memory

      If it is making hash reference...

      constant doesn't make a hash reference; the programmer does that. constant takes a hash reference as an argument and uses the data contained in the referent (the key/value pairs) to create constants (empty-prototyped functions). Again, see the source.

      If it is ...

      So if you don't you read the source of constants.pm and you don't see what the code does , then you can read the source and see what the source code does, and then the answer will emerge or it won't but maybe then something other

Re: How Constant pragma works with multi constants
by tobyink (Abbot) on Feb 06, 2013 at 13:43 UTC

    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
Re: How Constant pragma works with multi constants
by 7stud (Deacon) on Feb 06, 2013 at 17:30 UTC
    use strict; use warnings; use 5.012; sub show { say shift; } show(10); show 5; --output:-- 10 5

    Continuing on:

    sub my_use { my $href = shift; for my $key (keys %$href) { say $href->{$key}; } } my $href = { STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3 }; my_use($href); my_use $href; --output:-- 3 2 1 3 2 1

    And...for the grand finale:

    my_use({STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3}); my_use {STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3}; my_use { STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3 }; --output:-- 3 2 1 3 2 1 3 2 1
    A variable and the thing it refers to can be used interchangeably in your code. You can also write hashes and arrays across multiple lines.
Re: How Constant pragma works with multi constants
by manorhce (Beadle) on Feb 07, 2013 at 09:49 UTC

    Can you please tell me what is structure of anonymous hash (without name) is stored in Symbol Table how Perl decodes the value here

    use constant { STRAIGHT_LINE_ID => 1, PRORATED_ID => 2, CBP_CUSTOM_ID => 3};

    Here the anonymous has not any name

    If the code is written like below then I can say the value can be fetched from memory with perticular name

    use constant TICKET_FIELDS => qw(ticket_id date_modified date_created ); my %ticket_fields = map {$_=>1} TICKET_FIELDS;

    Please correct me whether I am wrong to store a anything in memory a variable is needed, and anonymous hash itself a memory and without name. How the value is fetched?

      You are replying to yourself. Moreover, the answer to the question has already been given: The anonymous hash is used as a parameter to constant::import, which can easilly access it in its @_.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2015-04-27 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who makes your decisions?







    Results (501 votes), past polls