Updated code:
#!/usr/bin/perl use strict; use warnings; my $string = 'This is string'; my $capture1 = substr $string, 6, 2, "is stretched"; # This will subst +itute as expected # REALLY? print $string,"\n"; # I get: # "This iis stretchedstring" # perhaps better is: my $string2 = 'This is string'; $string2 =~ s/string/streched string/; print "$string2\n"; # that prints: # "This is streched string" # Your second substr(): substr $string, 9, 6, 'STRING'; print $string,"\n"; # that prints: # This iis STRINGhedstring # I suspect that you want: $string2 =~ s/string/STRING/; print "$string2\n"; # that prints: # "This is streched STRING"
Perl is wonderful with what are called "regular expressions" or regex'es. In general, use the power of the Perl language. substr() is a low level thing that can be used, but is very, very rare in Perl programs. substr() is very common in C programs because there is not a "built in regex" operator.

To summarize:

#!/usr/bin/perl use strict; use warnings; my $string = "This is string"; print "$string\n"; $string =~ s/string/stretched string/; $string =~ s/string/STRING/; print "$string\n"; __END__ This is string This is stretched STRING

In reply to Re^3: How to use Substr to bulk substitutions? by Marshall
in thread How to use Substr to bulk substitutions? by phoenix007

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.