Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape code-like things in a Perl string?

$i=1; while($i<3) { print << "EOT"; def px$i = new E(user) if (!px$i.hasErrors()) { println "${px$i.name} / ${px$i.empr.to} OK" }

EOT

    $i++;
}

produces the error:

Can't call method "px" without a package or object reference at borrar.pl line 3.

How can I "escape" the if ?

Thanks.

like image 783
xain Avatar asked Feb 24 '10 16:02

xain


2 Answers

It's kind of hard to tell what this code is supposed to accomplish, but maybe you want the outer dollar signs in the println statement to be preserved in the final output?

println "\${px$i.name} / \${px$i.empr.to} OK"

With that change, the error-free output I see is:

def px1 = new E(user) 
if (!px1.hasErrors()) {
        println "${px1.name} / ${px1.empr.to} OK"
}

def px2 = new E(user) 
if (!px2.hasErrors()) {
        println "${px2.name} / ${px2.empr.to} OK"
}
like image 158
Sean Avatar answered Oct 05 '22 17:10

Sean


The command line option -MO=Deparse shows you how Perl has interpreted your code after simplifying it (e.g. converting heredocs to qq{} blocks). e.g.

$ perl -MO=Deparse test.pl
$i = 1;
while ($i < 3) {
    print qq[    def px$i = new E(user) \n    if (!px$i.hasErrors()) {\n            println "${$i->px . 'name';} / ${$i->px . 'empr' . 'to';} OK"\n    }\n\n];
    ++$i;
}

The relevant part is:

println "${$i->px . 'name';} / ${$i->px . 'empr' . 'to';}

Perl has converted ${px$i.name} to ${$i->px . 'name'} !

In perl, ${...} means to evaluate whatever is inside the block, and treat it as a symbolic reference (i.e. a variable name) or a scalar reference, then dereference it to turn it back into a scalar. So Perl tries to execute whatever is inside those blocks, treating their contents as Perl code. This is because your heredoc, "EOT" is like a double-quoted string, and interpolates dollar signs.

Solution is: escape your dollar signs ($ -> \$) or use single quotes and concatenation rather than heredocs.

like image 42
rjh Avatar answered Oct 05 '22 17:10

rjh