Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Sometimes, one of my comparison subroutines causes a run-time warning. Perl has an amazing ability to do "something reasonable" in a situation like this and I am happy with what Perl does.

But the warning message doesn't tell me enough to go quickly to the input record that caused the problem. This is demonstrated below.

What I would like to do is: if a warning happens within compareLT105(), is to print something specific to this particular subroutine call, like: $arrayRef->[0] in addition to the Perl generated warning. That would narrow the field from ~1 million records to one record.

I don't want to "die", I just want to add something to the warning message. I read about installing a __WARN__ signal handler, but that appears to require exposing $arrayRef to a wider scope. Block eval{} works with "die".

This is of course a very simplistic version of the "real thing", but I hope provides enough info to get me on the right track.

I hope the solution is so easy that it is a "Duh!".

#!/usr/bin/perl -w use strict; $|=1; #turns off buffering for STDOUT my @x = ( [1,'100'], [2,'106'], [3,'100A'], [4,'100B2'], ); foreach my $ref (@x) { print "For $ref->[1] compareLT105 says: ", compareLT105($ref),"\n"; } sub compareLT105 { my $arrayRef = shift; return 'true' if $arrayRef->[1] < 105; return 'false'; } __END__ For 100 compareLT105 says: true For 106 compareLT105 says: false Argument "100A" isn't numeric in numeric lt (<) at C:\TEMP\warnexample +.pl line 21. For 100A compareLT105 says: true Argument "100B2" isn't numeric in numeric lt (<) at C:\TEMP\warnexampl +e.pl line 21. For 100B2 compareLT105 says: true

In reply to How to modify a warning message within a sub? by Marshall

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found