This is a case where there really is no good reason to run a shell command from within your perl script -- especially when the shell command is only going to run another perl script, as a one-liner. That's just way too complicated.

The easy way is to slurp the whole file into a scalar variable, do your regex substitution, and write the string back out to a file. (I'd recommend keeping the original as-is and saving the altered data to a different file, so you can be sure it worked before obliterating the original data.)

Here's one way to do your task:

#!/usr/bin/perl use strict; my $find = '(<!--\s*<control_link_address>push1\.mycompany\.com</contr +ol_link_address>\s*-->)'; my $rplc = '$1\n <control_link_address>mdp.travel.co.uk</control_link_ +address>\n'; open( IN, "<", "/tmp/ls.conf" ) or die "/tmp/ls.conf: $!"; $/ = undef; $_ = <IN>; eval "s{$find}{$rplc}"; open( OUT, ">", "/tmp/ls.conf.new" ) or die "/tmp/ls.conf.new: $!"; print OUT;
I'm using a string eval so that the "$1" will behave as intended. I'm sure there are better ways... (Parsing, maybe?)

In reply to Re: perl regex for xml by graff
in thread perl regex for xml by equick

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.