The problem's not with the regex... but first, a few other things.

Confess! You just stuck use strict in there to make us happy, didn't you? This script won't run at all, as is. Besides the fact that none of the variables are scoped using my or local, you forgot to put the semicolon after strict. Heheh... nice try! :-)

Also, one must wonder why you don't use CGI. Put in the slight effort to learn it... you'll be glad you did.

Now... the problem is with

print (<READTHIS>);

A filehandle, used in list context, will return all the lines it has left. Since print likes lists, the whole file gets printed out verbatim right away. Then, when you get to

while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } }

there's nothing left.

Anyway, your while loop doesn't do what I think you think it does. The fact that you thought the regex was the problem leads me to think you actually wanted to say,

while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } else{ print $line; } }

and leave out,

print (<READTHIS>);

If you want to find out where your problem is, by the way, you can often do it by sticking print statements in odd places. For instance, you could have stuck one in here:

while ($line = <READTHIS>) { print "MOO!\n"; if ($line =~ /cat/) { print "yeppers!"; } }

Then, since you didn't hear a herd of cows coming through, you'd have realized that you actually weren't going through the loop at all.

P.S. Throw close READTHIS; down near the bottom, just for fun!


In reply to Re: reg ex matching 101 by Petruchio
in thread reg ex matching 101 by NotProud

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.