in reply to Arrays are not lists

sub ret_list { return $_[0..$#_]; }

I'd like to give you a good trouting, but I'm not certain if you did this on purpose or not. :-)

The "proper" way to take a slice is as follows:

sub ret_slice { return @_[0..$#_]; }

This will return the desired result. I think a better example to illustrate your point (lists vs. arrays) would have been this:

# the data our @zot = qw(apples Ford perl Jennifer); # the output print "Func Context RetVal \n", "---- ------- ------ \n"; { # our function my @list = &ret_std( @zot ); my $scalar = &ret_std( @zot ); print "Std LIST @{list} \n", # prints 'apples Ford perl' "Std SCALAR ${scalar} \n\n"; # prints 3 } { # a poorly-written function my @list = &ret_bad( @zot ); my $scalar = &ret_bad( @zot ); print "Bad LIST @{list} \n", # prints 'apples Ford perl' "Bad SCALAR ${scalar} \n\n"; # prints 'perl' } { # a better function my @list = &ret_good( @zot ); my $scalar = &ret_good( @zot ); print "Good LIST @{list} \n", # prints 'apples Ford perl' "Good SCALAR ${scalar} \n\n"; # prints 'apples Ford perl' } # the functions # returns full list, or number of elements sub ret_std { my @foo = @_[0..2]; return @foo; } # returns a list each time, but how long, and which parts?? sub ret_bad { return @_[0..2]; } # the "proper" function (from perldoc -f wantarray) # returns the full list, as a space-delimited scalar or list sub ret_good { my @bar = @_[0..2]; return (wantarray()) ? @bar : "@bar"; }

I apologize for the length and relative messiness of the code (this would be a good place to use write formats) but I hope I get my point across. Essentially, I follow what you're saying and you raise several crucial issues. Most importantly, PAY ATTENTION to A) where the return value(s) from your function are being used, and B) how your function is delivering those return values. Is it clear, or at least documented? &ret_bad() in particular scares me, I would hate to have a library full of functions like that. "Huh, I got the LAST element of the list? WTF?"

I hope I don't come across as being snide. I understand what you are trying to say and it was definitely a very thoughtful post, and should serve as a warning to us all. Thank you. :-) Patience, meditation, and good use of the scalar function will see us through.


Replies are listed 'Best First'.
returning lists and scalars
by merlyn (Sage) on Aug 05, 2000 at 17:52 UTC
    Another approach is to be specific. If you want to return a list of items, and your invocation makes no sense in a scalar context, say so:
    sub this_returns_a_list_only { wantarray or die "DURN IT, USE ME IN A LIST CONTEXT!"; ... ... }
    If you code like that, you can safely add all the return expressions you want, knowing they will be always invoked in a list context.

    There is no "proper" relationship between what a function returns in a list context and what it returns in a scalar context. There's only the relationship that makes sense for the domain in which the function returns.

    For example, it makes perfect sense to me that sortalways returns undef in a scalar context. What one single value would make sense to be returned otherwise? And that wonderful getpwnam, which returns a 9-element list in a list context, or just the most useful part of that (the second element, the user id number) in a scalar context.

    And finally, the localtime,gmtime twins. Very cool that the scalar context definition is completely unrelated to the list context definition.

    So, perhaps you can stop thinking of "what one expression makes sense to return" and think instead of "what do I want this subroutine to return in a list context vs. a scalar context?" and write your code appropriately. That's why wantarray is in there!

    -- Randal L. Schwartz, Perl hacker

RE: (Slice 'em and Dice 'em) RE: Arrays are not lists
by tilly (Archbishop) on Aug 05, 2000 at 19:20 UTC
    D'oh. Thanks for pointing out my obvious typo. That is what I get for posting late at night without testing my code.

    What I consider the most important point here is not that lists and arrays differ. Rather it is that there are a lot of ways in which scalar and array context can have unexpected behaviour, and there are a lot of hidden mines awaiting the person who does not think about which is which.

    Being aware of the context and making a point of consistently exploring it will teach you a lot of nuances of code.

    BTW I am no longer bitten by the difference between lists and arrays. There was a period when I realized that Perl really is list-oriented and I tried to make my code be list-oriented and I got bitten during the transitition...