in reply to Re: Merge 2 lines in 1 die statement
in thread Die statement with text & formatting of the user

if ($select->rows) { print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script +have to die" )); exit(0); }
In this snippet exit doesnt end the perl cgi script as i expect it to do but instead it exits the if structure.
print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script hav +e to die" )), exit(0) if $select->rows;
In this one which i preferes to use the script does die in general because it isnt in a block structure to just exit but infortunately it dies without first printing what it has to print....

Wouldnt it be better to just somethign like this work?
die(h1( {class=>'cyan'}, "Error occured $row->{username}! Script have +to die" ));

Replies are listed 'Best First'.
Re^3: Merge 2 lines in 1 die statement
by shmem (Chancellor) on Apr 28, 2007 at 09:30 UTC
    In this snippet exit doesnt end the perl cgi script as i expect it to do but instead it exits the if structure.

    There's no such thing as exiting an if structure with exit. You can exit a loop with last, but exit exits the program.

    if(1) { print "1 seems to be true\n"; exit 0; } print "After exit..\n"; __END__ 1 seems to be true

    The After exit. is not printed.

    Wouldnt it be better to just somethign like this work?

    No, because die throws its message out STDERR, which is not what you want. Read the docs.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script hav +e to die" )), exit(0) if $select->rows;
      Here? why the program exits without printing first?

        That's because you are passing a list to print. For this list to be printed, its elements must be evaluated first. There's a exit call in that list, so that is done, and the print never gets executed. Consider
        print "no rows\n", exit unless $rows; print "we seem to have rows.\n"; __END__

        and

        print "no rows\n" and exit unless $rows; print "we seem to have rows.\n"; __END__ no rows

        update: as ikegami point out below, you are not passing a list to print - since you treat print as a function, and are passing in its arguments in parens. With the comma operator, you are still evalutaing a list, and in that list exit gets evaluated..

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}