Let me expand a bit:

sub undefined { return; } sub empty { my @array= (); return @array; } my @undefd; my @empty= (); my @dedefd= (1,2,3); @dedefd= undefined(); my @emptied= (1,2,3); @emptied= empty(); my @undef2= undefined(); my @empty2= empty(); print "\@undefd is defined\n" if defined @undefd; print "\@empty is defined\n" if defined @empty; print "\@dedefd is defined\n" if defined @dedefd; print "\@emptied is defined\n" if defined @emptied; print "\@undef2 is defined\n" if defined @undef2; print "\@empty2 is defined\n" if defined @empty2;
yields just:
@dedefd is defined @emptied is defined

You can have an undefined array but there is no such things as an undefined list value. So you can't pass around arrays by value and successfully worry about whether they are defined or not. To make an array undefined, you have to write undef @array and to have a subroutine do that would require that a reference to the array be passed in.

So you really shouldn't try to have two types of empty arrays/hashes. Unlike strings where there is the empty string and undef, Perl doesn't provide a special value for undefined arrays/hashes and doesn't make it easy for you to provide one of your own.

If I need to distinguish an empty list from an error, then I'd probably return a reference to an empty array for the first case and a "false" value for the second.

        - tye (but my friends call me "Tye")

In reply to (tye)Re2: Replacement for defined(%)/defined(@)? by tye
in thread Replacement for defined(%)/defined(@)? by Masem

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.