It may be a bit late, but I finally understand your problem. It has nothing to do with symbolic reference. Sorry about that confusion. You want to initialize the variable $myscale with the value of $scale (if it is defined) and with '1' otherwise. Normally you would do this:
my $myscale = $scale // 1;

The problem is that $scale may be not defined because it is not declared. The code above causes a compile time error.

The following code incorporates many of replies you have already received. Note that the same code must work in three situations. It is tempting to write the code as a function and call it wherever it is needed. This does not always work correctly because it would be testing declare and define in the scope of the function, not of the call.

use strict; use warnings; use Test::Simple tests => 3; { # $scale is not declared my $myscale = do{ use strict 'vars'; no warnings 'uninitialized'; (do{eval '$scale'; $@} ) ? 1 : (eval '$scale') // 1; }; ok( $myscale == 1, 'not declared'); } my $scale; { # $scale declared, but not defined my $myscale = do{ use strict 'vars'; no warnings 'uninitialized'; (do{eval '$scale'; $@} ) ? 1 : (eval '$scale') // 1; }; ok( $myscale == 1, 'declared, but not defined'); } $scale = 7; { # $scale declared, and defined my $myscale = do{ use strict 'vars'; no warnings 'uninitialized'; (do{eval '$scale'; $@} ) ? 1 : (eval '$scale') // 1; }; ok( $myscale == $scale, 'defined'); }

The logic is confusing. The first eval compiles the string '$scale' recognizing it as a variable. If it is not declared, this is an error under 'no strict vars'. That error is signaled with the system variable $@. Because $@ is the last value in the do-block, it is returned. We only care about its logical value. "True" means that there was an error and the variable is not declared. We return the default value (1). "False" means there was no error, the variable is declared. At this point we would like to do the normal assignment, but our code has to compile in all three cases. We call eval again. We know that there will not be an error. This time, we want the return value (the value of the variable). If that value is undef, we use the default value. The result of all this logic is returned by the outer do and assigned to $myscale.

Bill

In reply to Re: detecting an undefined variable by BillKSmith
in thread detecting an undefined variable by LloydRice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.