in reply to A useful use of goto
First and foremost, use a Safe. For example:
When you are done, you can simply throw away the Safe object, and everything is totally cleaned up.use Safe; my $config = Safe->new; *{ $config->varglob($_) } = $handler{$_} for keys %handler; $ok = $config->reval( $cfg_str ); # Or: $ok = $config->rdo( $config_source ); $err = $@;
Secondly, (if using regular string eval, and not a Safe) you could have added the following to your $cfg_str:
That would have cause the local to be undone the moment the string eval finished running.join ";\n", map "local *$_ = \$handler{$_}", keys %handler;
Thirdly, you could have used recursion:
But this is silly, when a Safe is so much simpler.local *handler_localizer = sub { if( my $name = each %handler ) { no strict 'refs'; local *{$name} = $handler{$name}; handler_localizer(); } else { $ok = eval $cfg_str; $err = $@; } };
|
|---|