smallaxe has asked for the wisdom of the Perl Monks concerning the following question:

greets monks,

i am trying to use an array of filenames, and insert the contents of each pseudo-html file between an html header and footer to make a new static html page.

i was originally hopings i could use something like:
print OUTFILE `cat $arraymember`;
but to no avail, so far... then i decided to use the normal file handle approach, by opening the file, iterating through each line in the current file and inserting each line into my new file using a seperate filehandle, and closing each current file each time it loops. the subroutine looks like this:
sub catfile { foreach (@dirarray) { $filepage = shift(@dirarray); #print TOCOUT "$filepage\n"; #debug! unless ($filepage eq '') { open (TEMPFILE, $filepage) or die "cannot open $filepage: $!\n"; #print TOCOUT `cat $filepage`; #i wish! while ($newline = <TEMPFILE>) { print TOCOUT "$newline"; #print "$newline"; #debug! } close (TEMPFILE); } } } # end catfile

what i'm getting is each file for output is being written the entire contents of *every* file in the array, instead of just the contents of one file. i think this is because the array i am iterating over doesn't shift like i want it to, but... there is a debug line where i try to print out the filename after each iteration of the array - this works perfectly as expected - one filename per iteration. please excuse the incredibly messy code - it's in the stages of exasperation. question, why is it opening multiple pages? is there an easier way to do this?

thanks extra muchly in advance....

edited: Wed Oct 16 16:06:01 2002 by jeffa - code tags s/<br>//g

Replies are listed 'Best First'.
Re: special context of file names in array?
by sauoq (Abbot) on Oct 16, 2002 at 02:17 UTC

    It is difficult to decipher exactly what you are trying to do from your description and code. If I understand, then the code below should work. It is most definitely not how I would normally write such a routine but I didn't want to put too much time in as I wasn't even sure it would do what you wanted. So, I used backticks with cat (as you did), slurped in all of the files, and used a minimal amount of error checking.

    It'll take the file names given on the command line and create new files with "new_" prepended to the original names that contain the contents of the file sandwiched between the contents of the files header.txt and footer.txt.

    #!/usr/bin/perl -w use strict; sub add_header_and_footer_to_files { my @files = @_; my $header = `cat header.txt`; my $footer = `cat footer.txt`; for my $filename (@files) { my $file = `cat $filename`; open OUT, '>', "new_$filename" or die "Couldn't open $filename.new for writing: $!\n"; print OUT $header, $file, $footer; } } add_header_and_footer_to_files(@ARGV);

    Good luck!

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: special context of file names in array?
by Enlil (Parson) on Oct 16, 2002 at 01:57 UTC
    I know this is the same thing most people put initially, but have you tried:
    use strict; use warnings;

    Also have you opened the file TOCOUT for writing, anywhere?

    also: I think the code

    foreach my $filepage (@dirarray) {...};

    or
    foreach (@dirarray) { my $filepage = $_; .. }

    instead of the $filepage=shift(@dirarray);

    I hope this helps you along.

    -Enlil