>>>>> "AM" == Abimael Martinez <abijr....@gmail.com> writes:

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



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

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


  AM>   &add_div("content", $doc_name);

don't call subs with &. it is old perl4 style and can cause problems.

  AM> }

  AM> sub add_div {
  AM>   # Get the stuff 

make your comments useful. that doesn't say anything beyond what the
code is doing already.

  AM>   my $div_id = shift @_;
  AM>   my $file_name = shift @_; 

  AM>   # Create file handle
  AM>   my $file = new IO::File;
 
  AM>   # Open a temporary file handle
  AM>   my $tmp = new_tmpfile IO::File;
 
why are you using IO::File for this? just open the file with open and
such. this is overkill and causing you confusion.

  AM>   # Open the document
  AM>   $file->open ("+< $file_name"); 
  AM>   print "Couldn't open $file_name" unless (defined $file);

that open cannot change $file to make it undef so the test will never
work. again, if you just did a simple open you wouldn't fall into this
confusion.

  AM>   print {$tmp} "$div_start";

no need for the {} around a single scalar handle.  also no need (and it
can be wrong) to quote a scalar variable. it also causes an extra
useless copy to be done.

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

  AM>   print {$file} <$tmp>;

you can't write to the original file since you haven't done a seek() to
change the i/o direction.

this can all be done with File::Slurp in a few lines. this is untested
and with no outer loop or such.

use File::Slurp ;

my $text = read_file( $file ) ;
write_file( <<HTML ) ;
<div id=$div_id>

$text

</div>
HTML



much simpler, faster and cleaner. use the right tools for the job.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
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