passing objects between subroutines(perl)?

  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

how can i pass an object which is instantiated like:
my $object=Mail::Folder->new(x,y);

i want to pass $object from one sub. to another subroutine.
how can i do that in perl??
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

I'm not quite sure about what I am going to say. But you may want to try it.

The best way to pass objects is by reference, so try passing a reference to the object. In C++, after using the "new" keyword, all you have is a reference to the object, therefore you only pass references in C++. The same goes for Java.

Try passing the $object variable, all you need to know when programing your subroutine is which class the object is...
  • Managedlinks
  • Proficient
  • Proficient
  • Managedlinks
  • Posts: 294

Post 3+ Months Ago

in PERL objects are all references (think pointers)
so to pass an object to a subroutine simply use it as a parameter
eg:
my $object=Mail::Folder->new(x,y);

dosomething($object);

dosomething would(could) look like this
sub dosomething {
my $folder = shift;

$folder->delete();

}
Remember when using the indirection operator -> the first parameter is always the object itself e.g.
$object->objectmethod('data')
sub objectmethod {
my $this = shift; # or $me etc..
my $otherparam = shift; # data

use $this to access other object methods and variables

}
  • akaash
  • Beginner
  • Beginner
  • akaash
  • Posts: 62
  • Loc: Gujurat,India

Post 3+ Months Ago

Thanks a lot it is working ..

but i want to call the subroutine on some button or image click on the form then how can i do that? so that i can call dosubroutine($object) on that button or image click.

how can i do this?
  • Managedlinks
  • Proficient
  • Proficient
  • Managedlinks
  • Posts: 294

Post 3+ Months Ago

Your crossposting

see your other thread

then use the parameter passed in as the identifier


.....

if ($param eq 'dothis')
{
dothis($object);
}
elsif ($param eq 'dothat')
{
dothat($object);
}
else
{
print 'error no parameter';
}

.....

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 62 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.