In scalar context, a sub returning an array returns its "weight" or number of elements, while a sub returning a list returns its "last" or last element:
sub x {
(1 , 2 , 7);
}
sub z {
my @L = (1 , 2 , 7);
@L
}
my $x = x;
warn $x;
my $z = z;
warn $z;
This can be useful when you have a sub that was written to return a scalar but you need to return more from it: just return a list and have the last element be what it used to return and put all the new things you want to return from it in a list in front of it:
sub x {
($new_val, $new_val2, $old_value_returned_when_called_in_scalar_cont
+ext);
Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
update (broquaint): shortened title (was A list returns its last, an array returns its weight (keeping a subroutine which used to return a scalar useable when you need to return more))
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.