Liz already gave you the reason why it doesn't do what you want: because the s/// updates these variables.

What she didn't tell you, is that you can fix this, by adding a block level, putting the s/// in its own block.

#! perl -w use strict; use warnings; my $str = "Title: Learning Perl Author: Schwartz, Randal L. ISBN: xx +xx"; if ( $str =~ /^Title: (.*)\s+Author: (.*)\s+ISBN: (.*)/ ) { # print "<$1><$2><$3>\n"; my $author = $2; { $author =~ s/\.//g; # Remove full stops (periods) } print "<$1><$author><$3>"; # Line 8 }
which yields:
<Learning Perl ><Schwartz, Randal L ><xxxx>
Ain't that magic.

It's all hidden in that piece of documentation that you quoted:

The scope of $<digit> extends to the end of the enclosing BLOCK or eval string, or to the next successful pattern match, whichever comes first.
and here, because the s/// has its own block, the $1, $2, $3 also have their own limited scope. Outside that block, the outer values are restored. It is indeed as if there's an implicit local applied to them. So your values, the ones you want to show, got restored to the values your pattern match yielded, ready for you to print them.

In reply to Re: Modifying value of $1 clobbers $2, $3 etc? by bart
in thread Modifying value of $1 clobbers $2, $3 etc? by Not_a_Number

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.