Hi,

I am having some problems making this little script work, it is supposed  
glob files from current dir, and add stuff at the top and bottom of such
files. I am also unshure how the "autoflush" works.  



use strict;
use IO::File;
use IO::Handle qw( );

my @web_docs = <*.html>;
my @excluded = qw/ sidebar.html /;      


foreach my $doc_name ( @web_docs ) {
        # This joins all the filenames in a (filename1|filename2|...) form to 
        # match with a regex
        my $re = join '|', map{quotemeta} sort 
{length($b)<=>length($a)}...@excluded;
        # This compiles the regex
        $re = qr/($re)/;

        # Next if $doc matches any of the docs I which to keep unmodified
        next if ($doc_name =~ $re);
        &add_div("content", $doc_name);
}

sub add_div {
        # Get the stuff 
        my $div_id = shift @_;
        my $file_name = shift @_; 

        # Create file handle
        my $file = new IO::File;
 
        # Open a temporary file handle
        my $tmp = new_tmpfile IO::File;
 
        # Open the document
        $file->open ("+< $file_name"); 
        print "Couldn't open $file_name" unless (defined $file);
        
 
        #$file->autoflush(1);
        #$tmp->autoflush(1);
        #autoflush STDOUT 1;
 
        my $div_start = "<div id=" . '"' . $div_id . '">' . "\n\n";

        print {$tmp} "$div_start";
        print {$tmp} <$file>;
 
        my $div_end = "\n\n</div>";     
        
        print {$tmp} "$div_end";

        print {$file} <$tmp>;

        $file->close;
        $tmp->close; 

}

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to