threads / discuss / 4829

Perl gurus: why do we need Scalar::Util?

Subject: Perl gurus: why do we need Scalar::Util?

## tl;dr

10 messages between Jul 10, 2006 and Jul 11, 2006.

replies: 9people: 4as markdown or json

Petr Baudis· Jul 10, 2006, 13:00 UTC · re: Johannes Schindelin · lore

Re: Perl gurus: why do we need Scalar::Util?

  Hi,

Dear diary, on Mon, Jul 10, 2006 at 01:44:39PM CEST, I got a letter where Johannes Schindelin <Johannes.Schindelin@gmx.de> said that...

> 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.
-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
Snow falling on Perl. White noise covering line noise.
Hides all the bugs too. -- J. Putnam
Petr Baudis· Jul 11, 2006, 00:53 UTC · re: Petr Baudis · lore

[PATCH] 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.)

Signed-off-by: Petr Baudis <pasky@suse.cz>
---
 perl/private-Error.pm |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/perl/private-Error.pm b/perl/private-Error.pm
index ebd0749..848e82b 100644
--- a/perl/private-Error.pm
+++ b/perl/private-Error.pm
@@ -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;
         }
Randal L. Schwartz· Jul 11, 2006, 01:38 UTC · re: Petr Baudis · lore

Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm

>>>>> "Petr" == Petr Baudis <pasky@suse.cz> 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.
-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!
Randal L. Schwartz· Jul 11, 2006, 01:40 UTC · re: Randal L. Schwartz · lore

Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm

>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> 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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!
Randal L. Schwartz· Jul 11, 2006, 01:42 UTC · re: Randal L. Schwartz · lore

Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm

>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> writes:
>>>>> "Randal" == Randal L Schwartz <merlyn@stonehenge.com> 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.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!
Junio C Hamano· Jul 11, 2006, 01:57 UTC · re: Petr Baudis · lore

Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm

Petr Baudis <pasky@suse.cz> writes:
> We used just the blessed() routine so steal it from Scalar/Util.pm. ;-)
> (Unfortunately, Scalar::Util is not bundled with older Perl versions.)

Eh, but aren't we going to rip out the try{}catch{} stuff to avoid extra closures?

Randal L. Schwartz· Jul 11, 2006, 03:38 UTC · re: Junio C Hamano · lore

Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm

>>>>> "Junio" == Junio C Hamano <junkio@cox.net> writes:
Junio> Petr Baudis <pasky@suse.cz> writes:
>> We used just the blessed() routine so steal it from Scalar/Util.pm. ;-)
>> (Unfortunately, Scalar::Util is 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. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!
Randal L. Schwartz· Jul 10, 2006, 13:28 UTC · re: Johannes Schindelin · lore

Re: Perl gurus: why do we need Scalar::Util?

>>>>> "Johannes" == Johannes Schindelin <Johannes.Schindelin@gmx.de> 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. :)
-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!
Petr Baudis· Jul 10, 2006, 14:29 UTC · re: Randal L. Schwartz · lore

Re: Perl gurus: why do we need Scalar::Util?

Dear diary, on Mon, Jul 10, 2006 at 03:28:49PM CEST, I got a letter where "Randal L. Schwartz" <merlyn@stonehenge.com> said that...

> 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.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
Snow falling on Perl. White noise covering line noise.
Hides all the bugs too. -- J. Putnam

← back to recent threads