esharris has asked for the wisdom of the Perl Monks concerning the following question:

Why does this program ...
#!/usr/bin/perl -w use strict; my $x = 'Dwayne Johnson'; print <<XXX; $x's the Rock. XXX
produce this output in perl, v5.8.1?
Name "x::s" used only once: possible typo at ./test.pl line 7. Use of uninitialized value in concatenation (.) or string at ./test.pl + line 7. the Rock.
The problems go away if I prepend the single quote with a backslash.

Replies are listed 'Best First'.
Re: <<, variable, and single quote behaving badly.
by saintmike (Vicar) on Apr 26, 2004 at 17:38 UTC
    $x's is a deprecated way of writing $x::s, a relict from Perl 4 days.
Re: <<, variable, and single quote behaving badly.
by bart (Canon) on Apr 26, 2004 at 19:15 UTC
    Simply put: $x::s and $x's are equivalent to perl. The reason for that is backward compatibility: the apostrophe was perl4's package name separator, on perl5, it's the double semicolon. But the apostrophe still works.

    Sample code:

    $Foo'x = 123; print $Foo::x;
    Result:
      123
    

    The odd thing might be that this still doesn't produce a warning under perl5.8.3.

      For better or worse (better I suppose), this syntax will disappear in Perl 6 according to Apocalypse 2. See Larry's comments under RFC 071. Of course, large parts of A02 have been rethought since then, but I don't think this is one of them.
Re: <<, variable, and single quote behaving badly.
by Plankton (Vicar) on Apr 26, 2004 at 17:34 UTC
    $ cat monk.pl #!/usr/bin/perl -w use strict; my $x = 'Dwayne Johnson'; print <<XXX; ${x}'s the Sock. XXX $ ./monk.pl Dwayne Johnson's the Sock.
    Think about it ... say if you wanted to say:
    Dwayne Johnson_s the Sock.
    Would you say ...
    print <<XXX; $x_s the Sock. XXX
    ... now is $x_s a variable or not? {} and \ tell << that x is a variable.

    Plankton: 1% Evil, 99% Hot Gas.
A reply falls below the community's threshold of quality. You may see it by logging in.