in reply to Die statement with text & formatting of the user

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;

Replies are listed 'Best First'.
Re^2: Merge 2 lines in 1 die statement
by Nik (Initiate) on Apr 28, 2007 at 07:52 UTC
    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}
        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?

Re^2: Merge 2 lines in 1 die statement
by Nik (Initiate) on Apr 28, 2007 at 10:10 UTC
    #========== 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.