It shouldn't be
@_[0] because that's the arrayslice consisting of the 0th element. If you turn warnings on, it will tell you about this. Arrayslicing is really cool. Try the following snippet:
my @x = 0 .. 9;
print "@x[2..4]\n";
print "@x[3,5,7]\n";
my @y = 4..6;
print "@x[@y]\n";
@x[7..9] = @y;
print "@x\n";
The reason why you don't want to use the arrayslice form when you want to extract a single element is, while it has been special-cased to work in the case of my $x = @x[2]; (it should assign 1 and not 2), it won't work in this case:
sub foo {
wantarray ? 3 : 5;
}
my @x;
@x[2] = foo();
my $x = foo();
print "$x[2] <-> $x\n";
@x[2] is list context, not scalar context.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
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.