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

print h1( {class=>'cyan'}, "Error occured $row->{username}! Script hav +e to die" ) if( $select->rows ); exit 0 if( $select->rows );
Is there a way to produce the same effect with same formatting by just using a die statement?!

Replies are listed 'Best First'.
Re: Merge 2 lines in 1 die statement
by rhesa (Vicar) on Apr 28, 2007 at 00:56 UTC
    I don't think so, since the output from a die call normally goes to STDERR. Assuming you're working on a CGI script, the text would go to the error log, and not to the browser.

    Try this instead:

    if( $select->rows ) { print h1( {class=>'cyan'}, "Error occured $row->{username}! Script + have to die" ); exit 0; }
Re: Merge 2 lines in 1 die statement
by ikegami (Patriarch) on Apr 28, 2007 at 02:24 UTC

    I would look as follows with a die statement:

    do { local *STDERR = *STDOUT; $! = 0; die(h1( {class=>'cyan'}, "Error occured $row->{username}! Script ha +ve to die" )); } if $select->rows;

    Of course, the following would be shorter:

    do { print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script +have to die" )); exit(0); } if $select->rows;

    Even shorter:

    print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script hav +e to die" )), exit(0) if $select->rows;

    The problem is neither is particularly readable. You might as well use

    if ($select->rows) { print(h1( {class=>'cyan'}, "Error occured $row->{username}! Script +have to die" )); exit(0); }

    There is a readable one-line, however:

    sub fatal_error { print(h1( {class=>'cyan'}, @_); exit(0); } fatal_error("Error occured $row->{username}! Script have to die") if $select->rows;
      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" ));
        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}
      #========== CHECK IF USER EXISTS & OR TRYING TO REGISTER MORE THAN ONC +E ======== local *STDERR = *STDOUT; $! = 0; $select = $db->prepare( "SELECT username, DATE_FORMAT(date, '%d %b, %H +:%i') AS f_date FROM users WHERE username = ?" ); $select->execute( $username ); $row = $select->fetchrow_hashref; die( h1( {class=>'cyan'}, "&#924;&#945; &#949;&#943;&#963;&#945;&#953; + &#942;&#948;&#951; &#956;&#941;&#955;&#959;&#962; $row->{username}! +&#928;&#961;&#945;&#947;&#956;&#945;&#964;&#959;&#960;&#959;&#943;&#9 +51;&#963;&#949;&#962; &#949;&#947;&#947;&#961;&#945;&#966;&#942; &#96 +3;&#964;&#953;&#962; $row->{f_date}." )) if( $select->rows ); $select = $db->prepare( "SELECT username FROM users WHERE host = ?" ); $select->execute( $host ); $row = $select->fetchrow_hashref; die( h1( {class=>'cyan'}, "&#922;&#972;&#966;&#964;&#959; $row->{usern +ame}! &#924;&#951;&#957; &#960;&#961;&#959;&#963;&#960;&#945;&#952;&# +949;&#943;&#962; &#957;&#945; &#954;&#940;&#957;&#949;&#953;&#962; &# +960;&#959;&#955;&#955;&#941;&#962; &#949;&#947;&#947;&#961;&#945;&#96 +6;&#941;&#962; &#956;&#949; &#948;&#953;&#945;&#966;&#959;&#961;&#949 +;&#964;&#953;&#954;&#940; o&#957;&#972;&#956;&#945;&#964;&#945;!" )) + if( $select->rows );
      I liek the approach you mentioned ikegami, thank you but:

      Software error:

      Μα είσαι ήδη μέλος marmel13! Πραγματοποίησες εγγραφή στις 25 Apr, 01:16.

      at D:\www\cgi-bin\register.pl line 74. For help, please send mail to the webmaster (nikos1337@gmail.com), giving this error message and the time and date of the error. Sat Apr 28 13:05:09 2007 register.pl: Μα είσαι ήδη μέλος marmel13! Πραγματοποίησες εγγραφή στις 25 Apr, 01:16.

      Iam getting both the error that i want to print colored which is good but the software error above....
        die not only exits your program, it also sets a non-zero return code. Your webserver sees that, and concludes there was an error in your program.

        Either use ikegami's hint of setting $!=0, or do the sensible thing and use exit.

Re: Merge 2 lines in 1 die statement
by sgifford (Prior) on Apr 28, 2007 at 05:51 UTC
    You could create your own equivalent to die that would do the above. Something like:
    sub mydie(@) { print h1(@_); exit(0); }

    Then you can say:

    $select->rows and mydie {class=>'cyan'}, "Error occured $row->{username}! Script h +ave to die";

    Update: Fix typo above (Thanks fenLisesi!)

Re: Die statement with text & formatting of the user
by jonadab (Parson) on Apr 28, 2007 at 15:08 UTC

    I usually do what sgifford says, and write my own mydie function to replace the built-in die (which, among other things, allows calling a function that wraps my error message in a page that includes such things as site headers, footers, stylesheets, sidebars, whatever, provided said routine does not itself call the mydie function), but there's also CGI::Carp qw(fatalsToBrowser); The latter is simpler and useful for development, although I don't know that I would encourage keeping it in production code, since it potentially could expose to the user information you might not want them to have, which might be a security issue.

    -- 
    We're working on a six-year set of freely redistributable Vacation Bible School materials.