in reply to Re: detecting an undefined variable
in thread detecting an undefined variable
Here a generic - albeit slower - solution for definedness
use strict; use warnings; use Data::Dump qw/pp dd/; #my $scale =42; #our $scale =42; sub var { eval $_[0];# or die $@; } my $myscale = var('$scale') // 1; pp $myscale;
and inspecting the errorstring in $@ can help checking for existence instead.
Global symbol "$scale" requires explicit package name (did you forget to declare "my $scale"?) at (eval 1) line 1.
use strict; use warnings; #my $scale =42; #our $scale =42; my $myscale = eval('$scale') // 1; print $myscale;
but I prefer encapsulating such magic to be able to capture side effects like polluting $@ or potential injections by tainted input.
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice
|
|---|