scope - Variable Scoping Issue in perl -



scope - Variable Scoping Issue in perl -

i not sure going on code, believe has how variables scoped, changing them "my" "our" doesn't anything. error comes in sec if block seek print $question1, perl says "$question1 requires specific bundle name". code there test need later in program. need $question variables able used throughout program.

foreach $line ( split /:/, $test ) { $match1 = "1"; $match2 = "2"; if ( $line =~ /$match1/ ) { $question1 = $line; print "$question1\n"; } if ( $line =~ /$match2/ ) { $question2 = $line; print "$question2\n"; print "$question1\n"; } }

to increment scope of variable beyond block, need move declaration outside of block, so:

my ($question1, $question2); # both initialized undef foreach $line (split /:/, $test) { $match1 = "1"; $match2 = "2"; if ($line =~ /$match1/) { $question1 = $line; # not declaring 'my', assigning } if ($line =~ /$match2/) { $question2 = $line; # not declaring 'my', assigning } }

perl scope

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -