>>>>> "Dissdennon" == Dissdennon stupolsky <[EMAIL PROTECTED]> writes:

Dissdennon> hi
Dissdennon> i have a script like this:

Dissdennon> #!/usr/bin/perl

Dissdennon> open (MOO, "loo");


Dissdennon>    while (<MOO>)

Dissdennon>   {
Dissdennon>    s/moo/foo/g;

Dissdennon>    }

Dissdennon> what i'm trying to do is to replace all the moos in a
Dissdennon> file called loo in the same directory where the script
Dissdennon> is. when i run it i don't get any errors but there is
Dissdennon> no results eather. in the loo - which is only a text
Dissdennon> file like this:
Dissdennon> moo

Dissdennon> moo

Dissdennon> moo

Dissdennon> nothing happans. nothing's getting changed...

Dissdennon> why?

You need to actually rewrite the file.  Simply reading it and
performing the substitution changes the line in memory, but there's
no writing the file back.

The simplest is to use inplace editing mode:

#!/usr/bin/perl -pi.bak
s/foo/goo/g;

And invoke this with "loo" on the command line.  Or, if you want
to be more explicit:

    #!/usr/bin/perl
    $^I = ".bak"; # this is like -i.bak
    @ARGV = qw(loo); # this is like "loo" on the command line
    while (<>) {
      s/foo/goo/g;
      print; # this is like -p
    }

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to