[Chicago-talk] filehandles? what am I doing wrong?

Don Drake don at drakeconsult.com
Sun Dec 23 08:40:03 PST 2007


The date is setup only once at the beginning of the script, so you  
only get one file.

Put this in the while loop:
my ($yr, $mo, $day, $hr, $min, $sec) = (localtime)[5,4,3,2,1,0];
my $date = $sec . $min . $hr . $day . ($mo + 1) . ($yr + 1900);

-Don

On Dec 23, 2007, at 10:31 AM, Richard Reina wrote:

> I have written a short script that reads data from a file and  
> attempts to separate the data into separate a file when it sees  
> ####### and !!!!!!! as delimiters.  The problem I am having is that  
> the script opens writes and closes the first file with no problem  
> but not additional new files for the rest of the occurrences of
> ####### and !!!!!!!
>
> #!/usr/bin/perl -w
> # test_file_writer.pl
>
> open(DEV, "<test_FILEEEEEE");
>
> # get a nice date string for naming an email file
> my ($yr, $mo, $day, $hr, $min, $sec) = (localtime)[5,4,3,2,1,0];
> my $date = $sec . $min . $hr . $day . ($mo + 1) . ($yr + 1900);
>
> while($_ = <DEV>) {
>
>   print $_;        # show the output)
>
> if ($_ =~ /#######/) {
>
>       #this is the beginning of a new file
>       print "THIS IS THE BEGINNING OF A NEW FILE\n";
>       $write_file = "y";
>       open(NEW_FILE, ">NEW_FILE" . $date) || die "CANT OPEN  
> NEW_FILE: $!\n";
>
> }
>
> if ($write_file eq "y") {
>
>       print NEW_FILE $_;
>
> } #end of if
>
> if ($_ =~ /!!!!!!!/) {
>
>       print "Closing File\n";
>       close (NEW_FILE) || die "CAN'T CLOSE NEW_FILE: $!";
>       $write_file = "n";
>
> } # end of if
>
> } # end of while
> ## END
>
> Additionally confusing is that it does not die on open or close like  
> one would think if it were unable to open and close.
>
> Here the input file (test_FILEEEEEE):
>
> #######
> richard at rushlogistics.com
> this is the body of a file
> !!!!!!!
> #######
> richard at rushlogistics.com
> this should be seprate file
> !!!!!!!
> #######
> richard at rushlogistics.com
> this should be another serperate file
> !!!!!!!
> #######
> richard at rushlogistics.com
> this should be another file
> !!!!!!!
> #######
> richard at rushlogistics.com
> and this just another
> !!!!!!!
>
> After the program executes I end up with only 1 new file like  
> NEW_FILE267523122007 when the script should create 5 of them.
>
> Any ideas as to where I've gone wrong would be greatly appreciated.
>
> Thanks,
>
> Richard
>
>
> Your beliefs become your thoughts. Your thoughts become your words.  
> Your words become your actions. Your actions become your habits.  
> Your habits become your values. Your values become your destiny. --  
> Mahatma Gandhi_______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk



More information about the Chicago-talk mailing list