Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Is space supposed to be ignored between a sigil and its variable name?

by boom (Scribe)
on Apr 18, 2009 at 11:26 UTC ( [id://758440]=perlquestion: print w/replies, xml ) Need Help??

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

I just discovered that perl ignores space between the sigil and its variable name and was wondering if someone could tell me if this was the expected behaviour. I've never run into this before and it can result in strange behaviour inside of strings. For example, in the following code, $bar will end up with the value 'foo':

my $foo = 'foo';<br/> my $bar = "$ foo";<br/> <p>This also works with variable declarations:</p> <code>my $ bar = "foo\n"; print $bar;

The second case doesn't really matter much to me but in the case of string interpolation this can lead to very confusing behaviour. Anyone know anything about this?

  • Comment on Is space supposed to be ignored between a sigil and its variable name?
  • Download Code

Replies are listed 'Best First'.
Re: Is space supposed to be ignored between a sigil and its variable name?
by jettero (Monsignor) on Apr 18, 2009 at 11:48 UTC
    Definitely. It isn't confusing at all though. Perl ignores whitespace. That's actually all you need to know.
    my # token $ # token x # token = # token "1" # token ; # token # statement detected!!
    I've seen people do things like this from time to time...  $ var -> x [ 4 ] = 3 ;. The feel a bit stuttering to me, but some people like all that extra space.

    -Paul

      Perl ignores whitespace.

      ...except where it doesn't :)

      Like for example in  < $fh >  vs.  <$fh>.  Or even in <$ fh> — to keep it on-topic.

        Yeah, I suspect the perl toeknizer turns <$fh> into a single token... probably historical, since it always used to be <BLARG> and <> also does <stuff.*> ... so yeah, fine except where it doesn't work -- like way too much overloaded operators?

        -Paul

      Sure it's confusing. Without running the code, tell me what $i and $j are.

      my $x = ['a', 'b', 'c', 'd', 'e']; my $i = $ x -> [ 4 ]; my $j = "$ x -> [ 4 ]"; print "$i\n"; print "$j\n";
        No :) some things aren't defined well, stick to what is defined :)
        my $x = ['a', 'b', 'c', 'd', 'e']; my $i = $ x -> [ 4 ]; my $j = "$ x -> [ 4 ]"; my $k = "$x->[4]"; warn $i; warn $j; warn $k; __END__ e at - line 6. ARRAY(0x225f04) -> [ 4 ] at - line 7. e at - line 8.
Re: Is space supposed to be ignored between a sigil and its variable name?
by Anonymous Monk on Apr 18, 2009 at 11:38 UTC
Re: Is space supposed to be ignored between a sigil and its variable name?
by toolic (Bishop) on Apr 18, 2009 at 13:08 UTC
    Others have answered your question regarding sigils and whitespace.

    Perhaps you could avoid unexpected behavior by being more deliberate about using interpolating quotes only when you want to interpolate. Otherwise, always use non-interpolating quotes when you do not want to interpolate. The following will set the $bar variable equal to a string containing a literal dollar sign, followed by a number of spaces, followed by the literal string 'foo' (if that is what you really want).

    my $bar = '$ foo';

    See Quote and Quote like Operators

      Unless you 'wanted' to interpolate

      my $bar = "$ foo and $cake";

      Or a more likely example

      my $bar = "$ $total_foo_dollars";

      I had no idea that spaces in quotes could be abused this way and it just feels so wrong.

      How about

      my $foo = ['a', 'b', 'c']; my $bar = "$ foo -> [2]";

      vs

      my $foo = ['a', 'b', 'c']; my $bar = "$foo->[2]";

      Before reading this thread I would have a clear answer for what the result of $bar would be in each case. After reading this thread I had a less clear different answer. And after actually testing the code I can see now that I was wrong in all my assumptions. Now I've got a nagging feeling that I should review all my code for unintended quoting results.

        What do those print, and why?  (Without cheating and running the code! :)

        #!/usr/bin/perl $foo = "foo"; print "1: $ $ $ foo\n"; print "2: $ $$ foo\n"; print "3: $ $$foo\n"; print "4: $$$foo\n";
Re: Is space supposed to be ignored between a sigil and its variable name?
by Bloodnok (Vicar) on Apr 18, 2009 at 12:43 UTC
    As others have said elsewhere on this thread, perl ignores whitespace - if you've ever used perltidy on a script/module containing sub attributes, by default, it [perltidy] reformats sub :Attribute name {... to read sub : Attribute name {... i.e. positively inserts whitespace either side of the attribute sigil (':') if that's what it is ;-)

    A user level that continues to overstate my experience :-))
      perl ignores whitespace
      No it doesn't. If that where true, then there wouldn't be any difference between:
      $foo =~ /bar/;
      and
      $ f o o = ~ / b a r /;
      the latter not only does not compile, but the spaces inside // are significant, and while =~ is one token, = ~ are two. With a different meaning.

      The reason that there is optional whitespace after the sigil is a left over from perl4. And probably has to do with the sigil and the variable name lexed as different tokens. And in almost all cases, Perl allows whitespace between tokens.

      Now, in Perl6, the sigil is actually part of the variable name. Hence, '$foo' is a single token. And hence, no whitespace allowed between the sigil and the name.

Re: Is space supposed to be ignored between a sigil and its variable name?
by Corion (Patriarch) on Apr 20, 2009 at 13:35 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found