Getting Perl to print out error messages

Discussion in 'Perl Misc' started by laredotornado, Sep 21, 2011.

  1. 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
    laredotornado, Sep 21, 2011
    #1
    1. Advertising

  2. laredotornado <> writes:
    > 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.
    Rainer Weikusat, Sep 21, 2011
    #2
    1. Advertising

  3. On 2011-09-21, laredotornado <> wrote:
    > 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
    Ilya Zakharevich, Sep 21, 2011
    #3
  4. laredotornado <> wrote:
    >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
    Jürgen Exner, Sep 22, 2011
    #4
  5. laredotornado

    Guest

    On Wed, 21 Sep 2011 09:20:13 -0700 (PDT), laredotornado <> wrote:

    >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>
    , Sep 22, 2011
    #5
  6. On 2011-09-21, Henry Law <> wrote:
    >> 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
    Ilya Zakharevich, Sep 23, 2011
    #6
  7. On 2011-09-21 22:00, Ilya Zakharevich <> wrote:
    > On 2011-09-21, laredotornado <> wrote:
    >> 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...).


    Yes. And almost everyone here disagrees with you.

    hp
    Peter J. Holzer, Sep 25, 2011
    #7
  8. laredotornado

    Dr.Ruud Guest

    On 2011-09-25 10:27, Peter J. Holzer wrote:
    > On 2011-09-21 22:00, Ilya Zakharevich<> wrote:
    >> On 2011-09-21, laredotornado<> wrote:


    >>> 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...).

    >
    > 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 @_ }.

    --
    Ruud
    Dr.Ruud, Sep 25, 2011
    #8
  9. "Peter J. Holzer" <> writes:
    > On 2011-09-21 22:00, Ilya Zakharevich <> wrote:
    >> On 2011-09-21, laredotornado <> wrote:
    >>> 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...).

    >
    > 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.
    Rainer Weikusat, Sep 25, 2011
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    333
    Veronica Jacobs
    Jun 1, 2004
  2. DazedAndConfused
    Replies:
    0
    Views:
    325
    DazedAndConfused
    Sep 13, 2005
  3. keto
    Replies:
    0
    Views:
    924
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    345
    David Cournapeau
    Dec 30, 2008
  5. iMath
    Replies:
    2
    Views:
    150
    Peter Otten
    Dec 26, 2012
Loading...

Share This Page