Perl: what's the point. = ~

In a perl script, I sometimes wrote

my $s = "text";
$s .=~ " another text";
print "$s\n";

      

The expected output was text another text

not printed, instead, strange text was shown in my terminal like textߞ ߋ

.

No doubt about it: the error was the operator .=~

, although I really wanted to write.=

But I'm curious: why is there no .=~

syntax error? What is the meaning of this operation?

+3


source to share


3 answers


When choroba is not around;) You can use the B :: Deparse and ppi_dumper , to tell you what you're dealing with ( .=

and ~

)



$ perl -MO=Deparse,-p -e " $foo .=~ /bar/; "
($foo .= (~/bar/));
-e syntax OK


$ ppi_dumper foo.pl
PPI::Document
  PPI::Statement
    PPI::Token::Symbol          '$foo'
    PPI::Token::Whitespace      ' '
    PPI::Token::Operator        '.='
    PPI::Token::Operator        '~'
    PPI::Token::Whitespace      ' '
    PPI::Token::Regexp::Match   '/bar/'
    PPI::Token::Structure       ';'

      

+3


source


.=~

- two operators, .=

and ~

. The tilde is the binary negation operator, see perlop .

Check:



$ perl -MO=Deparse,-p -e '$x .=~ $y'
($x .= (~$y));
-e syntax OK

      

+9


source


When Perl does something that you don't understand syntactically, you either B :: Deparse or B :: Concise to figure it out.

B :: Deparse

Running

$ perl -MO=Deparse

      

on this code:

my $s = 'text';
$s .= "\337\236\221\220\213\227\232\215\337\213\232\207\213";
print "$s\n";

      

B :: Compressed

Running

$ perl -MO=Concise,-exec 

      

on this code:

1  <0> enter 
2  <;> nextstate(main 1 -:1) v:{
3  <$> const[PV "text"] s
4  <0> padsv[$s:1,2] sRM*/LVINTRO
5  <2> sassign vKS/2
6  <;> nextstate(main 2 -:2) v:{
7  <0> padsv[$s:1,2] sRM
8  <$> const[PV "\337\236\221\220\213\227\232\215\337\213\232\207\213"] s
9  <2> concat[t3] vKS/2
a  <;> nextstate(main 2 -:3) v:{
b  <0> pushmark s
c  <0> padsv[$s:1,2] s
d  <$> const[PV "\n"] s
e  <2> concat[t4] sK/2
f  <@> print vK
g  <@> leave[1 ref] vKP/REFC

      

In both cases, the answer is the same. You have a literal text filled with many strange characters. This is the result of the compiler applying unitary bitwise negation to the literal at compile time and storing the result in the parse tree. ~

+8


source







All Articles