[Dbix-class] Q: txn_do() w/ or w/o closure

Bernhard Graf dbic4 at augensalat.de
Thu Apr 8 11:45:04 GMT 2010


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

Bernhard Graf



More information about the DBIx-Class mailing list