Or shorter:

No. This is wrong and not just for the obvious reasons. Your "correction",

sub callback { sub { shift->( @_ ) } }
is just as wrong.

The term closure is not synonymous with "code reference." You are returning a reference to a sub that, when called with a coderef and some arguments, immediately calls that coderef with those arguments. And that isn't very useful at all because the reference you return won't be able to be called with arguments anyway!!! So, your shorter "solution" just brings us full-circle to the original problem and fails to add anything but complexity.

First, have a look at the problem...

sub callback { my $coderef = shift; my @args = @_; sub { $coderef->(@args) } } sub short_but_wrong { sub { shift->(@_) } } sub foo { print "@_\n" } my $mine = callback( \&foo, 1, 2, 3 ); my $yours = short_but_wrong( \&foo, 1, 2, 3 ); print "Mine: "; $mine->(); print "Yours: "; $yours->();
That outputs:
X: 1 2 3 Undefined subroutine &main:: called at cback line 7.
Why? Because the call, $yours->(), didn't supply an argument so the shift resulted in undef which isn't a valid code reference.

The whole point of a closure is that it captures its lexical environment. It binds the code with some variables. Your "shortening" eliminates the lexical environment entirely; there is nothing to capture, so it isn't a closure at all.

Since you need the lexicals, you won't shorten my original (without crunching whitespace and picking shorter identifiers) by much more than this:

sub callback { my ( $coderef, @args ) = @_; sub { $coderef->( @args ) } }

-sauoq
"My two cents aren't worth a dime.";

In reply to Re: Re: Re: Passing arguments to callback routines with named references by sauoq
in thread Passing arguments to callback routines with named references by nysus

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.