Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to (Slice 'em and Dice 'em) RE: Arrays are not lists by mwp
in thread Arrays are not lists by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found