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

Re: mission impossible for the day

Thread Previous
From:
Alex Gough
Date:
March 8, 2001 09:19
Subject:
Re: mission impossible for the day
Message ID:
3AA7BEDB.8070409@rcon.org
Date: Thu, 8 Mar 2001 13:23:25 +0000 (GMT)
Message-Id: <200103081323.NAA06417@tiree.fdgroup.co.uk>

 >> Your task, should you choose to accept it, is to search and utterly 
destroy the reason why Switch 2.01 (requires Text::Balanced) doesn't
 >> like bleadperl.  This is what we know:

 > It's a bug in Switch.pm (or to put it another way, its fixable
 > in Switch.pm, possibly by adding a stringify operator).

 > The nub of the problem is infinite mutual recursion, caused by the
 > following overloaded operator in Switch.pm:

 > "bool"  => sub { croak "Can't use && or || in expression containing
 > __" },

 > Bool then calls croak, which calls format_arg, which....

 > I guess adding a stringify operator to Switch will make the problem
 > go away, but since overloading and the internals of Switch.pm arent
 > my strong points, I'll hand the baton on to wiser folk for the fix

 > I presume it's only shown up recently in beadperl due to some change
 > in Carp. NB: why *is* croak attempting to display the caller args?

I like the idea of seeing args listed when croak and its ilk are called,
it saves some of the bother of writing croak "bleh!", @_;

There are two ways to fix this, either all references which are
displayed as args get shown as references or if they can be directly
stingified, that could be shown instead.  I think I prefer the second.

The following two patches do the first, or the second, hopefully with
minimal effect to non-overloaded programs.

--
Alex Gough (alex@rcon.org)


########  stringify where possible...
--- Heavy.pm.old	Thu Mar  8 15:44:34 2001
+++ Heavy.pm	Thu Mar  8 16:53:55 2001
@@ -48,7 +48,14 @@
      $arg = 'undef';
    }
    elsif (ref($arg)) {
-    $arg .= ''; # Make it a string;
+    if (defined($overload::VERSION) && overload::Overloaded($arg)) {
+      # someone, somewhere, said use overload;
+      my $StrMeth = overload::Method($arg,qw{""});
+      $arg = $StrMeth ? $arg->$StrMeth() : overload::StrVal($arg);
+    }
+    else {
+      $arg .= ''; # Make it a string;
+    }
    }
    $arg =~ s/'/\\'/g;
    $arg = str_len_trim($arg, $MaxLenArg);



#######  just print reference
--- Heavy.pm.old	Thu Mar  8 15:44:34 2001
+++ Heavy.pm	Thu Mar  8 16:56:17 2001
@@ -48,7 +48,7 @@
      $arg = 'undef';
    }
    elsif (ref($arg)) {
-    $arg .= ''; # Make it a string;
+    $arg = defined($overload::VERSION) ? overload::StrVal($arg) : "$arg";
    }
    $arg =~ s/'/\\'/g;
    $arg = str_len_trim($arg, $MaxLenArg);

 >> >> destroy the reason why Switch 2.01 (requires Text::Balanced)
 >> doesn't like bleadperl.  This is what we know:

 > It's a bug in Switch.pm (or to put it another way, its fixable in
 > Switch.pm, possibly by adding a stringify operator).

 > The nub of the problem is infinite mutual recursion, caused by the 
following
 >  overloaded operator in Switch.pm:

 > "bool"  => sub { croak "Can't use && or || in expression containing __"
 >  },

 > Bool then calls croak, which calls format_arg, which....

 > I guess adding a stringify operator to Switch will make the problem go
 >  away, but since overloading and the internals of Switch.pm arent my
 >  strong points, I'll hand the baton on to wiser folk for the fix

 > I presume it's only shown up recently in beadperl due to some change in
 >  Carp. NB: why *is* croak attempting to display the caller args?

I like the idea of seeing args listed when croak and its ilk are called,
it saves some of the bother of writing croak "bleh!", @_;

There are two ways to fix this, either all references which are
displayed as args get shown as references or if they can be directly
stingified, that could be shown instead.  I think I prefer the second.

The following two patches (to Carp/Heavy.pm) do the first, or the 
second, hopefully with minimal effect to non-overloaded programs.

--
Alex Gough (alex@rcon.org)


########  stringify where possible...
--- Heavy.pm.old	Thu Mar  8 15:44:34 2001
+++ Heavy.pm	Thu Mar  8 16:53:55 2001
@@ -48,7 +48,14 @@
      $arg = 'undef';
    }
    elsif (ref($arg)) {
-    $arg .= ''; # Make it a string;
+    if (defined($overload::VERSION) && overload::Overloaded($arg)) {
+      # someone, somewhere, said use overload;
+      my $StrMeth = overload::Method($arg,qw{""});
+      $arg = $StrMeth ? $arg->$StrMeth() : overload::StrVal($arg);
+    }
+    else {
+      $arg .= ''; # Make it a string;
+    }
    }
    $arg =~ s/'/\\'/g;
    $arg = str_len_trim($arg, $MaxLenArg);



#######  just print reference
--- Heavy.pm.old	Thu Mar  8 15:44:34 2001
+++ Heavy.pm	Thu Mar  8 16:56:17 2001
@@ -48,7 +48,7 @@
      $arg = 'undef';
    }
    elsif (ref($arg)) {
-    $arg .= ''; # Make it a string;
+    $arg = defined($overload::VERSION) ? overload::StrVal($arg):"$arg";
    }
    $arg =~ s/'/\\'/g;
    $arg = str_len_trim($arg, $MaxLenArg);


Thread Previous


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