in reply to Re^2: Reading session data
in thread Reading session data

Is there any situation where the 'old' syntax is preferable or even required?

It is not required for hashrefs like this but obviously you can't use the normal arrow to dereference things where there are no components. So, if you have a scalar reference the only ways to dereference that are with a $$ or using postfix dereference. In such situations I much prefer the $$ for clarity. In fact I've just tried this example to illustrate and it won't even compile with the postfix deref line uncommented (error is $* is no longer supported as of Perl 5.30).

#!/usr/bin/env perl use strict; use warnings; use feature 'say'; my $foo = 'some string'; my $ref = \$foo; say "Deref with \$\$: $$ref"; # Uncomment next line for compilation error - why? #say "Postfix deref: $ref->$*";

Perhaps someone who likes/uses postfix deref can explain where the compilation error comes from and how to fix it. As for me, I'm quite happy with $$ in these situations.

See also I use postfix dereferencing ... for various comments on how wonderful/terrible postfix dereferencing is.


🦛

Replies are listed 'Best First'.
Re^4: Reading session data
by kcott (Archbishop) on Jul 01, 2023 at 13:25 UTC

    G'day hippo,

    "Perhaps someone who likes/uses postfix deref can explain where the compilation error comes from and how to fix it."

    The problem revolves around the version of Perl being used, the feature bundle loaded, and additional features requested. Here's some examples:

    v5.36 with full feature bundle
    $ perl -E 'say $^V; my $x = "xyz"; my $r = \$x; say "PFDR: $r->$*";' v5.36.0 PFDR: xyz
    v5.36 with no feature bundle
    $ perl -e 'print $^V; my $x = "xyz"; my $r = \$x; print "PFDR: $r->$*" +;' $* is no longer supported as of Perl 5.30 at -e line 1.
    v5.22 with default :5.22 feature bundle
    $ perl -e 'use v5.22; say $^V; my $x = "xyz"; my $r = \$x; say "PFDR: +$r->$*";' $* is no longer supported as of Perl 5.30 at -e line 1.
    v5.22 with default :5.22 feature bundle plus "postderef" feature
    $ perl -e 'use v5.22; use feature "postderef"; say $^V; my $x = "xyz"; + my $r = \$x; say "PFDR: $r->$*";' $* is no longer supported as of Perl 5.30 at -e line 1.
    v5.22 with default :5.22 feature bundle plus "postderef" feature (dereferencing outside of quotes)
    $ perl -e 'use v5.22; use feature "postderef"; say $^V; my $x = "xyz"; + my $r = \$x; my $y = $r->$*; say "PFDR: $y";' v5.36.0 PFDR: xyz
    v5.22 with default :5.22 feature bundle plus "postderef_qq" feature
    $ perl -e 'use v5.22; use feature "postderef_qq"; say $^V; my $x = "xy +z"; my $r = \$x; say "PFDR: $r->$*";' v5.36.0 PFDR: xyz
    v5.24 with default :5.24 feature bundle
    $ perl -e 'use v5.24; say $^V; my $x = "xyz"; my $r = \$x; say "PFDR: +$r->$*";' v5.36.0 PFDR: xyz

    I couldn't see it specifically documented anywhere but 'use feature "postderef_qq";' seems to imply 'use feature "postderef";'. This is borne out by my own tests (in particular, see the "v5.22" tests above) and documentation of feature bundles that include postderef_qq do not also include postderef.
    [I have a niggling feeling that I did see such documentation a long time ago. If someone locates it, please share. Thankyou.]

    Some relevant documentation links (in no particular order):

    — Ken

      Thanks for the detailed analysis, kcott. I am using 5.34 so adding in use v5.34; has allowed the construct to work for me. use feature 'postderef_qq'; also works.

      However, the version-dependent nature of all of this combined with the paucity of documentation and the arcane error message received has only served to reinforce my decision to avoid postfix dereferencing in general.


      🦛