As already mentioned in one reply, it can simplify your life to have your config file set up a hash instead, and then use exists($config_vars{foo}) to check for foo.

If, for some reason, you can't change how the config file returns things, check if a config var foo is defined with defined($foo) which will return a true or false value.

That just checks to see if the variable has a defined value. The literal answer to your question to check if a variable exists is to check *foo{SCALAR} which will return true (actually, a reference to the variable) if a scalar variable $foo exists. This isn't of much practical use, since code like if (*foo{SCALAR}) { print "foo is $foo"} will always do the print, since the compiler will create the variable before runtime when it sees the $foo. The other problem is that if perl sees any kind of foo variable (hash, array, sub, format, glob) it will create that kind *and* a bonus scalar $foo. So stick with defined($foo).


In reply to Re: Check to see if a variable exists by ysth
in thread Check to see if a variable exists by Anonymous Monk

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.