I would most likely write that regex like this:

#!/usr/bin/perl -w use strict; foreach (<DATA>) { s/^i ([^\n]+)$/makeImageTag($1)/e; #see Update } sub makeImageTag { my $foo = shift || 'no_image.png'; print "$foo\n"; } __DATA__ i howdy.gif i foo.jpg i bar.png i blah.jpg

Here you have perl handle the newlines

Can someone explain what the difference is between \1 and $1? Obviously I can't use the former when there's an expression to be evaluated...

from perlre:

$pattern =~ s/(\W)/\\\1/g;

This is grandfathered for the RHS of a substitute to avoid shocking the sed addicts, but it's a dirty habit to get into. That's because in PerlThink, the righthand side of a s/// is a double-quoted string. \1 in the usual double-quoted string means a control-A. The customary Unix meaning of \1 is kludged in for s///. However, if you get into the habit of doing that, you get yourself into trouble if you then add an /e modifier.

also you can ponder this from perlre s/(\d+)/\1000/;

Update: Jeez, I should've rewritten the whole regex. Absolutly no need for the negated char class  ([^\n]+) should be just (.+) since I thoughtfully pointed out the <> takes care of newlines



grep
grep> cd /pub
grep> more beer

In reply to Re: Warnings Are Good! Plus A Question about $1 by grep
in thread Warnings Are Good! Plus A Question about $1 by Cody Pendant

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.