Hi gregzartman,

You've already got some good replies (it's all about Perl's concept of context); just to provide a different angle: what do you want the variables to hold?

When you write my $foo;, the variable initially holds undef, and it's understandable and common to write something like $foo = func() // ''; (that is, set $foo to the scalar return value of func(), but when that return value is undef, set $foo to the empty string instead). Whether it's good practice depends on the situation; the point of this might be to prevent later warnings about $foo being undefined when interpolated into a string, (Update 2:) or to detect whether $foo has been assigned to or not. Note that the // defined-or operator became available in Perl 5.10.

However, when you write my @bar;, that variable doesn't contain undef, it simply contains zero values. Also, a function which returns a list or array would normally not return undef to mean "no results", it would return an empty list*. So there is no need to do something like "assign an empty value to an array if the function doesn't return anything", since the array will simply continue to contain zero values, and there are no warnings about that.

Now, a different question might be: what if a function returning zero values were to represent an error condition that you wanted to detect? To do that, I would suggest to first fetch the return value of the function into an array, and then test it, for example: @bar = func2(); if (!@bar) { warn "func2 returned nothing" }.

* Update 2: Some really interesting discussions come up when you search for "return undef", for example this thread seems to be very relevant, also this one.

Hope this helps,
-- Hauke D

Updated: Typo fix and minor rewordings.


In reply to Re: Return array from sub or return empty array (updated) by haukex
in thread Return array from sub or return empty array by gregzartman

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.