Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object oriented Perl

Status
Not open for further replies.

r2d22y

Technical User
Nov 30, 2004
46
SE
Hello

I wonder.... I'm Java developer and would like to use some "objects" in my application. I have 3 questions that I would be very happy if anyone could help me to explain so I understand.

Lets say I have an object called Person.

In my application I do:

.
use Person; #Defined and is in the same directory as other files...Person.pm
.
.
my $person=Person->new($user,$nr); # $user and $nr unique for current user.
.
.

know I want to store this object in an vector, in java I would have done something like

myvector.addElement(person); # person= $person

and for retrieving my person I would have done

Person p1 =(Person) myvector.getElementAt("position where my object are stored");

I have tried to do like this in Perl but get some errors

push(@myvector,$person);

and

pop(@myvector);

so...

1) How do I store objects in a datastructure like above
2) How can I access the data in this case I would like to go through the hole vector and get all objects of type Person.
3) Lets say I'm using a session handler how do I store/retrieve an Object in a session.

Regarding sessions I have been using:
.
.
use CGI::Session;
my $session = new CGI::Session($sid); #$sid=sessionId
my $usr= $session->param("idkey");
.
.

Thanks in advance!

/MM
 
I don't see why you should get errors.
Show us some code and the errors.



(very injured) Trojan.
 
Hello

There was a while ago I made this and I dont remember the error exactly. I solved it with some another session variables etc. But I would really like to know how to achieve what I explained above.

Lets say I do
.
use Person;
.
.
my $person=Person->new($idnr,$nr);
$person->setInfoaboutperson("someinfo");
push(@myvector,$person);
.
.
.
my $p2=Person->new($uis,$nr);
$p2=pop(@myvector);

$p2->getInfoaboutperson();
.
.


You think this should work?
I dont have to typecast anything?

Thanks!

/MM
 
Yes, but poping @myvector to $ps overwirites the new person you just created.
No, you don't have to typecast anything.




(very injured) Trojan.
 
Ok thanks alot.

Is it the same with using sessions? Can I store an object as a sessionvariable and the be able to access data for current object

Like:
use CGI::Session;
use Person;
.
my $session = new CGI::Session($sid); #$sid=sessionId
my $person=Person->new($user,$nr);
$person->setInfoaboutperson("someinfo");
$session->param("activeuser",$person);
.
.
my $p2=$session->param("activeuser");
$p2->getInfoaboutperson();
.
.

Thanks again!

/MM
 
Yes,
In perl, objects are objects.
The scalar you are returned from the "new" is a blessed reference and so an array will happily store any kind of references.




(very injured) Trojan.
 
Hello

Ok, thank you for your help!

Best Regards //MM
 
Trojan - you're injured?

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Yep,
I chopped my thumb up on my table saw whilst chopping up wood.
It's not a pretty sight. :-(




(very injured) Trojan.
 
I was resizing a kitchen cabinet door since it was too large to fit the cabinet it was being moved to.





(very injured) Trojan.
 
Carefull there oldtimer, leave the power tools to the sighted (and the sober)! [wink]



*** I'm darn near over-the-hill myself but still have the one good eye. [3eyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top