You often want to make a list into an anonymous arrayref. So you just do: [EXPR], where EXPR is the expression returning the list. But sometimes it can be quite complex: while I feel perfectly comfortable with chaining several functions in very long statements, as long as I indent adequatly of course, I would feel guilty putting a lonely open square paren at one pont and a lonely closing one say, three lines later.
For example in the last example of a recent post of mine I had two subs returning a list, and then compared them by taking two anonymous arrayrefs. (Via Test::More's is_deeply.) But if for any reason I wanted them to return arrayrefs directly, I would rewrite them as:
sub dargv { local *ARGV; @ARGV = @_; [<>]; } sub dopen { my @t = map { open my $fh, '<', $_ or warn "Can't open `$_': $!\n"; <$fh>; } @_; \@t; }
respectively. Not so bad, certainly. And somebody would argue that the latter is clearer because it's "more explicit." Yet it somehow bothers me to have to assign to a temporary variable only to take a reference to it, and sometimes I would like a prefix anonymous reference maker.
Of course, I may just roll my own helper sub:
sub mkaref { [@_] }
Thinking about, I may write similar subs for other kinds of refs:
# the most reasonable thing for this strange beast, IMHO sub mksref { @_ == 1 ? do { my $x=$_[0]; \$x } : "@_"; # users can play with (a localized) $" } sub mkhref { my %x; @x{@_}=(); \%x; } sub mkcref { my @x = @_; sub { @x }; # a closure }
But then this feels all so clumsy, and I'd like an idiomatic, buitin way to do the same. Granted: I'm not claiming that we really need such a thing, but anyway I'd like to know -for fun- which syntax you would think could be appropriate for such beasts.
Personally, since the referencing operator \ clearly speaks "reference" and the sigils unambiguously identify the kind of reference, I would use \$, \a, \% and \&, followed by... something... except that any symbol I can think of, even if separated by whitespace, would make for some special variable. So, end of story. But I'd like to hear your thoughts and ideas. (Except if you only want to say that we don't really need this, because I know!)
In reply to Half-serious quest for prefix anonymous refs taking by blazar
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |