Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can not print die_string using die function

Status
Not open for further replies.

zhenning

Technical User
Sep 22, 2005
50
CA
I tried to print an error string using die function:

open(F,$pathname) || die("No data available for the specified date, Please select another date.\n");

But I just got the program finished without printing error string. What could be wrong?

Thanks!

Zhenning
 
it should work like you have it, but with perl the lower precedence "or" operator is better used for flow control:

Code:
open(F,$pathname) or die "No data available for the specified date: $! [Please select another date].\n";

 
Hi KevinADC,

Thank you for your comment. I tried you code and still not working, the program exits without printing the message. Where else could be wrong?

Thanks.
 
Out of curiousity what version of perl are you using and on what? (e.g. cygwin/windows combo thingy... linux/bsd/qnx?)

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
just try this and see what happens:

Code:
open(F,'<blah.blah') or die "Can't open blah.blah: $!";
close(F);

if you run as a CGI add p[roper headers first.
 
Thanks Kevin, I tried your code with no output.

I run as CGI and I only have following header:

use vars qw(@lines $dbobj $file $i);

Did I miss any headers?
 
like this if a CGI, add not other lines of code except the shebang (the path to perl) line if necessary:

Code:
print "Content-type: text/html\n\n"; #<-- this is the header I was refering to
open(F,'<blah.blah') or die "Can't open blah.blah: $!";
close(F);

If nothing prints to the browser/screen, check the server error logs.
 
Hi Kevin,

I tried that. No output at browser and I got following error and error logs:

[Mon Aug 14 16:24:18 2006] [error] [client xxx.xxx.xxx.xxx] Can't open blah.blah: No such file or directory at /var/ line 27., referer:
I have following headers:
print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD>";
print "<TITLE>Port Status results</title>";
print "</HEAD>";
print "<BODY>";

Why the messages went to the error logs but not to the browser window?
Thanks!
 
STDERR is usually equivalent to STDOUT, but in your case it does not appear to be so. But I don't know why unless STDERR has been redirected somehow.
 
If I use:
open(F,$pathname1) or print "No data available for the specified date, Please select another date.\n";

The string can show on browser window. Just "die" does not work... Weird!
 
Try using:

use CGI::Carp qw/fatalsToBrowser/;

and see if that redirects fatals errors to the browser.
 
Have you tried printing with warn vs die and $!?

i.e

open FILE, "< $pathName" or warn "$!: Error\n";
 
I tried "use CGI::Carp qw/fatalsToBrowser/;" and I got following errors when excuting the program:
Can't locate CGI/Carp.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linuxthread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.80 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 .) at ./tracking_new.cgi line 5.
BEGIN failed--compilation aborted at ./tracking_new.cgi line 5.

Do I miss modules in perl?

I also tried: open FILE, "< $pathName" or warn "$!: Error\n";

The same with die, the Error showed in error_log but not in browser.

Thank you for all the suggestions. Sorry I know too little about Perl...

 
You must have a non-standard installation of perl. CGI::Carp has been a core module for along time, which means it should come with any standard distribution of perl. Version 5.8.0 certainly should have it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top