in reply to Return array from sub or return empty array

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.

Replies are listed 'Best First'.
Re^2: Return array from sub or return empty array
by GrandFather (Saint) on Jul 04, 2016 at 06:54 UTC

    Mostly for the sake of the OP and anyone else who thinks error return codes are a good idea.

    The old school technique of returning a special value to indicate an error condition is kinda OK, but far better is to throw at the point the error condition is raised. That makes tracking down bugs related to detected errors much easier. The down side with ignoring errors is you may never notice the error condition and the code will quite happily complete - often very fast. That is of course an excellent outcome if you don't care that the result is bogus.

    In cases where you know that something may fail but really don't care, wrap the called code in an eval and ignore the error. At least then you are explicitly ignoring the error rather than maybe just forgetting to test the result.

    Premature optimization is the root of all job security
Re^2: Return array from sub or return empty array (updated)
by gregzartman (Initiate) on Jul 04, 2016 at 18:37 UTC

    Thanks for all the great replies guys. What you have explained confirms what I thought was going on

    Hauke, as you point out the issue is with undef. In some of the places in the API I'm working with an undef scalar can throw perl warnings in the log files. The simple solution is to just do:

     @array2 = a_sub_that_returns_a_list || '';

    My main reason for asking the question was to understand what perl was doing with the || in the context I was using it and you guys have explained it well. Thanks!

      Hi gregzartman,

      The simple solution is to just do:

      @array2 = a_sub_that_returns_a_list || '';

      As AnomalousMonk explained, I doubt that code will do something that is useful for you...?

      Why not just do

      @array2 = a_sub_that_returns_a_list();

      ?

      Regards,
      -- Hauke D