Getting Perl to print out error messages

L

laredotornado

Hi,

I'm using ACtivePerl 5.12.4 on Windows 7. How do I get Perl to print
out run time error messages? I have this at the top of my script ...

use warnings;
use strict;

but making a call like this ...

$baseTestDir = File::Spec->catfile($data{'myco.checkout.dir'},
$data{'selenium.myco.test.root'});

causes script execution to exit without printing out why (I was able
to pinpoint the failure point by putting in a bunch of print
statements). If I add "use File::Spec" at the top, execution occurs
past that line, but it would be great if Perl just told me why things
failed.

Any help is appreciated, - Dave
 
R

Rainer Weikusat

laredotornado said:
I'm using ACtivePerl 5.12.4 on Windows 7. How do I get Perl to print
out run time error messages? I have this at the top of my script ...

use warnings;
use strict;

but making a call like this ...

$baseTestDir = File::Spec->catfile($data{'myco.checkout.dir'},
$data{'selenium.myco.test.root'});

causes script execution to exit without printing out why (I was able
to pinpoint the failure point by putting in a bunch of print
statements). If I add "use File::Spec" at the top, execution occurs
past that line, but it would be great if Perl just told me why things
failed.

NB: I don't Windows.

How where you running the script? When I try the same here, I get

[rw@sapphire]/tmp $perl a.pl
Can't locate object method "catfile" via package "File::Spec" (perhaps you forgot to load "File::Spec"?) at a.pl line 3.
 
I

Ilya Zakharevich

Hi,

I'm using ACtivePerl 5.12.4 on Windows 7. How do I get Perl to print
out run time error messages? I have this at the top of my script ...

use warnings;
use strict;

Do not. `use warnings' should be better restricted to coding
packages. Use the "standard" boilerplate

#!/usr/bin/perl -w
use strict;

(but this was discussed many times here...).

Hope this helps,
Ilya
 
J

Jürgen Exner

laredotornado said:
I'm using ACtivePerl 5.12.4 on Windows 7. How do I get Perl to print
out run time error messages? I have this at the top of my script ...

use warnings;
use strict;

but making a call like this ...

$baseTestDir = File::Spec->catfile($data{'myco.checkout.dir'},
$data{'selenium.myco.test.root'});

causes script execution to exit without printing out why (I was able
to pinpoint the failure point by putting in a bunch of print
statements). If I add "use File::Spec" at the top, execution occurs
past that line, but it would be great if Perl just told me why things
failed.

Any help is appreciated, - Dave

I cannot reproduce your problem. After declaring the obvious variables I
am getting

Can't locate object method "catfile" via package "File::Spec" (perhaps
you forgot to load "File::Spec"?) at C:\tmp\t.pl line 6.

That message seems to be self-explanatory to me.

jue
 
S

sln

Hi,

I'm using ACtivePerl 5.12.4 on Windows 7. How do I get Perl to print
out run time error messages? I have this at the top of my script ...

use warnings;
use strict;

but making a call like this ...

$baseTestDir = File::Spec->catfile($data{'myco.checkout.dir'},
$data{'selenium.myco.test.root'});

causes script execution to exit without printing out why (I was able
to pinpoint the failure point by putting in a bunch of print
statements). If I add "use File::Spec" at the top, execution occurs
past that line, but it would be great if Perl just told me why things
failed.

Any help is appreciated, - Dave

Its possible your STDERR output is not going to the console.
I can reproduce your dificulty by redirecting STDERR to a file then
calling the File::Spec->catfile() function.

I'm not the greatest I/O expert and don't know windows 7 that well,
is it possible a shell can redirect STDERR, or maybe in your program or
some module your program loads?

Try this. I redirected STDERR to a file then trapped the system error.

-sln


###############
# test.pl
###############
use strict;
use warnings;

my ($baseTestDir, %data);

# Close stderr, reopen it to a text file:

close STDERR;
open STDERR, '>', 'stderr.txt' or die "$!";

# Trap runtime exceptions in eval {} block:

eval {
# This should cause system to die();

$baseTestDir = File::Spec->catfile($data{'myco.checkout.dir'},
$data{'selenium.myco.test.root'});
};

# See if eval trapped an error,
# if so, print it out to STDERR (really to file 'stderr.txt')

$@ && print STDERR "$@\n";

# Print a message to STDOUT

print "All!\n";


# Test that die() sends a message to STDERR (really to file 'stderr.txt')

die "Done!";


__END__

--------------------------------
From the command line:


c:\temp>perl test.pl
All!

c:\temp>type stderr.txt
Can't locate object method "catfile" via package "File::Spec" (perhaps you forgo
t to load "File::Spec"?) at uu.pl line 16.

Done! at uu.pl line 32.

c:\temp>
 
I

Ilya Zakharevich

Do not. `use warnings' should be better restricted to coding
packages. Use the "standard" boilerplate

#!/usr/bin/perl -w
use strict;

(but this was discussed many times here...).

So would it be a good idea, therefore, to have the documentation changed
to reflect this "good practice"? Currently it says
The use warnings pragma [is]... a more flexible alternative for both the command line flag -w ...
I confess that I am confused.

No wonder. This piece of documentation is plain wrong (alas, the same as
most of Perl docs nowadays). AFAIK, one cannot reconstruct the
effects of -w by `use warnings'.

BTW, (I expect that) the majority of bugs caught by -w won't be caught
by `use warnings'. I did not test the OP's problem, but it might be
in this category.

Hope this helps,
Ilya
 
P

Peter J. Holzer

Do not. `use warnings' should be better restricted to coding
packages. Use the "standard" boilerplate

#!/usr/bin/perl -w
use strict;

(but this was discussed many times here...).

Yes. And almost everyone here disagrees with you.

hp
 
D

Dr.Ruud

Yes. And almost everyone here disagrees with you.

I advise to always use '-w' in the shebang line of your .pl files.
It helps me a lot.

The 'use warnings' is best for modules.

During development and testing you can further use '-W',
and a $SIG{__WARN__} = sub { die @_ }.
 
R

Rainer Weikusat

Peter J. Holzer said:
Yes. And almost everyone here disagrees with you.

Which means essentially nothing in absence of arguments supporting
this disagreement. "-w has big problems" doesn't count.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top