Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Absolutely! 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.


... 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: