On Thu, Apr 8, 2010 at 07:45, Bernhard Graf <[email protected]> wrote:
> Here are two ways of doing the same thing with txn_do():
>
>  my $author = $schema->resultset('Author');
>  my @titles = qw/Night Day It/;
>
>  # 1: giving a closure to txn_do()
>  $schema->txn_do(
>    sub {
>      $author->create_related('books', {
>        title => $_
>      }) for @titles;
>    };
>  );
>
>  # 2: giving a code ref together with all external variables
>  $rs = $schema->txn_do(
>    sub {
>      my ($author, $titles) = @_;
>      $author->create_related('books', {
>        title => $_
>      }) for @$titles;
>    }, $author, \...@titles
>  );
>
> Does it make any difference?
> Is there a recommended way of doing it (better use or avoid closure)?

First off, there is almost never a reason to avoid closures. Closures
aren't the boogeyman. Not even for parallelizing.

The main reason I can see to use the second form is so that you can
have a named subroutine. So, instead of:

$schema->txn_do( sub {
    my ($x, $y) = @_;
    ...
}, $some_x, $some_y );

You would do:

sub do_x_with_y {
    my ($x, $y) = @_;
    ...
}
# Elsewhere...
$schema->txn_do( \&do_x_with_y, $some_x, $some_y );

Rob

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/[email protected]

Reply via email to