in reply to Re^2: detecting an undefined variable
in thread detecting an undefined variable

Now I will admit a little secret. The second method in your original post would work under
no strict 'vars'; no warnings 'once';

Without strict, if a variable '$state' is not already declared, perl will automatically declare a package variable '$state'. Of course this variable would not be defined so perl would assign your '1' to $mystate. This could lead to a very subtle bug if in the future you ever tried to declare a lexical variable '$state'. It is legal to have two variables with the same name. Hard to tell which one perl will think you mean.

Bill

Replies are listed 'Best First'.
Re^4: detecting an undefined variable
by AnomalousMonk (Archbishop) on Sep 25, 2019 at 21:11 UTC
    The second method in your original post would work under

        no strict 'vars';
        no warnings 'once';

    Indeed, something similar to this will quietly work even with all strictures and warnings enabled if fully-qualified package-global names are used (e.g., $main::scale or its shorthand version $::scale):

    c:\@Work\Perl\monks>perl -MData::Dump -le "use strict; use warnings; ;; my $scale = $::scale // 42; print $scale; dd $::scale; " 42 undef
    If fully-qualified names are used, Perl is perfectly clear about who's who and what's what although as you say, it can be hard for the programmer to understand the prevailing state of affairs. (The trick of course is that $scale is not the same name as either $main::scale or $::scale.)


    Give a man a fish:  <%-{-{-{-<