in reply to Warnings Are Good! Plus A Question about $1
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> cd /pub grep> more beer |
|
|---|