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

10 messages from 2006-07-10 to 2006-07-11. Participants: Johannes Schindelin, Petr Baudis, Randal L. Schwartz, Junio C Hamano.
Thread: https://gitlist.dev/t/4829

## Johannes Schindelin, 2006-07-10 11:44

Subject: Perl gurus: why do we need Scalar::Util?
Message-ID: <Pine.LNX.4.63.0607101343060.29667@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0607101343060.29667%40wbgn013.biozentrum.uni-wuerzburg.de

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

```

## Petr Baudis, 2006-07-10 13:00

Subject: Re: Perl gurus: why do we need Scalar::Util?
Message-ID: <20060710130046.GW29115@pasky.or.cz>
URL: https://gitlist.dev/e/20060710130046.GW29115%40pasky.or.cz
In-Reply-To: <Pine.LNX.4.63.0607101343060.29667@wbgn013.biozentrum.uni-wuerzburg.de>

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

```

## Randal L. Schwartz, 2006-07-10 13:28

Subject: Re: Perl gurus: why do we need Scalar::Util?
Message-ID: <86k66lsi5q.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86k66lsi5q.fsf%40blue.stonehenge.com
In-Reply-To: <Pine.LNX.4.63.0607101343060.29667@wbgn013.biozentrum.uni-wuerzburg.de>

```
>>>>> "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, 2006-07-10 14:29

Subject: Re: Perl gurus: why do we need Scalar::Util?
Message-ID: <20060710142916.GX29115@pasky.or.cz>
URL: https://gitlist.dev/e/20060710142916.GX29115%40pasky.or.cz
In-Reply-To: <86k66lsi5q.fsf@blue.stonehenge.com>

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

```

## Petr Baudis, 2006-07-11 00:53

Subject: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <20060711005354.5911.62525.stgit@machine.or.cz>
URL: https://gitlist.dev/e/20060711005354.5911.62525.stgit%40machine.or.cz
In-Reply-To: <20060710130046.GW29115@pasky.or.cz>

```
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, 2006-07-11 01:38

Subject: Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <86bqrwncnq.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86bqrwncnq.fsf%40blue.stonehenge.com
In-Reply-To: <20060711005354.5911.62525.stgit@machine.or.cz>

```
>>>>> "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, 2006-07-11 01:40

Subject: Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <867j2knckf.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/867j2knckf.fsf%40blue.stonehenge.com
In-Reply-To: <86bqrwncnq.fsf@blue.stonehenge.com>

```
>>>>> "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, 2006-07-11 01:42

Subject: Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <863bd8nchg.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/863bd8nchg.fsf%40blue.stonehenge.com
In-Reply-To: <867j2knckf.fsf@blue.stonehenge.com>

```
>>>>> "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, 2006-07-11 01:57

Subject: Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <7v4pxo7vk4.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v4pxo7vk4.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20060711005354.5911.62525.stgit@machine.or.cz>

```
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, 2006-07-11 03:38

Subject: Re: [PATCH] Eliminate Scalar::Util usage from private-Error.pm
Message-ID: <86y7v0lska.fsf@blue.stonehenge.com>
URL: https://gitlist.dev/e/86y7v0lska.fsf%40blue.stonehenge.com
In-Reply-To: <7v4pxo7vk4.fsf@assigned-by-dhcp.cox.net>

```
>>>>> "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!

```
