Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Context aware functions - best practices?

by LAI (Hermit)
on Jan 14, 2003 at 18:46 UTC ( [id://226921]=note: print w/replies, xml ) Need Help??


in reply to Context aware functions - best practices?

Hm. My first reaction is to consider the possibility of:

my $x = foo(1,2,3,5);

This should of course be a scalar result, and not equal to scalar (@x = foo(1,2,3,5)). I'd feel uncomfortable omitting the results from the 2,3 and 5, so I'd probably do one of the following:

return wantarray ? @arr : "@arr"; return wantarray ? @arr : \@arr;

depending on use, 'yadda'x3. Perhaps, if it really suits the particular sub, you might want to return a join, but I'd personally be leery of that.


LAI
:eof

Replies are listed 'Best First'.
Re^2: Context aware functions - best practices?
by Aristotle (Chancellor) on Jan 14, 2003 at 18:59 UTC
    You are aware that scalar (@x = foo(1,2,3,5)); will evaluate to the number of elements returned by foo rather than return the last one of them, right? :) And no, I even considered returning an array ref. (But not a concatenation, yuck - if the caller wants unstructured data he can flatten the array himself, but he can't well unflatten a string.) I'm not going to ask this particular function for two return values when I'm only interested in one of them though. I just don't want to have to remember and to type the parens in every instance I only ask for one return value, because that will be pretty a common case. CGI::param works the same way with good reason, f.ex.

    Makeshifts last the longest.

      You are aware that
      scalar (@x = foo(1,2,3,5));
      will evaluate to the number of elements returned by foo rather than return the last one of them, right? :)

      Yes, of course. I wrote that in reference to my $x = foo(1); setting $x to 1 :oP

      I agree that it would be a rare case when the flattened array would be an acceptable return value, but I still don't like the idea of throwing away data. Oh, well. The only alternative I can think of is the hideous

      return wantarray ? @arr : $#arr ? \@arr : $arr[0];

      which borders on the obfuscated. I wouldn't use something like that if I were you. Of course I'm me, and love using lines like that, but I digress...

      Zaxo's suggestion of using $arr[-1] makes sense too, especially if the sub is doing something cumulative with its args. Imagine a sum() function that keeps track of all the intermediate sums, or whatever.


      LAI
      :eof

        Ugh, that's the worst of both worlds, esp if I didn't know the number of return values beforehand (I do in the situation at hand). In that case every invocation in scalar context will have to be tested with ref.. if it at least consistently returned an arrayref I wouldn't have to think about it.

        As I said, what I want is the same convenience as CGI offers with my $foo = $cgi->param(''); vs my ($foo) = $cgi->param(''); - you get the same result either way. Nothing else.

        If you don't like the idea of throwing away results, consider my $foo = (bar(), baz(), qux());. I'm asking for pretty much the same semantics.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://226921]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found