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!)

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to Half-serious quest for prefix anonymous refs taking by blazar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.