Use of unitialised gubbins c/w Brainbench

Nik Clayton nik at ngo.org.uk
Thu Dec 7 21:43:26 GMT 2006


David Cantrell wrote:
> On Thu, Dec 07, 2006 at 01:49:06PM +0000, Andy Armstrong wrote:
>> On 7 Dec 2006, at 13:31, Stray Taoist wrote:
>>> If it gives a 'use of uninitialised variable' warning, is there anyway
>>> (without lots of debugging warns) for me to change the output of  
>>> the warning
>>> to say 'Variable 'foo' was uninitialised at line blah'?
>> You could certainly replace sprintf() with one that said 'Argument 3  
>> to sprintf is undef at line blah'. That do?
> 
> How would you get at the line number?  $. isn't useful.
> 
> I'd be tempted to use -P and __LINE__, but it's a nasty nasty hack.
> Alternatively, write a source filter which mangles __LINE__, but source
> filters are Bad.

If this is a quick hack for sprintf(), override CORE::GLOBAL::sprintf() with 
your own sub that does the check, and uses caller() to get the caller line info.

Something like this (untested)

*CORE::GLOBAL::sprintf = sub {
     my @args = @_;

     foreach my $index ($#args) {
         if(! defined $args[$index]) {
             my @call = caller();
             die "Argument $index to sprintf() is undef at " .
                 $call[1] . ":" . $call[2];
         }
     }

     return CORE::sprintf @args;
};

might do the trick.

N


More information about the london.pm mailing list