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

Monks,
Why does this code...
if ($cookies{PHPSESSID} ge "") { eval { $session = PHP::Session->new($cookies{PHPSESSID}->value); }; print $query->redirect(-location=>"$redirecturl"."0") if $@; exit 0 if $@; $id = $session->id;

not exit, and throw off this error:
Can't call method "id" on an undefined value at...
Thanks for your help.

Replies are listed 'Best First'.
Re: Eval and Exiting
by tachyon (Chancellor) on Oct 02, 2003 at 04:17 UTC

    Presumably because $@ is no longer true or perhaps $session never gets defined without actually throwing the exception you are trying to catch.... If you do this you save the double test on $@ and it should work just fine.

    $session = ''; eval { $session = PHP::Session->new($cookies{PHPSESSID}->value); }; if ( ! $session or $@ ) { print $query->redirect(-location=>"$redirecturl"."0"); exit 0; } $id = $session->id;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Eval and Exiting
by Zaxo (Archbishop) on Oct 02, 2003 at 05:42 UTC

    If PHP::Session->new fails quietly, just returning undef without dieing, then $@ may not be set. That would match the behavior you see. You didn't say, is the redirect header printed even when exit is not taken?

    After Compline,
    Zaxo

Re: Eval and Exiting
by Roger (Parson) on Oct 02, 2003 at 07:25 UTC
    Other people has pointed out the source of your script problem. I am just adding an observation: have you included the clause use strict; at the beginning of your script? It's very easy to get the scoping of the variables wrong otherwise.
Re: Eval and Exiting
by bart (Canon) on Oct 02, 2003 at 11:14 UTC
    Maybe it's not what you think of as your immediate problem, but that ge definitely looks wrong to me. That test will always return true, even if that value is not set. Most likely
    if ($cookies{PHPSESSID}) {
    would be enough of a test, as it is supposed to be an object, thus true, if set at all.

    I no longer see a reason for the eval wrapper, either, as there isn't much reason I can see for it to fail. Here's my version:

    unless ($cookies{PHPSESSID}) { $query->redirect(-location=>$redirecturl."0"); exit; } $session = PHP::Session->new($cookies{PHPSESSID}->value); $id = $session->id;
    Untested, though.
Re: Eval and Exiting
by Beechbone (Friar) on Oct 02, 2003 at 10:37 UTC
    Maybe ther is another eval() in $query->redirect()? That would of cause overwrite $@.

    I'd recommend to always write an exception catcher like this:

    eval { die 1; }; if (my $e = $@) { # <--- secure... warn "Got the exception '$e'!"; exit(0); }

    Just a side note: It may not even be the redirect() method that contains the other eval(), it could be the DESTROY() of any variable going out of scope between the die() and the end of the eval. This can be avoided this way:

    sub DESTROY { my $self = shift; if ($@) { local $@; return $self->DESTROY(@_); }

    ...but I don't think that's the case here.

    Search, Ask, Know