Well, this line is really wrong:
print NEW_FILE s/foo/bar/g;
The "s///" operator will return true or false (1 or 0), and that is what the "print" function will get. Your output file will have a "0" or a "1" for each line of the input file, instead of the actual data.

Also, you're likely to run this script more than once (since you probably won't get everything right the first time), so you probably want the edited versions of files to be in a different directory (keep the source files and source directory unchanged), and you surely don't want to open the output files with  ">>$filename".

Since perl gets used for this sort of thing a lot, there are a lot of short-cuts to make it easier. You could do the edit like this:

#!/usr/bin/perl use strict; # suppose original files are in directory "source", and # we want to put edited versions into directory "edited": my @files = <source/*>; # that's called a "file glob" for my $ifile ( @files ) { my $ofile = $ifile; $ofile =~ s/source/edited/; open( I, "<", $ifile ); open( O, ">", $ofile ) or die "$ofile: $!"; while (<I>) { s/foo/bar/g; print O; } close I; close O; }
As for the FTP part, if the remote machine is running an anonymous-ftp server, and the files in question are available that way, then Net::FTP is fine. But if you need to use a particular (non-anonymous) user account and a password, you really should use Net::SFTP.

I would recommand that you keep the (S)FTP stuff in a separate script from the editing stuff. (It's actually more likely that you don't need a perl script at all to do the file transfers between machines, but if you want to make up a perl script to "reinvent" the existing (s)ftp programs, go ahead.)

The point is that you can write a re-usable file-transfer script that will be handy for lots of occasions (assuming the standard tools are less handy), and you can even write a re-usable editing script for making changes to a list of files, which will be handy for lots of occasions (it is possible to provide a list of regex substitutions as an input). Both of these things are relatively easy.

But if a script is going to do both file transfers and editing, that one script will be larger and more complicated and will take longer to write; and making it re-usable in any practical sense is going to be a lot more difficult.


In reply to Re^3: FTP and update files from a list by graff
in thread FTP and update files from a list by lev36

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.