Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: POD for use feature 'declared_refs' wrong

by kcott (Bishop)
on Oct 17, 2021 at 07:28 UTC ( #11137648=note: print w/replies, xml ) Need Help??


in reply to POD for use feature 'declared_refs' wrong

G'day Rolf,

"this POD is either confusing or plain wrong"

I'd say a bit of both.

In the context of the first paragraph of "Declaring a Reference to a Variable", experimental::refaliasing should definitely be replaced with experimental::declared_refs.

That section goes on to say, "It is intended mainly for use in assignments to references ...". It would be appropriate to mention experimental::refaliasing at this point.

"NB: that use feature qw(declared_refs) doesn't seem to make sense without the other feature."

There are instances where you want a declaration without assignment (not in "void" context).

I've certainly used "\my VAR" in this context often enough. This doesn't require any special feature or warnings code.

$ perl -E ' use strict; use warnings; say ref \my $scalar; say ref \my @array; ' SCALAR ARRAY

Off the top of my head, I couldn't think of an application for "my \VAR" in this context; however, just for completeness, here's a contrived example showing that "declared_refs" is required but "refaliasing" is not.

$ perl -E ' use strict; use warnings; say ref my \$scalar; say ref my \@array; ' The experimental declared_refs feature is not enabled at -e line 4. $ perl -E ' use strict; use warnings; use feature "declared_refs"; say ref my \$scalar; say ref my \@array; ' Declaring references is experimental at -e line 5. Declaring references is experimental at -e line 6. SCALAR ARRAY $ perl -E ' use strict; use warnings; use feature "declared_refs"; no warnings "experimental::declared_refs"; say ref my \$scalar; say ref my \@array; ' SCALAR ARRAY

While these features remain experimental, they're rather unwieldy [see Update below] and probably easy to get wrong:

$ perl -E ' use strict; use warnings; use feature qw{refaliasing declared_refs}; no warnings qw{experimental::refaliasing experimental::declared_re +fs}; my ($sc, @ar) = qw{qwert asdfg zxcvb}; my \$scalar = \$sc; my \@array = \@ar; say $scalar; say "@array"; ' qwert asdfg zxcvb

[Note: Perl 5.34 used for all examples.]

Update: Regarding my comment about experimental features being unwieldy; they are, in fact, not as unwieldy as I presented. I had forgotten about the experimental pragma (thanks to ++ikegami for the reminder). The two lines:

use feature qw{refaliasing declared_refs}; no warnings qw{experimental::refaliasing experimental::declared_re +fs};

can be reduced to the much simpler one line:

use experimental qw{refaliasing declared_refs};

— Ken

Replies are listed 'Best First'.
Re^2: POD for use feature 'declared_refs' wrong
by swl (Vicar) on Oct 17, 2021 at 08:24 UTC

    WRT your last point, these features are useful when one wants to avoid repeated dereferences of list items inside loops. Doing so can give a small speed gain but my experience is that they need to avoid a pretty high number of derefs to make a meaningful difference. The same applies to Data::Alias for earlier perl versions.

    A highly contrived (and incomplete) example is below. The main point is that it avoids dereferencing calls such as $path->[$i].

    \my @path = $some_array_ref; my $accumulator; for my $i (0..@$path) { $accumulator += $path[$i]; }
Re^2: POD for use feature 'declared_refs' wrong
by LanX (Sage) on Oct 17, 2021 at 20:24 UTC
    Thanks Ken,

    you are right, it's possible to use my $var; in non-void context, actually I'm regularly prepending warn for debugging.°

    > While these features remain experimental, they're rather unwieldy and probably easy to get wrong:

    They are indeed not nice to activate, I'd probably write a module bundling those four steps to avoid boilerplate.

    But the feature is very nice in many circumstances, Brian d Foy² lists some here:

    it's also very handy when working with aliases, and one doesn't want to litter ones code with cryptic $_[N°]

    D:\tmp\pm>perl -Mfeature=:all -M-warnings sub uc_name { my \$name = \$_[0]; $name = uc $name } $n="ken"; uc_name($n); say $n __END__ KEN

    FWIW Perl4 had an aliasing feature with *type-globs, but this was restricted to package vars.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    °) actually I'm even putting the warn into the line before, like this I'm flexible with (un)commenting it.

    # warn my $x = ...;

    ²) corrected spelling twice (= deux fois ;)

      "FWIW Perl4 had an aliasing feature ..."

      Purely as an historical note, I do recall reading about that in the first Camel book. In perlhist:

      Perl 4 introduced the first Camel book.  Really.  We mostly just
          switched version numbers so the book could refer to 4.000.
      

      So that feature would have existed in Perl3. I don't have any information on which specific Perl version introduced it. I don't believe I used that feature in any version prior to Perl5.

      — Ken

        I'm pretty sure one needed to pass a glob for multiple arrays, because references were missing

          foo(*arr1,*arr2)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

      They are indeed not nice to activate, I'd probably write a module bundling those four steps to avoid boilerplate.

      Which four steps? Enabling the two features and disabling the two warnings? It already exists.

      use experimental qw( refaliasing declared_refs );

        ++ Thanks for the reminder about the experimental pragma. I've added an update to my post.

        — Ken

Re^2: POD for use feature 'declared_refs' wrong
by ikegami (Patriarch) on Oct 18, 2021 at 13:29 UTC
    [deleted]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11137648]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2022-01-28 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?