Skip to main content
Post Made Community Wiki by ThiefMaster
Source Link
user2348
user2348

In Perl, scalar context vs. list context can be tricky. It's got some good points that make certain operations convenient, but occasionally you run into something terrible, like completely changing the meaning of an operator (potentially from a significant distance away in the code).

sub foo { (1..5) }
my @list = foo();           # (1,2,3,4,5)
my $length = scalar @list;  # 5. the length of the list.
my $length2 = scalar foo(); # '' (the empty string. because it's false)

That's just not right.

(It arises from trying to make something that acts sort of like the regular range operator, so you can say something in a loop like next if /start_regex/ .. /end_regex/).