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