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

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

Replies are listed 'Best First'.
Re^2: Is space supposed to be ignored between a sigil and its variable name?
by ruzam (Curate) on Apr 18, 2009 at 16:38 UTC

    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";
        Excellent exercise :D

        Okay, okay, I admit it - I got 1 right, but the other 3 flummoxed me, so I ... well I cheated!!

        Now I know what they all print - all that's left to do now is the hard part i.e. RTFM & work out why ...

        A user level that continues to overstate my experience :-))