foreach $file(readdir DH) { $name = $file; next if $name =~ /^\./; # skip over dot files &addheader($_); }
Style issues: there's no need to copy $file to $name. There's also no need to use the ampersand when calling functions. It'd be a bug if you were passing $_ to addheader() when you really mean $file, but you're lucky (in a dubious sense) to be using globals.
sub addheader { open FH, "< $indir/$file" or printf "can't open %s\n",$file; $line = <FH>; $_ = $line;
You're ignoring the passed-in variable. You're attempting to read from a file if it doesn't open (where you should be dieing or at least not continuing). You're assigning to $_ unnecessarily.
$field1 = m/^Clock/; # Check for Clock at beginning of file printf "\n$file"; if ( $field1 == "1"){
You're performing an assignment, not a regex match. Use =~ instead. (That one trips me up occasionally too.) You also probably don't want to do a numeric comparison against a string. Either use eq, or drop the quotes around 1. Also, $field1 comes from nowhere. It only appears in a previous line, and it definitely will never contain what you think it should sometimes contain.
my @lines = <FH1>; foreach $line ( @lines ) { print FH2 $line; }
File::Copy will do this for you much better, with much less code.
$oldfile = 'c:/public/final/$file';$file won't interpolate in single quotes.
That should get you started. If you fix your variable passing, you'll be a lot further; then you can add strict and warnings to have perl catch many of these for you.
In reply to Re: File prepend , copy, & rename
by chromatic
in thread File prepend , copy, & rename
by mgolini
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |