<?xml version="1.0" encoding="windows-1252"?>
<node id="311792" title="Re: What should be returned in scalar context? (best practice)" created="2003-12-02 19:15:29" updated="2005-07-27 11:06:04">
<type id="11">
note</type>
<author id="182681">
sauoq</author>
<data>
<field name="doctext">
&lt;p&gt;Expectations should be defined by the language, not the programmer. Therefore, I think we should standardize on... well, nothing. Because Perl doesn't. I do, however, believe there is a best practice: unless you have a very good reason, it is best to sidestep the issue entirely and let Perl provide behavior that is already familiar to the programmer.&lt;/p&gt;

&lt;p&gt;It is easy to imagine a function where it makes the most sense to return a reference to an array in scalar context. Likewise, it is easy to imagine one where it makes the most sense to return a formatted string. Neither would make sense as a default though. They are special cases.&lt;/p&gt;

&lt;p&gt;In fact, the only time you should be mucking around with the return value in different contexts &lt;em&gt;is when you have a special case&lt;/em&gt;. (And it just so happens that many, if not most, of Perl's builtins are sensibly treated as special cases.)&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;right&lt;/strong&gt; "default" behavior, in my opinion, is to &lt;code&gt;return @ret;&lt;/code&gt; and let the programmer using your function sort it out. Document that your function returns an array and be done with it. Perl does a fine job of defining expectations for arrays, and your function can rely on those...
&lt;code&gt;
my $nelts   = @array;    # Number of elements. 
my ($first) = @array;    # First element. 
my @whole   = @array;    # Whole thing.

sub some_func { return @array }

my $nelts   = some_func();    # Number of elements.
my ($first) = some_func();    # First element.
my @whole   = some_func();    # Whole thing. 
&lt;/code&gt;
That's about as consistent as it gets in Perl. It'll serve you well too, should you need to globally replace an array with a function call or vice-versa.&lt;/p&gt;

&lt;p&gt;I think that returning a list literal (such as you create with the slice &lt;code&gt;@ret[0 .. $#ret]&lt;/code&gt; in your example) is sorta-semi-okay as an alternative, but probably not nearly as useful. It also isn't very common practice and should probably be much more carefully documented when used. Still, I suppose Perl provides the right expectations insofar as &lt;code&gt;$x = qw( a b c );&lt;/code&gt; and &lt;code&gt;sub f { qw(a b c) }; $x = f();&lt;/code&gt; both set &lt;tt&gt;$x&lt;/tt&gt; equal to &lt;tt&gt;'c'&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;Out of all of your examples, the one that makes me cringe is your "something completely different" example. Why go to the trouble ensuring 
&lt;code&gt;my $i = foo();&lt;/code&gt; is equivalent to &lt;code&gt;my ($i) = foo();&lt;/code&gt; when most perl programmers have already gone to the trouble to learn that they probably aren't? It hardly seems worth the obfuscation. I expect most, if they ran across that function, would spend much longer trying to figure out exactly why you did it that way than they ever would working out a bug caused by calling it in scalar context. I include myself in that.&lt;/p&gt;




&lt;div class="pmsig"&gt;&lt;div class="pmsig-182681"&gt;
&lt;pre&gt;
-sauoq
"My two cents aren't worth a dime.";
&lt;/pre&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
311537</field>
<field name="parent_node">
311537</field>
</data>
</node>
