how to NOT quote constants in hash keys

Discussion in 'Perl Misc' started by Martin Adler, Jun 28, 2004.

  1. Martin Adler

    Martin Adler Guest

    What can I "use" to make the following program work as I want it to?

    #!/usr/bin/perl -w
    use strict;
    use constant N => 17 ;
    my %x ;
    $x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
    print keys %x ;

    The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
    But how can I tell it to interpret constants as their (constant)
    value?
    I want Perl to interpret $x{N} as $x{17}.

    One of the goals of perl is "to make easy tasks easy and difficult
    tasks possible". It seems to me that it is POSSIBLE to use constants
    as hash keys (e.g.: $x{ (N) }), but it does not look EASY.

    Or what else should I do?
    Martin Adler, Jun 28, 2004
    #1
    1. Advertising

  2. Martin Adler

    Tore Aursand Guest

    On Mon, 28 Jun 2004 09:59:53 -0700, Martin Adler wrote:
    > #!/usr/bin/perl -w
    > use strict;
    > use constant N => 17 ;
    > my %x ;
    > $x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
    > print keys %x ;
    >
    > The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
    > But how can I tell it to interpret constants as their (constant)
    > value?
    > I want Perl to interpret $x{N} as $x{17}.


    Why didn't you read the documentation before posting? It would have saved
    you a lot of time;

    perldoc constant


    --
    Tore Aursand <>
    "Measuring programming progress by lines of code is like measuring
    aircraft building progress by weight." (Bill Gates)
    Tore Aursand, Jun 28, 2004
    #2
    1. Advertising

  3. Martin Adler

    Paul Lalli Guest

    On Mon, 28 Jun 2004, Martin Adler wrote:

    > What can I "use" to make the following program work as I want it to?
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use constant N => 17 ;
    > my %x ;
    > $x{N} = 25 ; # who wants to write $x{(N)} = 25 all the time?
    > print keys %x ;
    >
    > The output is "N", so Perl apparently interprets $x{N} as $x{"N"}.
    > But how can I tell it to interpret constants as their (constant)
    > value?
    > I want Perl to interpret $x{N} as $x{17}.
    >
    > One of the goals of perl is "to make easy tasks easy and difficult
    > tasks possible". It seems to me that it is POSSIBLE to use constants
    > as hash keys (e.g.: $x{ (N) }), but it does not look EASY.
    >
    > Or what else should I do?


    Perl is trying to make something easy. It's just not what you want to be
    easy in this case. Perl's auto-quoting is taking over here, which makes
    it easy for the vast majority of cases, when we want $x{N} to mean
    $x{'N'}, instead of the remarkably few cases when we want $x{N} to mean
    $x{(WhateverValueConstantNRepresents)}.

    You have three basic options:
    write $x{N()} instead of $x{N}
    write $x{+N} instead of $x{N}
    use the ReadOnly module from CPAN instead of the constant pragma. This
    defines constants that look like Perl variables instead of C Macros:

    use ReadOnly $N => 17;
    $x{$N} = 25;


    Paul Lalli
    Paul Lalli, Jun 28, 2004
    #3
  4. Martin Adler

    Paul Lalli Guest

    On Mon, 28 Jun 2004, Paul Lalli wrote:
    > use the ReadOnly module from CPAN instead of the constant pragma. This
    > defines constants that look like Perl variables instead of C Macros:
    >
    > use ReadOnly $N => 17;
    > $x{$N} = 25;


    I should clarify two things:
    1) the above is not actually correct syntax.
    Read the Readonly documentation for the correct syntax.

    2) Readonly.pm comes with a significant performance hit over constant.pm.
    Read teh Readonly documentation for details.

    Paul Lalli
    Paul Lalli, Jun 28, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,100
  2. Stimp
    Replies:
    2
    Views:
    2,221
    Stimp
    Sep 20, 2006
  3. Eric Layman
    Replies:
    3
    Views:
    605
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. rp
    Replies:
    1
    Views:
    478
    red floyd
    Nov 10, 2011
  5. Replies:
    2
    Views:
    117
    Tad McClellan
    Apr 15, 2005
Loading...

Share This Page