473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 19074
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

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

Similar topics

1
by: Asyn lordasyn | last post by:
Ok... I know there is got to be something simple that I forgot, but I for the life of me can't find it. Please, for the sake of the wall I keep slamming my head into, any insight would be helpful. ...
2
by: Jozef | last post by:
Hello, I have a friend in the UK and another in Denmark that I sent an Access XP database to. I used the Package and Deployment wizard to create this file. The only reference that I added was...
16
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
1
by: Paul S. Natanson | last post by:
What is a Null Reference error and how do I fix it? My newly installed VB.Net2003 gives me a "Microsoft Development Environment" error message box EVERY time I try to run/start ANY project -...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
40
by: vfunc | last post by:
I get an undefined reference error for a definition that clearly looks like it is in a .h file. So what can be the cause of an undefined reference error, when this .h file containing the...
2
by: Dikbill | last post by:
Hi there, I've got a Windows project with references to an ocx file. When I run the executable and the referenced ocx isn't available I get a Windows error. How can I trip a reference error...
9
by: tonym415 | last post by:
I think this is a reference error. All of a sudden my database doesn't know what a "control" is. I have written forms and I get a "Module Not Found" error. The line that is highlighted is a sub...
5
by: MattB | last post by:
I'm relatively familiar with asp.net and vb.net and have been working in the environment for several years. I've dealt with null reference errors before and generally know how to code around them...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.