use strict;
use warnings;
my $i = 1; # The object number we're up to
# create object 1 somehow
open(FILE, ">", "object$i.png") or die "Failed to open object$i.png: $
+!";
# make sure we are writing to a binary stream
binmode FILE;
# Convert the image to PNG and print it to file
print FILE $im->png;
$i++;
# create object 2 somehow, rinse and repeat
You may put all of that in a loop, or you may create an output subroutine, you may even choose to do all the printing at the end... But you're going to need to do the printing to files inside your program rather than capturing STDOUT and redirecting that to a file.
Keep in mind that using open with > will result in any previous data in the file called "object$i.png" to be overwritten. You can use the following test if this might be a problem:
my $filename = "object$i.jpg";
if(-e $filename) {
die "$filename already exists in this directory\n";
# or something more useful.
}
open(FILE, ">", $filename) or die "Failed to open $filename: $!";
I hope this helps
jarich |