Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

catching warnings

by Anarion (Hermit)
on Apr 25, 2002 at 20:37 UTC ( [id://162140]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

Anarion has asked for the wisdom of the Perl Monks concerning the following question:

I try to catch into a variable this warning:
Useless use of a variable in void context

I have tried to trap it with $SIG{__WARN__} but it doesn't work outside of a BEGIN block, and that's what I want. Here's some code:
lorien:~$ perl -wle '$SIG{__WARN__}=sub{print"TRAPED"};s///' TRAPED TRAPED lorien:~$ perl -wle '$SIG{__WARN__}=sub{print"TRAPED"};$.,$.=0' Useless use of a variable in void context at -e line 1. lorien:~$ perl -wle 'BEGIN{$SIG{__WARN__}=sub{print"TRAPED"};}$.,$.=0 +' TRAPED

I have tried to use it inside an eval block:
lorien:~$ perl -wle '$SIG{__WARN__}=sub{print"TRAPED"};eval q{$.,$.=0 +;}' lorien:~$ lorien:~$ perl -wle '$SIG{__WARN__}=sub{print"TRAPED"};eval {$.,$.=0; +}' Useless use of a variable in void context at -e line 1. lorien:~$
Any idea of why that happens and how to catch it without a BEGIN block?
$anarion=\$anarion;

s==q^QBY_^=,$_^=$[x7,print

Replies are listed 'Best First'.
Re: catching warnings
by Fletch (Bishop) on Apr 25, 2002 at 21:51 UTC
      I already checked that before post.

      I don't want to ignore warnings, I want to save it into a variable, but without the begin i can't.

      $anarion=\$anarion;

      s==q^QBY_^=,$_^=$[x7,print
Re: catching warnings
by jsprat (Curate) on Apr 25, 2002 at 21:59 UTC
    Do you want to execute something when the warning occurs or do you just want to swallow it? If you want it to disappear, then

    no warnings qw(void);

    should do the trick. If you want to print a custom message, try

    warnings::warn('void', 'TRAPED');

    Note: any of the categories of warnings will work.
    Good luck!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://162140]
Approved by particle
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.