Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

So, in most cases, the downstream code wants a scalar to either be some value or an empty string. The downstream code wants an array to either be some non-empty array or an empty array.

Now, maybe, I have obtained some subroutines or methods that return scalars and other subroutines or methods that return arrays. I want to use those subroutines or methods without modifying them because, if I change them, I might make a mistake and then they won’t do what I thought they were supposed to do (and everyone else on the project thought they would do).

So, wouldn’t it make sense to wrap those subroutines or methods in a subroutine that will make sure that the downstream code gets what it expects?

sub somefunc_for_scalars { my $answer = some_class->some_method(); return defined($answer) ? $answer : ''; }

Digression: If the reader wants to use Perl's // operator in the return statement, go ahead. However, it may confuse other programmers. See http://stackoverflow.com/questions/23873379/what-does-the-double-forward-slash-mean-here for a nice explanation of Perl's // operator, which is available with Perl 5.10 or later.

sub somefunc_for_arrays { my @answer = get_array2(); return @answer; }

However, somefunc_for_arrays is superfluous if it is correct for get_array2() to return some non-empty array or an empty array.

In some rare instances, the downstream code does not want an empty array. So, code somefunc_for_arrays thusly:

sub somefunc_for_arrays { my @answer = get_array2(); if (!(scalar @answer)) { die 'get_array2() returned an empty array +.' }. return @answer; }

In reply to Re: Return array from sub or return empty array by Cow1337killr
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found