Perl gurus: why do we need Scalar::Util?
Hi,
please do not let my die dumb: what is this "blessed" thing all about? And why do we need it in the private-Error.pm??
Thanks,
Dscho
Hi,
Dear diary, on Mon, Jul 10, 2006 at 01:44:39PM CEST, I got a letter where Johannes Schindelin <[email protected]> said that...
Quoted 2 lines
please do not let my die dumb: what is this "blessed" thing all about? And why do we need it in the private-Error.pm??
I'm working on it. I'll try to get a patch together by the evening.
Signature · 4 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ Snow falling on Perl. White noise covering line noise. Hides all the bugs too. -- J. Putnam
PATCH 1/1 Eliminate Scalar::Util usage from private-Error.pm
We used just the blessed() routine so steal it from Scalar/Util.pm. ;-) (Unfortunately, Scalar::Util is not bundled with older Perl versions.)
---
perl/private-Error.pm | 20 ++++++++++++++++---- 1 files changed, 16 insertions(+), 4 deletions(-)
perl/private-Error.pm +16 -4
@@ -43,8 +43,6 @@ sub throw_Error_Simple # Exported subs are defined in Error::subs -use Scalar::Util (); - sub import { shift; local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
@@ -290,6 +288,20 @@ use vars qw(@EXPORT_OK @ISA %EXPORT_TAGS @ISA = qw(Exporter); + +# Stolen from Scalar::Util: + +# Hope nobody defines a sub by this name +sub UNIVERSAL::a_sub_not_likely_to_be_here { ref($_[0]) } + +sub blessed ($) { + local($@, $SIG{__DIE__}, $SIG{__WARN__}); + length(ref($_[0])) + ? eval { $_[0]->a_sub_not_likely_to_be_here } + : undef +} + + sub run_clauses ($$$\@) { my($clauses,$err,$wantarray,$result) = @_; my $code = undef;
@@ -312,7 +324,7 @@ sub run_clauses ($$$\@) { $i -= 2; next CATCHLOOP; } - elsif(Scalar::Util::blessed($err) && $err->isa($pkg)) { + elsif(blessed($err) && $err->isa($pkg)) { $code = $catch->[$i+1]; while(1) { my $more = 0;
@@ -421,7 +433,7 @@ sub try (&;$) { if (defined($err)) { - if (Scalar::Util::blessed($err) && $err->can('throw')) + if (blessed($err) && $err->can('throw')) { throw $err; }
Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Quoted 2 lines · Petr Baudis
We used just the
blessed()routine so steal it from Scalar/Util.pm. ;-) (Unfortunately,Scalar::Utilis not bundled with older Perl versions.)
Eh, but aren't we going to rip out the try{}catch{} stuff to avoid extra closures?
Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Junio" == Junio C Hamano <[email protected]> writes:
Quoted 2 lines · Junio> Petr Baudis <[email protected]> writes:
Quoted 2 lines
We used just the
blessed()routine so steal it from Scalar/Util.pm. ;-) (Unfortunately,Scalar::Utilis not bundled with older Perl versions.)
Junio> Eh, but aren't we going to rip out the try{}catch{} stuff to Junio> avoid extra closures?
Heh. Yeah, didn't notice that. I keep cutting down the trees, but the forest remains. :)
Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
In reply to Petr BaudisQuoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Petr" == Petr Baudis <[email protected]> writes:
Petr> We used just the blessed() routine so steal it from Scalar/Util.pm. ;-) Petr> (Unfortunately, Scalar::Util is not bundled with older Perl versions.)
Wow. That's sure the long way around for what I would use this for:
sub blessed {
my $item = shift;
local $@; # don't kill an outer $@
ref $item and eval { $item->can('can') };
}We call it "doing the can-can". :)
And this solution has the advantage that it doesn't pollute UNIVERSAL.
Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Randal" == Randal L Schwartz <[email protected]> writes:
Randal> sub blessed {
Randal> my $item = shift;
Randal> local $@; # don't kill an outer $@
Randal> ref $item and eval { $item->can('can') };Randal> }
Oops, lose the local $@ line. Just found out this is a broken thing in current Perls. The rest is good though.
Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
"Randal" == Randal L Schwartz <[email protected]> writes:
"Randal" == Randal L Schwartz <[email protected]> writes:
Randal> sub blessed {
Randal> my $item = shift;
Randal> local $@; # don't kill an outer $@
Randal> ref $item and eval { $item->can('can') };
Randal> }
Randal> Oops, lose the local $@ line. Just found out this is a broken Randal> thing in current Perls. The rest is good though.
And thirdly, ignore what I *just* said, and concentrate on what I *previously* said, becaused my testing was off.
Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Johannes" == Johannes Schindelin <[email protected]> writes:
Johannes> please do not let my die dumb: what is this "blessed" thing all about? And Johannes> why do we need it in the private-Error.pm??
Ugh. Just took a peek for the first time at the "next" branch, and I see the dangerous syntactic-sugar try { } catch { }. Sorry for not noticing that earlier.
While that syntax looks like it would make things easier in theory, in practice it is a source of leak-after-leak because it creates a closure for the two blocks, and that can easily lead to a circular reference for long-running tools. This would be of some concern if someone writes a mod_perl module or a standalone webserver that doesn't exec itself to clean up (which it shouldn't need).
So, if there's going to be rewrite, the first part would be to eliminate the try { } catch { } sugar, and replace it with more traditional exception catchers.
eval { };
if ($@) { ... }Note that I'm *not* suggesting not to use Error.pm - that's a great means by which to create hierarchical error classes that stringify nicely and carry context on the error. I'm just saying to throw out the try/catch syntax helper.
Sorry about that. If it's any consequence, we got it right in Perl 6. :)
Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Dear diary, on Mon, Jul 10, 2006 at 03:28:49PM CEST, I got a letter where "Randal L. Schwartz" <[email protected]> said that...
Quoted 4 lines
While that syntax looks like it would make things easier in theory, in practice it is a source of leak-after-leak because it creates a closure for the two blocks, and that can easily lead to a circular reference for long-running tools.
Care to elaborate, please? All I've found are several mentions that the problem is there, but not what the problem actually is and it doesn't occur to me why is it a problem.
Signature · 4 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ Snow falling on Perl. White noise covering line noise. Hides all the bugs too. -- J. Putnam