0

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?

Cadz
  • 139
  • 3
  • 21

1 Answers1

1

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.

Miguel Prz
  • 13,718
  • 29
  • 42