How can I avoid code things in a Perl string?

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

EOT

    $i++;
}

causes an error:

It is not possible to call the px method without referencing a package or object on the borrar.pl line.

How can I "run away" if?

Thanks.

+5
source share
5 answers

It's hard to say what this code should do, but maybe you need to save the external dollar characters in the println statement in the final release?

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

With this change, I see error-free output:

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"
}
+6
source

-MO=Deparse , Perl (, heredocs qq {}). .

$ 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;
}

:

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

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

perl, ${...} , , ( ) , , . Perl , , Perl. , heredoc, "EOT" .

: ($\$) , heredocs.

+4

.

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

, $px . :

$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++;
}

For more information on erasing a line in perldoc perlop , see “Gory parsed constructions details”.

+1
source
my $format = << 'EOT';
def px%d = new E(user)
    if (!px%d.hasErrors()) {
            println "${px%d.name} / ${px%d.empr.to} OK"
    }
EOT

for my $i ( 1 .. 3 ) {
    printf $format, ($i) x 4;
}
+1
source

All Articles