Re: Determining the true type of a reference
by adrianh (Chancellor) on May 24, 2003 at 16:32 UTC
|
| [reply] [d/l] |
|
I searched CPAN for 'reference' but Scalar::Util didn't come up in the results. Wow. This works great. Thank you!
| [reply] |
Re: Determining the true type of a reference
by theorbtwo (Prior) on May 24, 2003 at 20:09 UTC
|
| [reply] |
|
I have a tendency to be amazingly stupid quite often and every now and then amazingly devious. ;-)
| [reply] |
Re: Determining the true type of a reference
by PodMaster (Abbot) on May 25, 2003 at 06:42 UTC
|
| [reply] |
Re: Determining the true type of a reference
by Aristotle (Chancellor) on May 25, 2003 at 00:14 UTC
|
That has a drawback when stringification is overloaded. The logical next step is to take that into account:
sub reftype {
require overload;
local $_ = overload::AddrRef(shift);
return /=(\w+(?:::\w+)*)\(0x[0-9a-f]\)\z/;
}
I think you can put things into the package tables at runtime that won't match \w+(?:::\w+)*, but I can't imagine any sane use of that so I'd document it as a limitation and move on.
Update: substituted a star for the plus quantifier. Thanks theorbtwo.
Makeshifts last the longest. | [reply] [d/l] |
Re: Determining the true type of a reference
by bobn (Chaplain) on May 25, 2003 at 16:02 UTC
|
Maybe I'm just slow today. I don't even understand the question. You think that a reference of type ARRAY is the same as membership in the (poorly named) Class ARRAY? This seems nonsensical on it's face. Two entirely different things.
ref did what the doc says it does. So did isa.
I think the simple answer is "Don't name your classes after predefined types". Then you don't have this issue.
We have enough real porblems here without going out of our way to confuse ourselves.
Bob Niederman, http://bob-n.com | [reply] |
|
I'm not sure I actually responded to the real question. I still think naming a class 'ARRAY' is begging for trouble.
But here a snippet that knows the difference:
$aref = []; $href = {}; $sref = \"";
my $text = 'some text';
my $ref = \$text;
my $blessed = bless($ref, 'ARRAY');
print prt_typ($_) for ( [ $aref, '$aref'], [ $href, '$href'],
[ $sref, '$sref'], [$blessed, '$blessed'], );
sub prt_typ
{
my ( $rin, $str ) = @{$_[0]};
print "ref($str) = ", ref($rin), "\n";
eval { my $x = ${$rin} };
return "$str is SCALAR\n\n" unless $@;
eval { my %x = %{$rin} };
return "$str is HASH\n\n" unless $@;
eval { my @x = @{$rin} };
return "$str is ARRAY\n\n" unless $@;
}
Results in:
ref($aref) = ARRAY
$aref is ARRAY
ref($href) = HASH
$href is HASH
ref($sref) = SCALAR
$sref is SCALAR
ref($blessed) = ARRAY
$blessed is SCALAR
Bob Niederman, http://bob-n.com | [reply] [d/l] [select] |
|
{
package FakeScalar;
use overload '${}' => sub { \(shift->{scalar}) };
sub new { bless { scalar => undef }, shift };
};
use Scalar::Util qw(reftype);
my $scalar = FakeScalar->new;
print "reftype(FakeScalar) = ", reftype($scalar), "\n";
print prt_typ( [ $scalar => 'FakeScalar'] );
__END__
# produces
reftype(FakeScalar) = HASH
ref(FakeScalar) = FakeScalar
FakeScalar is SCALAR
| [reply] [d/l] |
|
|
Core Dump using B::XXXX::METHODS resolved
by Mr. Muskrat (Canon) on May 26, 2003 at 15:15 UTC
|
I figured out why the B::XXXX::METHODS were causing page faults. It was a case of user error. <sheepish grin>
One must pass an object from the appropriate class to them or that will happen. For example, to use B::PVLV::TYPE you have to pass an object of the B::PVLV class to it.
use B;
my $lvalue = \substr("abc",1,1);
my $lvobject = B::svref_2object($lvalue);
print B::PVLV::TYPE($lvobject);
Had I read the item about B::svref_2object I would have known this. | [reply] [d/l] |
|
No, you just called the method incorrectly. $lvobject->TYPE should work regardless of what sort of thing was handed to B::??::TYPE. So what made you think you should call a method like a function? Normally you call a method like a method and then any inheritance (what B:: uses) or any class AUTOLOAD can handle the issue. So what gives? I was off at a great party over the weekend and didn't see your post until just now. :-)
| [reply] [d/l] |
|
| [reply] |
Re: Determining the true type of a reference
by little (Curate) on May 24, 2003 at 17:13 UTC
|
| [reply] |
|
print ref($aref);
print "@$aref";
and you will see that it outputs 'ARRAY' which is wrong. In this case, $aref is a SCALAR reference that is blessed into the class ARRAY. | [reply] [d/l] [select] |
|
that won't work due to your own belssing I assume my $blessed = bless($ref, 'ARRAY'); # bless the scalar reference into class ARRAY
Have a nice day
All decision is left to your taste
| [reply] |
|