turkanis has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there ever a difference in semantics between

sub foo {
    ....
    my $value = <expression>;
    return $value;
}

and

sub foo {
    ....
    return <expression>;
}

?

I ask this because I am experiencing a negative interaction between the Komodo remote debugger and the CPAN module DBIx::transaction. When debugging code that commits a transaction, I get an error like this:

panic: attempt to copy freed scalar 350d010 to 15c7b68 at
/usr/share/komodo/perllib/perl5db.pl line 4341, <STDIN> line 1.

Strangely, I have found that I can fix the error by changing the definition of close_transaction in DBIx/Transaction/db.pm from

sub close_transaction {
    my $self = shift;
    my $method = shift;
    my $code = DBI::db->can($method);
    $self->{private_DBIx_Transaction_Level} = 0;
    $self->clear_transaction_error;
    $self->transaction_trace($method);
    my $rv = $code->($self, @_);
    return $rv;
}

to

sub close_transaction {
    my $self = shift;
    my $method = shift;
    my $code = DBI::db->can($method);
    $self->{private_DBIx_Transaction_Level} = 0;
    $self->clear_transaction_error;
    $self->transaction_trace($method);
    return $code->($self, @_);
}

to me, it looks like my change should have no effect, and yet it eliminates the panic. What's going on here? Could it be a bug in the Komodo remote debugging libraries, or in the Perl implementation, or in DBIx::Transaction?

Thanks
  • Comment on Attempt to copy freed scalar and return value semantics

Replies are listed 'Best First'.
Re: Attempt to copy freed scalar and return value semantics
by stephen (Priest) on May 24, 2013 at 17:39 UTC

    The most obvious possibility is context. If you assign the method to $rv, it forces scalar context, while if you return it directly, it has whatever context the method was called in, which could be list or void context. We'd have to see the calling statement to be sure.

    Update: See http://community.activestate.com/node/8760.

    stephen

Re: Attempt to copy freed scalar and return value semantics
by Laurent_R (Canon) on May 24, 2013 at 17:52 UTC

    Probably the context.

    Maybe you could try:

    return scalar $code->($self, @_);

    and see you you still get the panic.

Re: Attempt to copy freed scalar and return value semantics
by flexvault (Monsignor) on May 24, 2013 at 18:16 UTC

    turkanis,

    I had something similar happen with Perl 5.10.x, and the problem went away in Perl 5.12.2. I know 5.10 had some problems, so maybe add a newer version of Perl to your system and try that.

    Don't upgrade your system Perl since most distributions add to and modify Perl for their own utilities.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      I'm glad I'm not the only one who has run into a similar problem. Unfortunately, I can't use a newer Perl; I'm trying to find a safe patch and report the bug, if any, to the correct person.