\n#!/usr/bin/perl -p\n# The -p switch, as explained in perlrun, reads each\n# line into $_, runs your code, then prints $_.\n# More precisely it puts a loop like this around your\n# code:\n#\n# LINE:\n# while (<>) {\n# ... # your program goes here\n# } continue {\n# print or die "-p destination: $!\\n";\n# }\n#\n# The -p switch allows you to edit lines on the fly by\n# editing $_.\n#\n# Previously I used -e to allow the program to go on\n# the command line, but this time around I am not\n# doing that.\n\nif (\n / # This regular expression matches:\n ^ # the start of the string\n foo # then foo\n (\\d+) # then some digits, put in $1\n = # an = sign\n (.*\\n) # other junk, including a return\n # that go into $2.\n /x # /x turns on commenting.\n # (since no variable is bound to the\n # match it matches $_ - which is\n # conveniently your current line.)\n) {\n # OK, we matched. If we have not yet come up with\n # a replacement for foo4, then set it to $2.\n $r ||= $2;\n # If this is foo4, then replace $_ (the new value is\n # what we'll print).\n $_ = "foo4$r" if 4 == $1;\n}\n
Cheers,
Ben