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.
Alakaboo
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|