in reply to Re: Help with efficiency
in thread Help with efficiency

My problem is that I have an unknown number of "sheets" or objects that I am trying to create. I want to create different graphic files for each object. What I am using now works, but I use the command prompt to tell the object information where to go, graphics.pl > im.png . I do not know how to adjust where the object information will go to so that it can change with different objects. Thanks for your help

Replies are listed 'Best First'.
Re: Re: Re: Help with efficiency
by jarich (Curate) on May 13, 2004 at 04:48 UTC
    What I am using now works, but I use the command prompt to tell the object information where to go, graphics.pl > im.png
    Instead of printing your single object to STDOUT, you'll need to print out each object inside your code.

    This will result in code roughly like the following:

    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

Re: Help with efficiency
by Abigail-II (Bishop) on May 13, 2004 at 01:43 UTC
    Well, just as I said, open the files you want to write to in your program, and write to them.

    Abigail