develooper Front page | perl.perl5.porters | Postings from June 2004

[perl #30407] Can't invoke methods on blessed objects stored as hash keys

From:
Kevin Killourhy
Date:
June 22, 2004 10:08
Subject:
[perl #30407] Can't invoke methods on blessed objects stored as hash keys
Message ID:
rt-3.0.9-30407-91167.3.24247847008522@perl.org
# New Ticket Created by  Kevin Killourhy 
# Please include the string:  [perl #30407]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org:80/rt3/Ticket/Display.html?id=30407 >




This is a bug report for perl from ksk@cmu.edu,
generated with the help of perlbug 1.28 running under perl v5.6.0.


-----------------------------------------------------------------
[Please enter your report here]

I have a package that stores a reference of every blessed
object.  It keeps this reference as a key in a hash.  However,
when I then access those keys, they seem to lose their
"blessing" to the package.  Here's a short example with a package
file Cow.pm and a perl script farm.pl.  The function MooInUnison()
fails because perl can't resolve moo() as a method of the Cow
package when the blessed object is stored as a hash key and
retrieved with keys.

------------
$ cat Cow.pm
package Cow;

use warnings;
use strict;

my %Herd = ();

sub new() {
    my $cow = {};        # make a new cow object
    bless $cow, 'Cow';   # bless it as a cow
    $Herd{ $cow } = 1;   # make it a herd member
    return $cow;
}

sub moo( $ ) {
    print "Moooo!\n";    # make the cow moo
}

sub MooInUnison() {
    foreach my $cow ( keys %Herd ) {
	# moo( $cow );   # this would works
	$cow->moo();     # this fails
    }
}

1;
------------
$ cat farm.pl
#!/usr/bin/perl

use warnings;
use strict;
use Cow;

printf "Using perl '%s'\n", $];

my $firstcow = Cow::new();
my $secondcow = Cow::new();
my $thirdcow = Cow::new();

$firstcow->moo();     # this will work
$secondcow->moo();    # this will work
$thirdcow->moo();     # this will work

Cow::MooInUnison();   # this will fail
------------
$ ./farm.pl
Using perl '5.006'
Moooo!
Moooo!
Moooo!
Can't locate object method "moo" via package "Cow=HASH(0x80f8a20)" at Cow.pm line 22.
------------

I also tested this with perl v5.6.1 with the same result.

I didn't find any currently open perlbug tickets which
described this problem.

Thanks for your help.

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags:
    category=core
    severity=low
---
Site configuration information for perl v5.6.0:

Configured by prospector at Fri Mar 23 12:48:14 EST 2001.

Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.2.17-8smp, archname=i386-linux
    uname='linux porky.devel.redhat.com 2.2.17-8smp #1 smp fri nov 17 16:12:17 est 2000 i686 unknown '
    config_args='-des -Doptimize=-O2 -march=i386 -mcpu=i686 -Dcc=gcc -Dcccdlflags=-fPIC -Dinstallprefix=/usr -Dprefix=/usr -Darchname=i386-linux -Dd_dosuid -Dd_semctl_semun -Di_db -Di_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Uuselargefiles'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc='gcc', optimize='-O2 -march=i386 -mcpu=i686', gccversion=2.96 20000731 (Red Hat Linux 7.1 2.96-79)
    cppflags='-fno-strict-aliasing'
    ccflags ='-fno-strict-aliasing'
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -ldl -lm -lc -lcrypt
    libc=/lib/libc-2.2.2.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:


---
@INC for perl v5.6.0:
    /usr0/ksk/usr/perl/site_perl
    /usr0/ksk/usr/perl
    /usr/lib/perl5/5.6.0/i386-linux
    /usr/lib/perl5/5.6.0
    /usr/lib/perl5/site_perl/5.6.0/i386-linux
    /usr/lib/perl5/site_perl/5.6.0
    /usr/lib/perl5/site_perl
    .

---
Environment for perl v5.6.0:
    HOME=/usr0/ksk
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/usr0/ksk/usr/lib:/usr0/ksk/usr/i386_rh71/lib:/lib:/usr/lib:/usr/local/lib:/usr/X11R6/lib
    LOGDIR (unset)
    PATH=/usr0/ksk/bin:/usr0/ksk/usr/bin:/usr0/ksk/usr/i386_rh71/bin:/bin:/usr/bin:/usr/local/bin:/usr/contributed/bin:/usr/openwin/bin:/usr/ccs/bin:/usr/xpg4/bin:/usr/ucb:/sbin:/usr/sbin:/usr/local/sbin:/usr/X11R6/bin
    PERL5LIB=/usr0/ksk/usr/perl/site_perl:/usr0/ksk/usr/perl
    PERLLIB=/usr0/ksk/usr/perl/site_perl:/usr0/ksk/usr/perl
    PERL_BADLANG (unset)
    SHELL=/usr/bin/bash-log




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About