Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

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

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';
my $bar = "$                     foo";

This also works with variable declarations:

my $



bar = "foo
";
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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, it is part of the language. No, you should not use it for serious code. As for being confusing in interpolation, all dollar signs (that are not part of a variable) should be escaped, not just the ones next to letters, so it shouldn't be a problem.

I do not know if this is the real reason behind allowing whitespace in between the sigil and the variable name, but it allows you to do things like

my $            count = 0;
my $file_handle_foo   = IO::File->new;

which might be seen by some people as handy (since it puts the sigils and the unique parts of the variable names next to each other). It is also useful for Obfu (see the end of line 9 and beginning of line 10):

#!/usr/bin/perl -w                                      # camel code
use strict;

                                           $_='ev
                                       al("seek40D
           ATA,0,                  0;");foreach(1..3)
       {<DATA>;}my               @camel1hump;my$camel;
  my$Camel  ;while(             <DATA>){$_=sprintf("%-6
9s",$_);my@dromedary           1=split(//);if(defined($
_=<DATA>)){@camel1hum        p=split(//);}while(@dromeda
 ry1){my$camel1hump=0      ;my$CAMEL=3;if(defined($_=shif
        t(@dromedary1    ))&&/S/){$camel1hump+=1<<$CAMEL;}
       $CAMEL--;if(d   efined($_=shift(@dromedary1))&&/S/){
      $camel1hump+=1  <<$CAMEL;}$CAMEL--;if(defined($_=shift(
     @camel1hump))&&/S/){$camel1hump+=1<<$CAMEL;}$CAMEL--;if(
     defined($_=shift(@camel1hump))&&/S/){$camel1hump+=1<<$CAME
     L;;}$camel.=(split(//,"40..m`{/J47134}L^7FX"))[$camel1h
      ump];}$camel.="
";}@camel1hump=split(/
/,$camel);foreach(@
      camel1hump){chomp;$Camel=$_;y/LJF7173175`47/616263
      06465666770/;y/12345678/JL7F17517347`/;$_=reverse;
       print"$_40$Camel
";}foreach(@camel1hump){chomp;$Camel=$_;y
        /LJF7173175`47/12345678/;y/12345678/JL7F175173 47`/;
         $_=reverse;print"40$_$Camel
";}';;s/s*//g;;eval;   eval
           ("seek40DATA,0,0;");undef$/;$_=<DATA>;s/s*//g;(   );;s
             ;^.*_;;;map{eval"print"$_"";}/.{4}/g; __DATA__   124
               1   5014540165163145401571 46401  41
                    40143141  1551451 54401   51155  141
                    147145  40151156 40141    16316 3
                     157143   15114116  41511     57156
                     40167  1511641   5040      1201
                     45162   1544015    1163      04014
                     1401   641621      41144       145
                     15514    1162       1534        0157
                      146     04011     747         1221
                      4515      11541  54171          40
                      46         01210116            316
                      315           714315             114
                      116            4145163           54
                     40            11115614         356
                    40         12516314514         440
                    1671        511641  50         40160
                  145162                              155151
                163163                                1511
              5715656

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...