develooper Front page | perl.perl5.porters | Postings from August 2016

[perl #129129] Apostrophe in String Interpolation

Thread Previous
From:
Dan Collins via RT
Date:
August 29, 2016 18:37
Subject:
[perl #129129] Apostrophe in String Interpolation
Message ID:
rt-4.0.24-636-1472495821-276.129129-14-0@perl.org
On Mon Aug 29 10:21:32 2016, colonel@monmouth.com wrote:
> I am using Perl 5.22.2 on a Fedora 23 x64 system.  I am getting
> anomalous results interpolating a string variable before an
> apostrophe.
> 
>       #!/usr/bin/perl
> 
>       $v = 1;
>       ${v's} = 23;			# Is this legal?
>       print "$v is equal to one.\n";
>       print "$v-s equal to one.\n";
>       print "$v's equal to one.\n";
> 
> Since apostrophes are not "word" characters, I would expect
> the print statements to print the same value.  Instead I get
> this:
> 
>       1 is equal to one.
>       1-s equal to one.
>       23 equal to one.
> 
> I do not see anything about this behavior in Wall-Christansen-
> Orwant or at the Monks.  It looks to me like a bug.  Is it?
> 
>                                  --George Sicherman
>                                    colonel@monmouth.com

This is the expected behavior. From `perldoc perlvar`:

    Variable names in Perl can have several formats. Usually, they must begin with a letter or underscore, in which case they can be arbitrarily long (up to an internal limit of 251 characters) and may contain letters, digits, underscores, or the special sequence :: or '. In this case, the part before the last :: or ' is taken to be a package qualifier; see perlmod.

`'` is a valid part of an identifier, just as `::` is, so they are respected while interpolating variables within double-quoted strings. `-` is not a valid part of an identifier - `${v-s}` is a syntax error, and `"$v-s"` is equivalent to `$v . "-s"`.

-- 
Respectfully,
Dan Collins

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About