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

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

Kevin Killourhy
June 22, 2004 10:08
[perl #30407] Can't invoke methods on blessed objects stored as hash keys
Message ID:
# New Ticket Created by  Kevin Killourhy 
# Please include the string:  [perl #30407]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
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 and a perl script  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
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

$ cat

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
$ ./
Using perl '5.006'
Can't locate object method "moo" via package "Cow=HASH(0x80f8a20)" at 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]
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:
    osname=linux, osvers=2.2.17-8smp, archname=i386-linux
    uname='linux 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
    cc='gcc', optimize='-O2 -march=i386 -mcpu=i686', gccversion=2.96 20000731 (Red Hat Linux 7.1 2.96-79)
    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/, 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:

Environment for perl v5.6.0:
    LANGUAGE (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)
    SHELL=/usr/bin/bash-log Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About