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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array access in class

Status
Not open for further replies.

AnnaKo

Technical User
Jul 26, 2008
1
IL
Hi all,

Please help me to find the problem. :)

I have docTopGens class:

package docTopGens;
use wt_w;

sub new
{
my($self) = {};
my($type, @array) = @_;
my $self->{'topGenArray'} = \@array;
my $temp = ref($self->{'topGenArray'});
bless($self, $type);
return($self);
}

here $temp = 'ARRAY';

the problem is that in the following method '$self->{'topGenArray'}' contains nothing and $temp ="";

sub get_wt
{
my $doc_g = $_[1];
my $TopGenArray = $self->{'topGenArray'};
my $temp = ref($self->{'topGenArray'});
my $size = @{$TopGenArray};
for($i=0; $i<$size; $i++)
{
$wt_w = $topGenArray->[$i];
$id = $wt_w->getDocID();
if($doc_g eq $id)
{
return $wt_w->getPG_D();
}
}
return 0;

}

the question is how can I access '$self->{'topGenArray'}'
at 'get_wt' function.


Thanks!
 
Perl modules like this are object-oriented. They're good because they don't leave a ton of global variables lying around, so you could have multiple instances of the same class and they'll each have their own sets of values in the variables and not interfere with each other.

Here's a demonstration:

MyModule.pm
Code:
package MyModule;

sub new {
   my $class = shift;

   my $self = {
      favorite_color => 'invisible',
      @_,
   };

   bless ($self,$class);
   return $self;
}

sub show_color {
   my $self = shift;

   print "My fave color is: $self->{favorite_color}\n";
}

sub new_color {
   my ($self,$new) = @_;

   $self->{favorite_color} = $new;
}

and your script.pl
Code:
use MyModule;

# Make a few different instances.
my $default = new MyModule;
my $red = new MyModule;
my $blue = new MyModule;

# Set red/blue's fave colors
$red->new_color ('red');
$blue->new_color ('blue');

print "What is default's color?\n";
$default->show_color;

print "\n";
print "What is red's color?\n";
$red->show_color;

print "\n";
print "What is blue's color?\n";
$blue->show_color;

There's a couple things to note here:

In sub new(), we return a blessed instance of $self. This is the variable that's returned in the "new MyModule" call (so, $default, $red, and $blue are the individual $self's from their respective objects).

When we call a method on the object, like $red->new_color, the subroutine new_color() receives $self again as its first argument. Notice we did "my $self = shift" in the module.

Having said that, if both of the subroutines you mentioned are in the same package, you simply need to receive $self from $_[0] in order to use it. Note that $temp however will NOT be available, because $temp is NOT under the $self structure. Every object variable that you want multiple subroutines to share must be under $self somewhere.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top