Foreach Loop in EmbPerl

63 Views Asked by At

Seems like variables declared outside foreach, initialized inside foreach will not persist its data.

Consider this example :

[-
$myVar;

foreach my $item (qw/item1 item2 item3/) {
    $myVar = $item;
}

print $myVar # This will print undef, I expect it to print item3
-]

How do I make this work with foreach loop?

1

There are 1 best solutions below

0
On

Change the print $myVar sentence to print OUT $myVar; OUT filehandle is tied to Embperl's output stream. You can use also a [+ $myVar +] block instead of printing to OUT.