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 #### 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: $!";