Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: "warn" is your best friend

by tobyink (Canon)
on Mar 23, 2014 at 14:39 UTC ( #1079441=note: print w/replies, xml ) Need Help??


in reply to Re: "warn" is your best friend
in thread "warn" is your best friend

You want to know my trick for that? When debugging, instead of outputting with warn $message, output messages with ::diag $message.

Advantages:

  • ::diag outputs messages prefixed with a "#" sign, so it looks nice in test output.

  • ::diag won't cause Test::Warnings fails.

  • When you're not running your test suite, Test::More probably won't be loaded, so the ::diag function won't exist. This will cause ::diag $message to bomb loudly, so you won't forget any debugging messages you've scattered amongst your code.

    Note that because of the way function calls are parsed in Perl, including parentheses in the function call like ::diag($message) will make it bomb at run-time instead of compile-time. This is why I recommend not using the parentheses in ::diag $message. If you need to resolve ambiguities using parentheses, you can always do so with external ones like (::diag $message).

  • Along the same lines, a statement that starts with :: looks weird, so will stand out like a sore thumb when you're looking for it to remove it.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^3: "warn" is your best friend
by Bloodnok (Vicar) on Mar 23, 2014 at 21:58 UTC
    Hiya tobyink,

    Much as tho' I like and indeed really appreciate, your insight into debugging the test cases, I was actually referring to the use of spurious debugging statement(s) in the code itself - where my use of warn suggested that the code was even more broken than and in a different place to, the actual problem.

    A user level that continues to overstate my experience :-))

      Yes, I meant use ::diag for debugging in the module code itself; not just in the test suite!

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        Aahh, I see now - at the time the leading '::' wasn't abundantly clear (as if anything ever is? :-) How on earth did you find out about that - there's nothing in the perldoc for it ?

        Awesome, ground-breaking (for me) discovery.

        Many thanx again ... tobyink++++

        A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1079441]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-06-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (94 votes). Check out past polls.

    Notices?