CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
OO Perl question

 



Alexey
Novice

Dec 18, 2007, 2:46 PM

Post #1 of 2 (520 views)
OO Perl question Can't Post

I am trying to create an object by passing a hash as an argument. That hash already has proper keys and velues for my object. In my constructor i want to take that hash and use it as internal datastructure to store data. Here is my constructor:

Code
package Myobject; 
use strict;

sub new {

my $Class = shift;
my %self = @_;

bless (%self, $Class);
return %self;
}

When i create my object:

Code
$obj1 = new Myobject(%params);

I get the following error:
Can't bless non-reference value at .....

What am i doing wrong?

I have done something like:

Code
my $self = { 
_var1 => $_[0],
_var2 => $_[1],
_var3=> $_[2],
_var4 => $_[3],
};

and it worked before, but wanted to have a hash passed with the variables already filled in for me instead of locking things down to a certain array indexes.
Any advise would help.
Thanks


seltzer
Novice

Dec 19, 2007, 10:06 AM

Post #2 of 2 (517 views)
Re: [Alexey] OO Perl question [In reply to] Can't Post

you should use $self (hash reference) not %self hence the non-reference error. In otherwords:

package MyObject;

use strict;

sub new {

my $class = shift;

my %params = @_;

$self=%params;

bless ($self,$class);

return $self;

}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives