45+something; something must be a scalar;
sort something;something must be a list;
even if something in first case is some charactors, it will give a single, scalar value;
while in the other will give a list. For example, take the “name”‖ of an
array. In a list context, it gives the list of elements. But in a scalar context, it returns the
number of elements in the array:
@people = qw( fred barney betty );
@sorted = sort @people; # list context: barney, betty, fred
$number = 42 + @people; # scalar context: 42 + 3 gives 45
Even ordinary assignment (to a scalar or a list) causes different contexts:
@list = @people; # a list of three people
$n = @people; # the number 3
Some expressions don’t have a scalar-context value at all. For example, what should
sort return in a scalar context? You wouldn’t need to sort a list to count its elements,
so until someone implements something else, sort in a scalar context always returns
undef.
Another example is reverse. In a list context, it gives a reversed list. In a scalar context,
it returns a reversed string (or reversing the result of concatenating all the strings of a
list, if given one):
@backwards = reverse qw/ yabba dabba doo /;
# gives doo, dabba, yabba
$backwards = reverse qw/ yabba dabba doo /;
# gives oodabbadabbay
At first, it’s not always obvious whether an expression is being used in a scalar or a list
context. But, trust us, it will become second nature for you eventually.
Here are some common contexts to start you off:
$fred = something; # scalar context
@pebbles = something; # list context
($wilma, $betty) = something; # list context
($dino) = something; # still list context!
Don’t be fooled by the one-element list; that last one is a list context, not a scalar one.
The parentheses are significant here, making the fourth of those different than the first.
If you’re assigning to a list (no matter the number of elements), it’s a list context. If
you’re assigning to an array, it’s a list context.
Here are some other expressions you’ve seen, and the contexts they provide. First, some
that provide scalar context to something:
$fred = something;
$fred[3] = something;
123 + something
something + 654
if (something) { ... }
while (something) { ... }
$fred[something] = something;
And here are some that provide a list context:
@fred = something;
($fred, $barney) = something;
($fred) = something;
push @fred, something;
foreach $fred (something) { ... }
sort something
reverse something
print something
Forcing Scalar Context
On occasion, you may need to force scalar context where Perl is expecting a list. In that
case, you can use the fake function scalar. It’s not a true function because it just tells Perl to provide a scalar context:
@rocks = qw( talc quartz jade obsidian );
print "How many rocks do you have?\n";
print "I have ", @rocks, " rocks!\n"; # WRONG, prints names of rocks
print "I have ", scalar @rocks, " rocks!\n"; # Correct, gives a number 4
Oddly enough, there’s no corresponding function to force list context. It turns out you
never need it. Trust us on this, too.
阅读(597) | 评论(0) | 转发(0) |