in reply to Re: Re: Re: semantics differences, &ref and &UNIVERSAL::isa()
in thread semantics differences, &ref and &UNIVERSAL::isa()
As you see, ref will help you find out if an object is of type X, while isa will tell you if the object is of type X, or if the package inherits from X.#!/usr/bin/perl -w use strict; # Our base, or super class package Foo; sub new { bless {}, shift; } # The subclass Bar inherits from Foo package Bar; use base 'Foo'; # The main package package main; # Instantiate an object of type Bar: my $obj = Bar->new(); # Three different kinds of tests, to see what type the object is: print "Using ref to test which object type:\n"; print "\$obj is Bar\n" if(ref $obj eq 'Bar'); print "\$obj is Foo\n" if(ref $obj eq 'Foo'); print "Using UNIVERSAL::isa:\n"; print "\$obj is Bar or subclass of Bar\n" if(UNIVERSAL::isa($obj,'Bar' +)); print "\$obj is Foo or subclass of Foo\n" if(UNIVERSAL::isa($obj,'Foo' +)); print "Using object method isa:\n"; print "\$obj is Bar or subclass of Bar\n" if($obj->isa('Bar')); print "\$obj is Foo or subclass of Foo\n" if($obj->isa('Foo'));
Hope that helps.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Re: semantics differences, &ref and &UNIVERSAL::isa()
by dakedesu (Scribe) on May 18, 2002 at 23:05 UTC |