Überprüfen, ob eine Referenz geblesst ist

Inhalt:

Was ist geblesst?

Zitat aus perldoc -f bless
      bless REF,CLASSNAME
      bless REF
              This function tells the thingy referenced by REF
              that it is now an object in the CLASSNAME package.
              If CLASSNAME is omitted, the current package is
              used.  Because a "bless" is often the last thing
              in a constructor, it returns the reference for
              convenience.  Always use the two-argument version
              if the function doing the blessing might be inher-
              ited by a derived class.  See perltoot and perlobj
              for more about the blessing (and blessings) of
              objects.

              Consider always blessing objects in CLASSNAMEs
              that are mixed case.  Namespaces with all lower-
              case names are considered reserved for Perl prag-
              mata.  Builtin types have all uppercase names, so
              to prevent confusion, you may wish to avoid such
              package names as well.  Make sure that CLASSNAME
              is a true value.

              See "Perl Modules" in perlmod.

Überprüfung

Möglichkeit 1

Überprüft nur, ob es ein Objekt ist
#! /usr/bin/perl

use strict;
use warnings;
use CGI;

my $cgi = CGI->new();

print "it's an object\n" if UNIVERSAL::isa($cgi,'UNIVERSAL');

Ausgabe:
  it's an object

Möglichkeit 2

Gibt auch aus, zu welcher Klasse das Objekt gehört!
#! /usr/bin/perl

use strict;
use warnings;
use CGI;
use Scalar::Util qw(blessed);

my $cgi = CGI->new();

print blessed($cgi);

Ausgabe:
  CGI

Möglichkeit 3

Um jetzt die Eleganz von Möglichkeit 1 mit der Funktionalität von Möglichkeit 2 zu paaren, kann nun ganz einfach folgende Funktion benutzen:

sub blessed {
   my $obj = shift;
   return (UNIVERSAL::isa($obj, 'UNIVERSAL') ? ref($obj) : undef);
}

Ergänzungen, Kommentare

-- SaschaKieferAkaEsskar - 18 Feb 2005 - Möglichkeit 3

Hier ist eine dazu passende Diskussion im Forum: http://board.perl-community.de/cgi-bin/ikonboard/ikonboard.cgi?s=6020dfeb0e3532336c824279201817c5;act=ST;f=6;t=1244 -- ChristianDuehl - 21 Feb 2005

Kommentare werden am besten in folgender Form vorgenommen, damit sie im Inhaltsverzeichnis angezeigt werden (natürlich ohne das <verbatim>):
---+++ Main.??? - 14 Jul 2003 - Betreff

UtilFaqSubForm edit

Titel Überprüfen, ob eine Referenz geblesst ist
Autor ReneeBaecker
Bereich FaqOOP
Topic revision: 2005-02-21, ChristianDuehl
 
Bitte die NutzungsBedingungen beachten.
Bei Vorschlägen, Anfragen oder Problemen mit dem PerlCommunityWiki bitten wir um WebBottomBarExample">Rückmeldung.