use strict and my $wibble are not seen by include.pl. do s executes it in a different lexical scope.
That means that $wibble = "fred"; actually does $main::wibble = "fred"; (assuming main is the current package).
Because of the my $wibble (not because of use strict), the main program's $wibble refers to a lexical var and not to $main::wibble. If you were to print $main::wibble, you'd see "fred".
The most straightforward way to solve the problem is to use eval instead of do. eval runs code in the same lexical scope as the eval itself (instead of creating an entirely new scope like do), so the code sees and obeys directives such as use strict and my $wibble.
use strict; use warnings; my $include_file = 'include.pl'; my $wibble; eval do { open(my $fh, '<', $include_file) or die("Unable to open include \"$include_file\": $!\n"); local $/; <$fh> }; die $@ if $@; print $wibble;
# include.pl $wibble = 'fred'; # Uncommenting the following line causes a strict error. #$wobble = 'joe';
In reply to Re: strict/do question
by ikegami
in thread strict/do question
by dch
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |