#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Singapore
    Posts
    12
    Rep Power
    0

    Another DBI Problem


    Hi. Another DBI problem going on here. I have this:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use DBI;
    
    my $dbh = DBI->connect('dbi:mysql:test', 'test', 'test') || die $DBI::errstr;
    print 'Connection OK';
    Once again, the database "test" does not exist.

    Now, I launch the script from the command line. Yes, there are error messages. Then I launch the script from Internet Explorer. Same output. However, if I change the script to this:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use DBI;
    
    print "Content-Type: text/html\n\n";
    
    my $dbh = DBI->connect('dbi:mysql:test', 'test', 'test') || die $DBI::errstr;
    print 'Connection OK';
    Now there are no error messages if I launch the script from IE. All I've done is print the HTML header first. Seeing that I cannot print out the header, I alter the script to this:
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use DBI;
    
    $SIG{__DIE__} = \&test;
    
    my $dbh = DBI->connect('dbi:mysql:test', 'test', 'test') || die $DBI::errstr;
    print 'Connection OK';
    
    sub test
    {
    print "Content-Type: text/html\n\n";
    print shift;
    }
    I launch the script. It works! But see? Just now I printed the HTML header, and no error messages came out. But now it works already!

    Can someone please explain to me what is happening here? Thanks a lot.
  2. #2
  3. An Ominous Coward
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2002
    Posts
    4,424
    Rep Power
    0
    die() sends output to STDERR, not STDOUT. The server is listening for data from STDOUT, not STDERR. You'll either need to provide your own error-handling routine(s) instead of using die (my personal preference) or redirect STDERR to STDOUT.

    You can also check out Carp::FatalsToBrowser for testing purposes if you'd like to use die while developing your scripts.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Singapore
    Posts
    12
    Rep Power
    0
    I see!

    Ok thanks.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Singapore
    Posts
    12
    Rep Power
    0
    By the way, I know this is a very silly question, but may I ask what is the difference between these 2 statements:

    Code:
    *STDERR = *STDOUT;
    *STDOUT = *STDERR;
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    England, UK
    Posts
    88
    Rep Power
    17
    How about using

    PHP Code:
    use CGI::Carp qw(fatalsToBrowser); 
    This would then direct your die messages to the browser so you can see if/when a die occurs. The output is not mega customisable but its good to know whats happening.

    Regards,
    Mike Mackay.

IMN logo majestic logo threadwatch logo seochat tools logo