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!

Value of array in a package

Status
Not open for further replies.

rubis

Programmer
Jun 21, 2001
54
GB
Hi all,

I have my own package which has one array as a global variable. In my main pgm, I use a "for" loop to create objects.


package.pm

our @array;

sub new {
my ($self, $a);
push (@array, $a);
}

main.pl

for ($i=0; $i<=3; $i++) {
$x[$i] = package->new($i);
}

print @x;


The value of @array will not be clear up when I call &quot;new&quot; method. I think that the value of @array will be cleared once I call &quot;new&quot;.. like create two different objects.
Any suggestion?

 
What do you want the array @x to contain? The way you have it now, $x[$i] contains the return value of [tt]push(@array, $a)[/tt], which is the new number of elements in @array, not the values in @array. To store those values in @x makes no sense (to me, anyway, maybe you have a good reason).

I think that the value of @array will be cleared once I call &quot;new&quot;.. like create two different objects.

Could you give a better description of what you are trying to do? The way you have coded it, there is no easy way to access the values of @array (unless you have written a method to do it). Are you trying to make this object oriented?

jaa
 
I concur, a better description of what you are attempting is needed.

It does look like an attempt at object-oriented-ness, based on the seemingly useless inclusion of a variable called 'self', a subroutine, used in congruence with common OO practice, called 'new'.

Try making these mods in the code:

#=========Package.pm======================
package Package;

our @array_of_objects;
# Array here is used as internal record
# of objects.

sub new {
my $class = shift; # always first arg here
my $number = shift; # the arg explicitly passed
push (@array_of_objects, $number);
return bless \$number, $class;
}

1; # Needed, enjoy the err without it.
#========================================
#==========main.pl====================
use Package;

for ($i=0; $i<=3; $i++) {
$x[$i] = new Package($i);
}

print @x; # displays ref-mem addresses

print $$_ . &quot;\n&quot; for @x;
# Displays the single property of each object
#=====================================

I think that this rewrite of your code 'does' what you were trying to do. But I don't know exactly what you were doing. Is that possible?
The above code is rather useless. Definitely need more info.

&quot;Throw me a figgen bone here, I'm the boss, need the info...&quot; -- Dr. Evil

--jim
 
I try to make it OOP. What I want to do is I want to create an array of objects (@x in main) from &quot;Package.pm&quot;. Each object has its own array which has different values (variables of objects). My problem is when I create an array (@a in the code above) in Package.pm but everytime that I call &quot;new&quot; in order to create a new object ($x[$i]) which the values of all variables should be clear but the value of @a isn't cleared. The pgm keeps pushing the new values into the array (@a).

If the first time execution of for loop, the value in @a is [1,2] in $x[1]. Then the 2nd time, the new value is [3,4], the final value of @a will be [1,2,3,4] in $x[2] rather than [1,2] in $x[1] and [3,4] in $x[2].

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top