3 ms·
Perl is really powerful, and with power comes responsibility. Reference handling is an area that needs great care, as you can easily make subtle mistakes. Codin
by kgtm 15y ago
Perl is really powerful, and with power comes responsibility. Reference handling is an area that needs great care, as you can easily make subtle mistakes. Coding standards are important in a code base, often there are many ways to accomplish the same thing. Usually some forms are cleaner than others and can be enforced. An example regarding dereferencing:
use v5.10;
use strict;
use warnings;
my $ref = ['one', 2, 3];
say $ref;
foreach (@{$ref}) {
say;
}
# These are the same. The first form is much cleaner.
# It also makes more sense semanticaly
say @{$ref}->[0];
say @{$ref}[0];
say $ref->[0];
- qntm 15y agoYou're proving your own point about the subtlety of reference handling and the important of coding standards in Perl. Was that intentional? say @{$ref}->[0]; This raises a deprecation warning for me. say @{$ref}[0]; This is technically correct, as the leading sigil "@" indicates you are returning an array, but since you're only returning one element in that array, this should probably be "${$ref}[0]" which returns a scalar only. say $ref->[0]; This the only form which I would consider correct or "clean". This example proves nothing about Perl's power, though. The fact that difficult and fiddly reference handling shenanigans are necessary to handle something as trivial as nested arrays and hashes shows quite the opposite.
- kgtm 15y agoAbsolutely! Running the code under Strawberry (5.12.3) produces no warnings and "works" as one would expect. Can it be any easier to shoot yourself in the foot? Regardless, Perl has a special place in my heart.
- chromatic 15y ago... since you're only returning one element in that array, this should probably be "${$ref}[0]" which returns a scalar only. It's perfectly fine to use the array sigil, in which case it's a one-element list instead of a scalar. That's useful sometimes.
- danssig 15y ago>Perl is really powerful, Compared to what? C? In what way is it more powerful than Ruby, Python, Ocaml, Haskell, etc.? There are things perl has over e.g. python (lambdas) but there are at least as many things those languages have over perl (e.g. python generators built in) and all of those languages are easier to use. Look at the code you wrote there! I assume "use v5.10" is actually a forward compatibility declaration instead of a backward one, right? Really embarrassing if so.
- sciurus 15y agoIt's the equivalent of "use feature qw(switch say state)". "use feature" allows "new syntactic constructs, or new semantic meanings to older constructs" to be enabled in the current scope. http://search.cpan.org/~flora/perl/lib/feature.pm http://search.cpan.org/~flora/perl/lib/feature.pm
- Mithaldu 15y agoWhen Larry left for Perl 6 everyone figured that would be it for Perl 5 and then a few years later when Perl 6 kept not being viable for more than toy use the p5p was forced to figure out ways of extending Perl 5 without breaking old code. That was one of the ideas. This is what is more current planning in that regard: http://www.youtube.com/watch?v=yJss-l2XuV8 http://www.youtube.com/watch?v=yJss-l2XuV8