One important thing to remember is that sweeping s/this/that/ functions can have unintended results. For example john.foo@there.com will become john.bar@here.com which may not be the desired result. I highly recommend doing a SEARCH first, printing lines where matches are found (along with the intended changes), and only after an eyeball scan to make sure we are not going to get bitten do the REPLACE. You really need to identify what will change as in big data sets you may be forgetting something rather critical. Worse it may take weeks to discover the error by which time other changes often make resorting to backup (you did make a BACKUP didn't you?) impractical. This advice is based on having been bitten before. Consider:

while(<DATA>){ my $orig = $_; if ( s/foo\@there\.com/bar\@here\.com/ ) { print "< $orig> $_\n"; } } __DATA__ foo@there.com john.foo@there.com foo@there.com.au <a href="mailto:foo@there.com">Mr Foo</a>

Which generates:

< foo@there.com > bar@here.com < john.foo@there.com > john.bar@here.com < foo@there.com.au > bar@here.com.au < <a href="mailto:foo@there.com">Mr Foo</a> > <a href="mailto:bar@here.com">Mr Foo</a>

Often it can be as simple as adding a boundary condition \b but in this case it does not work (carefully chosen examples :-). I suggest this sort of approach

s{([a-zA-Z_\.]+\@[a-zA-Zz\.\-]+)} {$1 eq $old ? $new : $1}ge

In this approach we find email address like tokens, see if they are what we want to change..... Note this *still* leaves you with the issue of Mr Foo now linking to presumably Mr Bar's address. This may or may not be an issue. The initial scan phase let's you *see* if it is an issue.....

As an aside having email addresses on you web pages is like asking for spam. In this day and age it is worth considering an alernative approach. I would suggest linking to an upload form CGI. The user clicks on an email link like <a href="/cgi-bin/mailer?to=sales>Contact Sales</a> and your CGI provides the email form. This has several advantages. First people can email you without needing a functional email client, it protects your email addresses, and it abstracts the link from sales to sales@mydomain.com into a single config location, removing the prolem you currently have forever. NMS formmail is a reasonably secure canned solution for the back end. An alternative is to use Javascript - there are several techniques.

cheers

tachyon


In reply to Re^2: multi-file search-and-replace by tachyon
in thread multi-file search-and-replace by aplonis

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.