in reply to Re^2: Strange behavior: passing $1 to AUTOLOAD
in thread Strange behavior: passing $1 to AUTOLOAD

As Elian says, make copies when passing in ephemeral values such as $1, $!, $@; one easy way to do that is to stringify, as long as that doesn't lose you information:

$person->name("$1");

If just the string is not enough (eg to pass the dualvar $!), I think the easiest way is to make a copy:

my $error = $!; $log->error($error);

Hugo

Replies are listed 'Best First'.
Re^4: Strange behavior: passing $1 to AUTOLOAD
by revdiablo (Prior) on Sep 22, 2004 at 23:35 UTC

    Aha! But what if you don't want to create a lexical variable that lives any longer than necessary? Well, you could use a bare block:

    { my $error = $!; $log->error($error) }

    But then what if you want the return value from the method? Well, you could use do:

    my $return = do { my $error = $!; $log->error($error) };

    That's probably the sane way to handle it. But what if you're not sane? Then we can have some fun!

    my $return = $log->error(${ \(my $e = $!) });

    Update: another fun way that never even makes the lexical variable:

    my $return = $log->error(@{[$!]});