4 ms·
A modern Perl programmer might write something much more elegant: my $x = 'Apple Carrot Banana'; my @y = $x->split(' ')->sort; if (@y > 2) {
by chromatic 16y ago
A modern Perl programmer might write something much more elegant:
my $x = 'Apple Carrot Banana';
my @y = $x->split(' ')->sort;
if (@y > 2) {
say "we've got ${\@y->size} elements!";
} else {
say "insufficient elements. fail.";
}
- perigrin 16y agoAnd one that doesn't like autobox would say: use 5.12.2; # enables modern features my $x = 'Apple Carrot Banana'; my @y = sort split ' ', $x; say @y > 2 ? "we've got ${\scalar @y} elements!" : 'insufficient elements. fail.'; Not everything needs to be an Object. Not every call needs to be a method call. Some people think better with procedures, some people think better with objects. One of the reasons I personally like Perl is I can use which ever one is appropriate to the problem domain. Edit: forgot the `sort` call.
- colomon 16y agoOr ever-so-slightly lighter Perl 6 version: my $x = 'Apple Carrot Banana'; my @y = $x.words.sort; if @y > 2 { say "we've got {@y.elems} elements!"; } else { say "insufficient elements. fail."; }
- deleted 16y ago[deleted]
- hyperbovine 16y agoIronic that the final rebuttal to "Perl looks like Java" is method chaining using periods :-) But hey, that looks very nice--glad to see Perl finally "got the dots." It's been years since I used Perl, mainly because I was squarely in the mindset of the OP. I'll have to give 6 a look.