Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

phillup (4419)

phillup
  (email not shown publicly)
http://www.philsplace.org/

Journal of phillup (4419)

Friday August 13, 2004
01:29 PM

cheap profiler

[ #20397 ]

Well, there was an interesting side effect of parsing my web logs to identify all the places where I'm throwing warnings.

I've got a relative count of how many times the code is being called. Of course, it is only the code that causes warnings that is being counted...

Anyways, this is my most troublesome snippet, accounting for 80 percent of the current warnings.

  $form .= $args->{'hidden'};
  $form .= '</form>';

This code builds forms, and has an optional parameter for hidden fields. If there are any hidden fields, they are added right before we close off the form.

It seems a bit much to have to say

  $form .= $args->{'hidden'} if $args->{'hidden'};

When the original code "Does The Right Thing".

Also, 100% of the errors have to do with string concatenation, or comparison operators where I expect that values may be 'undef'.

So... I'm about to spend a lot of time eleminating warnings that are "harmless". Because, even with all my bitching... I know that there will be a payoff... eventually.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • For clarity and brevity you can rewrite

    $form .= $args->{'hidden'} if $args->{'hidden'};

    as

    $form .= $args{'hidden'} || "";

    If the value of $args{'hidden'} evaluates to false, "" is used instead, and the undef value isn't used for concatenation.

    But note that 0 evaluates to false, so if you expect that as a legit value of $args{'hidden'}, this won't work.

    The new defined-or ( // instead of || ) operator will take care of this in Perl 5.10 (right?)

    • But note that 0 evaluates to false, so if you expect that as a legit value of $args{'hidden'}, this won't work.

      Hm... now that you mention it, mine won't either if the string is '0'.

      It isn't a problem in this specific instance... but I may need to actually use 'exists' on the value as a more 'generic' solution.
      • You could always create a small utility function to keep things nice and tidy.
        sub definedOrEmpty {
            defined($_[0]) ? $_[0] : "";
        }
        or something like that.
        $form .= definedOrEmtpy( $args->{'hidden'} );
        Maybe a shorter name would be better. orEmpty perhaps. Whatever's clear in your situation.
  • Also, 100% of the errors have to do with string concatenation, or comparison operators where I expect that values may be 'undef'.

    Classic mistake: Some of my code expects undef values so I ignore uninit warnings.

    Problem is the rest of your code doesn't expect undef warnings, in those spots getting an undef indicates a possible mistake. By ignoring uninit warnings for one part of the code you ignore problems in the rest. If a certain part of your code expects undefs write it so it handles them quietly

    • Classic mistake: Some of my code expects undef values so I ignore uninit warnings.

      Actually, I am not ignoring any warnings. I added the use warnings pragma and it did what it was supposed to do... flagged possible problems.

      Then, I looked at each and every case and saw that they were all the same class of errors (concatenation of undefined variables or comparison with undefined data) and inspected each and every one and determined that none of them was a bug causing issue.

      Classic mistake: This warning i