3 ms·
You'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
by qntm 15y ago
You'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.