Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

hash key stringification

by ig (Vicar)
on Apr 05, 2008 at 23:39 UTC ( #678576=perlquestion: print w/ replies, xml ) Need Help??
ig has asked for the wisdom of the Perl Monks concerning the following question:

My question is: how is the expression in $hash{expression} stringified?

perldata says anything more complicated than a simple identifier inside a hash subscript will be interpreted as an expression but it doesn't say how such an expression is stringified.

I recently (just this week, after many years of using perl) came across the subscript separator, nicely described in perlvar.

The form of the expression I was trying to understand was $href->{$x,$y}.

I spent many hours looking for an explanation of this syntax before a fortuitous google search turned up the entry in perlvar, where I don't think I ever would have looked on my own.

I had looked in perlsyn, perldata and perlop and searched extensively on-line without finding a description of this form. Was I missing something?

It would have helped me tremendously (as in, saving me many hours of searching) had the perldata man page mentioned, where it describes how the content of the curlies is forced to be a string, somethinge about $; and multidimensional array emulation.

So, now I am wondering if there is a better reference to perl syntax and semantics for perl 5.8.8 than perldata, perlsyn and perlop. Any suggestions?

Also, how should I, if I were so bold, suggest an improvement to the standard documentation?

Comment on hash key stringification
Re: hash key stringification
by ikegami (Pope) on Apr 06, 2008 at 00:44 UTC

    My question is: how is the expression in $hash{expression} stringified?

    Same as everywhere else.

    • A number stringifies to a string representing that number. The exact format is not specified and could very well be platform-dependent. Use sprintf to get more control.
    • undef stringifies to the NUL (0-length) string (and issues a warning).
    • Objects of classes that overload stringification can stringify however they like.
    • Compiled regexs stringify into the source regexp.
    • Blessed references get stringified to some unique opaque value. (For example, Class=REFTYPE(0xADDRESS))
    • References get stringified to some unique opaque value. (For example, REFTYPE(0xADDRESS))

    I think that's all the scalar subtypes?

    You can force stringification a number of ways, including

    "$var"
    and
    '' . EXPR

    The form of the expression I was trying to understand was $href->{$x,$y}

    $href->{$x,$y} is rarely used and probably shouldn't be used. I think the only reason it exists is for backwards compatibility with very old Perls. I agree that it should be mentioned in perldata.

    Also, how should I, if I were so bold, suggest an improvement to the standard documentation?

    perlbug

      Thanks for the help ikegami.

      I am thinking to submit a bug report on the documentation with the following suggestion for an addition:

      Hash keys are always strings. Hash subscripts that are not strings are converted to strings before being used as keys. A subscript may be a string, simple bareword, list, array or expression. A string is used as-is. A bareword is treated as if it were a quoted string. A list is joined with the subscript separator C<$;> (see L<perlvar>). Note that it is not joined with the list separator C<$">, as it would be if it were being interpolated into a double quoted string. The following are equivalent: $hash{'a', 'b', 'c'} = 1; $hash{join($;, 'a', 'b', 'c')} = 1; Arrays are evaluated in scalar contex and the result is converted to a string. Note the difference between an array and a list, but also that an array can be interpolated into a list. Alternatively, an array can be interpolated into a double quoted string. In the following, 1 and 2 are the same, as are 3 and 4, as are 5 and 6. @array = ('a', 'b', 'c'); $hash{@array} = 1; # 1 $hash{'3'} = 1; # 2 $hash{(),@array} = 2; # 3 $hash{join($;, @array)} = 2; # 4 $hash{"@array"} = 3; # 5 $hash{join($",@array)} = 3; # 6 Everything else is evaluated as an expression and the result is converted to a string. Note that while references can be used as hash subscripts and are easily converted to string representation it is not easy to convert the string representation back to a reference. Note also that array slices are handled differently than either lists or arrays. They are processed like lists are in other contexts and yield the last element of the slice.

      Any suggestions before it goes to bother those busy people would be much appreciated.

        Arrays are evaluated in scalar context and the result is converted to a string.

        Not just arrays, but all other expressions.

        Everything else is evaluated as an expression and the result is converted to a string

        "as an expression" is redundant since only expressions can be evaluated, and you forgot to mention scalar context. Perhaps "All other expressions are evaluated in scalar context and their result is converted to a string."

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-08-29 15:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (281 votes), past polls