Home: Perl Programming Help: Intermediate:
Multiple GD Objects



Sam Kennedy
Novice

Aug 28, 2012, 6:39 AM


Views: 2558
Multiple GD Objects

I'm working on some code which handles 10 images. To create an image object with GD you use:


Code
$im1 = new GD::Image($width,$height,1);


However, since I'm using 10 images, I don't want to write:

Code
$im1 = ... 
$im2 = ...
$im3 = ...


So I used a for loop and:

Code
${"im".$i} = new GD::Image($width,$height,1);


Where $i would be the image number, however this doesn't work.

What would be a better way of handling multiple GD objects?

Thank You :)
-Sam


FishMonger
Veteran / Moderator

Aug 28, 2012, 6:44 AM


Views: 2556
Re: [Sam Kennedy] Multiple GD Objects

Use an array.


Code
my @image; 
for (1..10) {
push @image, GD::Image->new($width,$height,1);
}



FishMonger
Veteran / Moderator

Aug 28, 2012, 6:57 AM


Views: 2553
Re: [Sam Kennedy] Multiple GD Objects

I should also point out that you could use a hash, if you wanted to refer to them by name.

Code
my %image; 

foreach my $name ( qw(foo bar bazz) ) {
$image{$name} = GD::Image->($width,$height,1);
}



Sam Kennedy
Novice

Aug 28, 2012, 11:28 AM


Views: 2548
Re: [Sam Kennedy] Multiple GD Objects

Thank you very much, it's sorted now :)