First of all, congratulations for the good sense to use strict and warnings.

Let's walk through your script's run:

  1. $test is declared and defined with the string of true.
  2. $lib is declared but not defined (meaning, no value is assigned to initialize it. When a variable is not defined, in Perl, loosely speaking, it contains undef.
  3. GetOptions is invoked, and detects that the test flag appeared on the command line with a value of "true". The libFile flag was NOT provided on the command line, so no value is assigned to $lib
  4. The first conditional detects that $test contains true, so we move into the second conditional.
  5. The second conditional attempts to dereference $lib. That is, it attempts to access the array reference contained in $lib. But wait, $lib doesn't contain an array reference. It doesn't contain anything aside from undef.
  6. Since Perl cannot dereference an entity that does not contain a reference, it throws an error and exits, unable to comply with the request. It simply will not pull a rabbit out of a hat (particularly when using strict, as you are correctly doing).

Where you may be getting confused is over the concept of Perl's autovivification. This does work:

use strict; use warnings; my $foo; push @$foo, 'bar';

But this does not work:

use strict; use warnings; my $foo; my @bar = @$foo;

Dave


In reply to Re: Can't use an undefined value as an ARRAY reference at ./test.pl line 14. by davido
in thread Can't use an undefined value as an ARRAY reference at ./test.pl line 14. 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.