On Oct 6, Nath, Alok (STSD) said:

        Here's a simple script that is suppose to read a file and
changes a particular string.

Opening a file for read/write doesn't mean you can do in-place edits just like that. If you want to do simple in-place editing, here's an example:

  {
    local @ARGV = ("Test.txt");  # the files to edit
    local $^I = '.bak';  # a backup extension (use "" for no backup)

    while (<>) {
      
s/Ethernet0\.connectionType(?!_changed)/Ethernet0.connectionType_changed/g;
      print;
    }
  }

I made two changes to your code. First, there's NO good reason to write code like:

  if ($str =~ m/this_pattern/) {
    $str =~ s/this_pattern/that_string/;
  }

You should just write

  $str =~ s/this_pattern/that_string/;

The other change is that the regex makes sure that it doesn't change "Ethernet0.connectionType_changed" to "Ethernet0.connectionType_changed_changed" by making sure that the "Ethernet0.connectionType" is not followed by "_changed" -- that's what the (?!_changed) part of the regex is doing.

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to