"> which coincidently, is how $x = 192 . 168 . 1 . 1 is parsed. Cannot reproduce"
Because spaces are significant here: my $x = 192.168.1.1; # $x is chr(192) . chr(168) . chr(1) . chr(1) my $y = 192 . 168 . 1 . 1; # $y is "19216811" Daniel From: Ingo Schwarze <[email protected]> To: Ken Cornetet <[email protected]> Cc: [email protected] Date: 01/04/2011 12:55 Subject: Re: Parsing/interpolation question Sent by: [email protected] Hi, Ken Cornetet schrieb am Thu, Mar 31, 2011 at 11:04:31AM -0400: > I ran into an issue creating hashes with IP addresses as a key. > They don't do what you'd expect them to do (at least not what I > expected). In other words, $hash{192.168.1.1} isn't the same > as $hash{'192.168.1.1'} Right, in this case, the quotes are required. > I understand what's between the "{" and "}" is subject to > interpolation, Not quite, it is interpreted as an expression, except when it is a bare identifier, see perldata(1), section DESCRIPTION, subsection "Scalar value constructors": In fact, an identifier within such curlies is forced to be a string, as is any simple identifier within a hash subscript. Neither need quoting. Our earlier example, $days{'Feb'} can be written as $days{Feb} and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression. This means for example that "$version{2.0}++" is equivalent to "$version{2}++", not to "$version{'2.0'}++". > but I don't understand what perl interpolation rules apply. > > What I found is that something like 192.168.1.1 seems to > parsed/interpolated as chr(192) . chr(168) . chr(1) . chr(1) Right, see the same manual page, right afterwards, "Version strings". A literal of the form "v1.20.300.4000" is parsed as a string composed of characters with the specified ordinals. This form, known as v-strings, provides an alternative, more readable way to construct strings, rather than use the somewhat less readable interpolation form "\x{1}\x{14}\x{12c}\x{fa0}". This is useful for representing Unicode strings, and for comparing version "numbers" using the string comparison operators, "cmp", "gt", "lt" etc. If there are two or more dots in the literal, the leading "v" may be omitted. Personally, i consider that a rather ugly feature i'd tend to avoid. > I would have expected that if any interpolation were to be done > on 192.168.1.1 the periods would be parsed as a string append giving > the result "19216811", This is Perl; the language design is _not_ based on the principle of least surprise. > which coincidently, is how $x = 192 . 168 . 1 . 1 is parsed. Cannot reproduce: $ perl -Mstrict -we 'my $x = 192.168.1.1; print $x;' \ > | hexdump -e '4/1 " %d"'; echo 192 168 1 1 $ perl --version | head -n2 This is perl 5, version 12, subversion 2 (v5.12.2 (*)) built for i386-openbsd Yours, Ingo _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs _______________________________________________ ActivePerl mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
