develooper Front page | perl.perl5.porters | Postings from March 2003

[PATCH] Text::Abbrev warnings safe

From:
Peter Scott
Date:
March 29, 2003 01:03
Subject:
[PATCH] Text::Abbrev warnings safe
Message ID:
5.2.0.9.2.20030328161002.00b45f08@shell2.webquarry.com
Text::Abbrev is not warnings safe if abbrev is passed zero arguments, 
because the line

    } elsif (ref \$_[0] eq 'GLOB') {  # is actually a glob (deprecated)
 
will autovivify an undef onto @_ if there's nothing there, and this 
causes uninitialized warnings on subsequent lines.

I know, why would you call abbrev with zero arguments.  'Cos it might 
be part of a routine that's parsing a data set of commands that mostly take arguments 
but a few don't, that's why.  Anyway, cheap fix:


--- lib/Text/Abbrev.pm.orig	Fri Mar 28 16:19:13 2003
+++ lib/Text/Abbrev.pm	Fri Mar 28 16:26:13 2003
@@ -43,6 +43,7 @@
 sub abbrev {
     my ($word, $hashref, $glob, %table, $returnvoid);
 
+    @_ or return;   # So we don't autovivify onto @_ and trigger warning
     if (ref($_[0])) {           # hash reference preferably
       $hashref = shift;
       $returnvoid = 1;
--- lib/Text/Abbrev.t.orig	Fri Mar 28 16:22:14 2003
+++ lib/Text/Abbrev.t	Fri Mar 28 16:26:03 2003
@@ -1,6 +1,6 @@
 #!./perl
 
-print "1..7\n";
+print "1..8\n";
 
 BEGIN {
     chdir 't' if -d 't';
@@ -49,3 +49,11 @@
 
 $s = join ':', sort keys %y;
 print (($r eq $s)?"ok $test\n":"not ok $test\n"); $test++;
+
+
+# warnings safe with zero arguments
+my $notok;
+$^W = 1;
+$SIG{__WARN__} = sub { $notok++ };
+abbrev();
+print ($notok ? "not ok $test\n" : "ok $test\n"); $test++;

-- 
Peter Scott
Pacific Systems Design Technologies




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About