Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Surprising whitespace behavior

by steves (Curate)
on Sep 23, 2004 at 13:45 UTC ( #393196=perlquestion: print w/replies, xml ) Need Help??

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

This observation was provided by a co-worker. He and I were both surprised that this worked as it does:

use strict; use warnings; my $s = 'foo'; print "s=$ s\n";
But I would have expected this to be consistent and it's not:
use strict; use warnings; my @a = (qw/a b c def/); my $s = "foo"; print "s=$ s, a=@ a\n";
s=foo, a=@ a

Is there a defined rule for the whitespace allowed between a variable's type identifier and its name? Or is this just an anomoly of variable interpolation in quoted strings?

Replies are listed 'Best First'.
Use B::Deparse to see why
by htoug (Deacon) on Sep 23, 2004 at 14:05 UTC
    If you try perl -MO=Deparse you will see that the perl compiler interprets the print statement as you surmised:
    my(@a) = ('a', 'b', 'c', 'def'); my $s = 'foo'; print "s=$s, a=\@ a\n"; - syntax OK
    Why there is a difference between the interpretation of $ followed by whitespace and @ followed by whitespace probably lies in the fact that @-interpolation is a quite new invention (5.6 AFAICR).

    -MO=Deparse is very handy for finding out why a coinstruct does what it does (and for deobfuscation).

      probably lies in the fact that @-interpolation is a quite new invention (5.6 AFAICR).
      Oh, you young'uns. {grin}

      @-interpolation in a string dates back to Perl version 5.000. It's the reason that you get the fatal error when you have something like "".

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        @-interpolation in a string dates back to Perl version 5.000.

        Oh, you young'uns. (:

        It dates back way before that. I've personally used @-interpolation in strings in Perl v3.

        > perl4 -e "print qq<(tye@ARGV)\n>" this that (tyethis that) > perl4 -e "print qq<(tye@foo)\n>" this that (tye@foo)

        What changed with Perl5 was that the DWIM of optional interpolation was removed.

        The only reason the second example needs to be fatal in Perl5 is so that the change in behavior was not a "silent change".

        I suspect you know all of this, but your node was at least misleading so I felt clarification was in order.

        - tye        

      5.005_3 had @-interpolation. I'm not sure about older Perls.

      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Surprising whitespace behavior
by QM (Parson) on Sep 23, 2004 at 18:21 UTC
    Not knowing which post to reply to...

    So now that we've determined that "$ x" behaves differently from "@ x", why stop there?

    DB<1> $x = 'DollarX' DB<2> @x = 'a'..'g' DB<3> %x = qw(1 a 2 b 3 c) DB<4> sub x { print "This is Sub x\n" } DB<5> p "<$x> <@x> <%x> <&x>" <DollarX> <a b c d e f g> <%x> <&x> DB<6> p "<$ x> <@ x> <% x> <& x>" <DollarX> <@ x> <% x> <& x> DB<7> p "$ #x" Final $ should be \$ or $name at (eval 12)[C:/Perl/lib/] +line 2, within string syntax error at (eval 12)[C:/Perl/lib/] line 2, near "} +"$ #x""
    So "$ x" is the only one this works for, and it breaks for "$ #x", even though that looks like a scalar (and it probably shouldn't break!). But "$ x[0]" produces "a", and so does "$ x{1}".

    I'm sure there's some Good Reason Why This Is So...Perhaps it's easy to figure out what to do with "$x", but there are more options with "@x". The string interpolation aspect of ignoring whitespace was probably not updated when "@x" interpolation was added. [I secretly hoped Merlyn would have the skinny on that -- but I'm sure he would have said so.]

    Quantum Mechanics: The dreams stuff is made of

      I think "$ x" should be "\$ x". I have no problem with double-quote interpolation being more strict than the equivalent Perl code (it already is and this one more case would be an improvement).

      I think it is just a matter of the original implementor(s) (way back in Perl2) deciding to support ignoring such whitespace (to match how Perl works outside of double quotes) and the more recent implementor(s) (p5p) not even thinking of supporting such a strange construct (and so not worrying about making them consistent).

      - tye        

cool - 1 column programming
by fglock (Vicar) on Sep 23, 2004 at 20:21 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://393196]
Approved by blokhead
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2023-01-28 10:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found