On Mon, 14 Jul 2008 18:28:57 -0500
Jonathan Rockway <[EMAIL PROTECTED]> wrote:

> Considering B is core, there is probably no need for a module to do
> this.  But, does this always return correct results?  Does the
> svref_2object call affect the REFCNT?  I am too lazy to check, but these
> are nice questions for the documentation to answer.  If svref_2object
> affects its argument, then it's probably best to write a module that
> compensates for this behavior.

Actually, now I look at it there seems to be more complicated things
going on.

Originally I tried testing it with just the HASH or ARRAY based object
types people tend to use. I thought I'd try out all the reference types,
and here's the behaviour I get:

A new SCALAR ref has REFCNT 2
A new  ARRAY ref has REFCNT 1
A new   HASH ref has REFCNT 1
A new   CODE ref has REFCNT 2
A new   GLOB ref has REFCNT 3
A new  Regex ref has REFCNT 1

[see attached program]

It seems interesting that a new SCALAR or CODE ref has 2 references
already, and a GLOB has 3. I guess the CODE one -might- be explained by
its PAD or something, and maybe the GLOB one has more references in the
Perl symbol table, but these are pure guesses. I also cannot explain the
SCALAR one.

This behaviour seems to complicate the idea of simply asserting
REFCNT == 1.

-- 
Paul "LeoNerd" Evans

[EMAIL PROTECTED]
ICQ# 4135350       |  Registered Linux# 179460
http://www.leonerd.org.uk/
#!/usr/bin/perl -w

use strict;

use B qw( svref_2object );

my %refs = (
   SCALAR => do { \my $var },
   ARRAY  => [],
   HASH   => +{},
   CODE   => sub {},
   GLOB   => \*SomeNewName,
   Regex  => qr/foo/,
);

foreach my $type (qw( SCALAR ARRAY HASH CODE GLOB Regex )) {
   my $REFCNT = svref_2object($refs{$type})->REFCNT;
   printf "A new %6s ref has REFCNT %d\n", $type, $REFCNT;
}

Attachment: signature.asc
Description: PGP signature

Reply via email to