Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

catching warnings

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

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 jsprat (Curate) on Apr 26, 2002 at 01: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!
Re: catching warnings
by Fletch (Bishop) on Apr 26, 2002 at 01: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

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
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found