Truth to tell, my personal opinion is that the entire idea of context in Perl is an interesting experiment in language design that other languages have wisely decided not to borrow...

Im on the other side on this one for sure. Context is one my favorite things in perl. Sure occassionally I get betting by it one way or another, but overall I think its a great thing.

Therefore I am interested in which of the above (or other variations of your choice) people think is a good default behaviour to standardize on..and more importantly why.

Im definately in the don't standardize camp. I would say that part of the artistry of perl is choosing things like this. Its like naming functions. To borrow a chess metaphor, a newbie perl programmer doesnt think about names or context at all. An experienced one deliberates on such things for ages before deciding on the right behaviour, and a grandmaster just picks a name and behaviour and it makes sense. The whole point here is convenience. Does the code read well in its various usages, is the behaviour intuitive and easy to assimilate. Or does it read like gobbleygook and trip you up all the time?

Only experience, and understanding of the usage context (using context at the human level and not perl level) will determine what behaviour is sensible.

I will say however that the whole

return wantarray ? @array : \@ref

idea doesnt seem like a clean solution. I used to do it a fair bit but I found it trips you up too often. Instead I almost always simply return the reference for things like this. In other scenarios I often make the results vary based on wantarrays status. For instance ive done stuff like this on many an occassion:

sub get_rec { my $self=shift; print(join ",",@$self),return unless defined wantarray; return wantarray ? @$self : join(",",@$self); }

Anyway, i would say that context is a bit like a rich cake. A little bit goes a long way, and too much is just awful. But none is even worse. :-)


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re: What should be returned in scalar context? by demerphq
in thread What should be returned in scalar context? by tilly

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.