469,330 Members | 1,317 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

What is an unblessed reference error?

47
How can I fix the error "Can't call method "id" on unblessed reference" for the following code?

Expand|Select|Wrap|Line Numbers
  1. sub language{
  2.     my $self = shift;
  3.     if (!$self->{LANGUAGE})
  4.     {
  5.         my @language = ();
  6.         my $statement = $db->prepare("select  languageId, languageName
  7.                                         from language");
  8.         $statement->execute();
  9.  
  10.         while(my @array = $statement->fetchrow_array()){
  11.             my ($id , $name) = @array;
  12.             my $object = Language->new();
  13.             $object->id($id);
  14.             $object->name($name);
  15.             push (@language, $object);    
  16.         }            
  17.         $self->{LANGUAGE} = \@language;
  18.     }
  19.     return $self->{LANGUAGE};
  20. }
  21.  
  22. my @languagetest = language();
  23.  
  24.             foreach my $current(@languagetest){
  25.             my $id = $current->id();
  26.             my $name = $current->name();
  27.             print "RETURN ID: [$id]  Name: [$name] \n";
  28.             }
Feb 25 '11 #1

✓ answered by miller

Yes, because $current is not an object. You should know what type of data structure $current is, but if you don't use Data::Dumper

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. my @languagetest = language();
  4.  
  5. foreach my $current (@languagetest) {
  6.     print Dumper($current);
  7.     my $id = $current->id();
  8.     my $name = $current->name();
  9.     print "RETURN ID: [$id] Name: [$name] \n";
  10. }

4 17372
miller
1,089 Expert 1GB
You do not show the code where the problem occurs. You create an object there:

Expand|Select|Wrap|Line Numbers
  1. my $object = Language->new();
  2.  
Therefore in the new method of the Language class, you should be returning a blessed data structure as is required in perl OOP (Object Oriented Prgramming). Where's your new method? are you blessing a hash or such?

perldoc: perltoot - Tom's object-oriented tutorial for perl

- Miller
Feb 25 '11 #2
Wiinie
47
I try using foreach to print out the result
Expand|Select|Wrap|Line Numbers
  1. # my @languagetest = language();
  2. #  
  3. #             foreach my $current(@languagetest){
  4. #             my $id = $current->id();
  5. #             my $name = $current->name();
  6. #             print "RETURN ID: [$id]  Name: [$name] \n";
  7. #             }
  8.  
and it gives me an error of unblessed reference when i call the method id() again.. please help me to solve this problem. or does it have another to print out the result.
Feb 25 '11 #3
miller
1,089 Expert 1GB
Yes, because $current is not an object. You should know what type of data structure $current is, but if you don't use Data::Dumper

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. my @languagetest = language();
  4.  
  5. foreach my $current (@languagetest) {
  6.     print Dumper($current);
  7.     my $id = $current->id();
  8.     my $name = $current->name();
  9.     print "RETURN ID: [$id] Name: [$name] \n";
  10. }
Feb 25 '11 #4
Wiinie
47
Thank so very much for the above answer.
And I have another question as well. I did use the selectall_array_ref to execute this query. and when I try to print in the sub nextWords. it does work and prints out all the result that i have expected.


Expand|Select|Wrap|Line Numbers
  1. sub nextWords {
  2.     my ($id, $offset) = @_;
  3.     my @array = ();
  4.     my $statement = $db->selectall_arrayref("select word from words 
  5.             left outer join language
  6.             on words.languageId = language.languageId
  7.             where words.languageId = $id
  8.             order by word asc
  9.             limit 10 offset $offset;");
  10.  
  11.     my @main= ();
  12.     foreach my $i (@$statement)
  13.     {
  14.         push(@main, @{$i}[0]);
  15.     }
  16.     @array[$id] = [@main];
  17.  
  18.     return  @array; 
  19. }    
  20.  
  21. my $lang_size = languages();
  22. for my $id(1 .. $lang_size ){
  23.  
  24.     print "=======ID = " . $id . "=======\n";
  25.     my $self->{CURRENTOFFSET}  = 0;
  26.     NAME: while (1 > 0){
  27.         my @newwordsList = nextWords($id,$self->{CURRENTOFFSET});
  28.  
  29.         $self->{CURRENTOFFSET}+= 10;
  30.  
  31.         foreach my $k (@{@newwordsList[$id]}) {
  32.             print $k . "\n";
  33.         }
  34.         print "\n";
  35.         if (!@{@newwordsList[$id]}[0]) {
  36.             last NAME;
  37.         }
  38.     }
  39. }
However, when i try to print out by create and other file.pl to compile it. it doesnt work, it give an error "DBD::SQLite::db selectall_arrayref failed: unrecognized token: "0x1db16c".

Expand|Select|Wrap|Line Numbers
  1. $object = DataLayer->new();
  2. print $object->nextWords();
  3.  
  4. for my $id( 1 .. 3)
  5.     {
  6.         my $offset= 0;
  7.         NAME: while (1>0)
  8.         {
  9.             my @wordList = $object->nextWords($id, $offset);
  10.             $offset+=10;
  11.             foreach my $k (@{@wordList[$id]}) {
  12.             print $k . "\n";
  13.             }
  14.             if (!@{@wordList[$id]}[0]) {
  15.             last NAME;
  16.             }
  17.         }    
  18.     }
  19.  
thanks in advance..
Feb 25 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Paul S. Natanson | last post: by
1 post views Thread by Paul S. Natanson | last post: by
40 posts views Thread by vfunc | last post: by
2 posts views Thread by Dikbill | last post: by
9 posts views Thread by tonym415 | last post: by
5 posts views Thread by MattB | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.