Home > Blockchain >  Perl: print compilation warnings for do()
Perl: print compilation warnings for do()

Time:09-09

I am using do() to execute a perl script. I wanted to make sure it was doing the right error checking, but I noticed that warnings don't get printed when I use do(). I left the following warning on purpose in my script. I see it when I run it on the command line

Useless use of numeric eq (==) in void context at utils/queryEvent/query_event.pl line 47.

When I use do(), this message is suppressed. I saw in perlvar that $@ doesn't contain warnings, so I tried to add this before my call to do(), but I still don't see anything.

local $SIG{__WARN__} = sub {
    print "Warning @_\n";
};
do(...);

Is there a way I can still print the compilation/parsing warnings?

I'm using Perl v.5.22.1 on Windows 10.

Added edit:
Both the script calling do() and the script being run through do() have use strict; and use warnings;. It is only the parsing warning that isn't showing up. If I call warn in the do script, then I see that (I'm assuming stderr).

Edit:
Adding example script.
A minimal reproducible version is the following

use strict;
use warnings;

sub test
{
    return 1;
}

main:
{
    (test() == 1);
}

Calling script

use strict;
use warnings;

main:
{
    do('test.pl');
}

CodePudding user response:

You are mistaken. do doesn't affect warnings in the least.

There is no warning because there is nothing that should cause a warning. (test() == 1) isn't being evaluated in void context. Instead, its result is returned by do.

But while warnings AREN'T being silenced, exceptions ARE being silenced. Fixing this will fix the other.

Add a trailing 1 to the file loaded by do, and change the do as follows:

do('test.pl')
   or die( "Can't execute `./test.pl`: ".( $@ // $! ) );

The trailing 1 is used to signal the "caller" whether an error occurred or not. This has the side effect of cause (test() == 1) to be evaluated in void context as expected.

CodePudding user response:

The -w or -W command-line switches will enable warnings throughout your program, including code included through a do statement.

  •  Tags:  
  • perl
  • Related