dimmesdale has asked for the wisdom of the Perl Monks concerning the following question:

I keep getting the error message: "Can't call method label on unblessed reference at ..."

The trouble is I'm almost positive that the reference IS indeed blessed. I double checked (and triple and etc.) all the relevant code, and when that didn't produce anything wrong, I used Data::Dumper and the Dumper method even reported that the reference was blessed! Here's the stuff, cut down, of course:

my $tableau; for my $p(0..6) { $tableau->[$p] = Pile->new({ max => 52 }); for (1..$p) { my $card = $deck->GetRandCard(); $card->state('down'); $tableau->[$p]->fill_pile($card); } my $card = $deck->GetRandCard(); $card->state('up'); $tableau->[$p]->fill_pile($card); } ... my $tab_imgs; my $i = 1; for(@$tableau) { ## Here's the line that's causing the problems -> my $c = ($_->cards())[0]->label(); my $photo = $canvas->Photo(-file => "$card_dir\\$c.$ext", -format => 'gif'); push @$tab_imgs, $canvas->createImage(50+$i++*50,500, -image => $pho +to); } # Stuff in pile.pm (tableau calls # new,fill_pile,get_rand_card) sub new { my $proto = shift; my $self = shift; my $class = ref($proto) || $proto; $self->{next} = 0; bless ($self, $class); return $self; } sub fill_pile { my $self = shift; my $cards = shift; $self->{cards} = [$cards,$self->{cards}]; } sub GetRandCard { my $pile = shift; my $card = ($pile->shuffle())->[0]; $pile->remove_card; return $card; } # oh, and before I forget, the data::dumper print up # this is of ($_->cards())[0], the thing that tries to call # label, but can't cause it's "not blessed" $VAR1 = [ bless( { 'state' => 'up', 'where' => bless( { 'next' => 0, 'max' => 52, 'cards' => [ bless( { 'state' = +> 'down', 'where' = +> $VAR1->[0]{'where'}, 'value' = +> '1', 'suit' => + 'h' }, 'Card' ) +, ##.. more stuff like this + ] }, 'Pile' ), 'value' => 'q', 'suit' => 's' }, 'Card' ), undef ];

I don't know if this will be enough, but I don't know what else may help short of giving a lot of code.

edited: Sat Jul 6 16:22:40 2002 by jeffa - title change (was: Help with error)

Replies are listed 'Best First'.
(jeffa) Re: Help with unblessed object reference error
by jeffa (Bishop) on Jul 06, 2002 at 16:41 UTC
    Hmmmmm, a shot in the dark: i really don't like the way you are getting to the label() method with:
    for (@$tableau) { my $c = ($_->cards())[0]->label(); ... }
    Try using transitory variables to see which part is not working, something like:
    for my $tabl (@$tableau) { my @deck = $tabl->cards(); my $card = $deck[0]; my $label = $card->label(); }
    might help find the bug. This way you can pinpoint each 'link in the chain'.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Help with unblessed object reference error
by chromatic (Archbishop) on Jul 06, 2002 at 16:55 UTC

    Does cards() return a list or an array reference? I'll guess that it's the latter, and you actually need:

    my $c = $_->cards->[0]->label();
Re: Help with unblessed object reference error
by ehdonhon (Curate) on Jul 06, 2002 at 19:06 UTC

    If that Data::Dumper you are providing at the end really is a dump of ($_->cards())[0], then if you want to call the label method on the card object, you'd need to call it this way:

     ($_->cards())[0]->[0]->label()

    Look closely at the dump, and you'll see that what you are getting back is:

     [ bless( {...}, 'Card' ), undef ] 
Re: Help with unblessed object reference error
by RMGir (Prior) on Jul 06, 2002 at 16:32 UTC
    I guess what we'd really need to see is the definition of method cards().

    You've got hash elem cards in here ($_->{cards}), but I don't see sub cards().

    Either that, or I'm completely lost, as usual :)
    --
    Mike

Re: Help with unblessed object reference error
by dimmesdale (Friar) on Jul 06, 2002 at 16:56 UTC
    UPDATE:
    Of course! Thanks chromatic.

    Okay. Here's cards method from pile.pm

    sub cards { my $self = shift; if (@_) { $self->{cards} = shift } return $self->{cards}; }

    I did the split up thing, jeffa, but still same problem (everything works but $card->label();)

    # here's some more code if it helps sub label { my $self = shift; return "$self->{suit}$self->{value}"; } sub remove_card { my $self = shift; splice @{$self->{cards}}, 0, 1; } # Fisher-Yates shuffle sub shuffle { my $self = shift; use integer; my $i; for ($i = int(@{$self->{cards}}); --$i; ) { my $j = int rand ($i+1); @{$self->{cards}}[$i,$j] = @{$self->{cards}}[$j,$i]; } return $self->{cards}; }
      Hmmm. My guess then would be that in the line where you do your $card->label(), $card does not have what you expect. Care to print "card=$card\n" just before the call to ->label()?

      Regards.