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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.