NotProud has asked for the wisdom of the Perl Monks concerning the following question:

All I want to do is print 'yeppers' if the output 
from a text file has the word 'cat'.

But I cannot get it to work.  

Here is the code.  Any suggestions would be great.
and why am I doing this on Friday night?

I'm Not Proud
#!/usr/local/bin/perl -w use strict print "Content-Type: text/html\n\n"; print "<html>\n"; print "<body bgcolor=fffbbf>\n"; #this chunk of code out put the input box. Got me? read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/~!/ ~!/g; $value =~ s/\s*$//; $FORM{$name} = $value; # print "$FORM{$name} = $value<br>\n"; } #this is the end of the output code. Got me? $readthis = 'd:/209.35.237.237/perl/regex/work.txt'; open(READTHIS, "< $readthis") or die "I can't open readthis: $!\n"; print (<READTHIS>); print "<br>\n"; print $value; print "<p>\n"; #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #this is the code I can not get to work #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ print <<EOF; <form name="textwrite" action="work.pl" method="post"> enter something: <input type="text" name="thisone" size="30"> <br> <input type="submit" value="submit"> </form> EOF print "<br></body>\n"; print "</html>";

Replies are listed 'Best First'.
Re: reg ex matching 101
by Petruchio (Vicar) on Oct 07, 2000 at 07:48 UTC
    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!

      Yep! The main problem is with 'print (<READTHIS>);'. Once I took this out the code 'worked'.

      Thanks to everyone for all the input. I have bad habits a-go-go from trying to learn this stuff too fast and I am getting sloppy.

      Yep! The main problem is with 'print (<READTHIS>);'. Once I took this out the code 'worked'.

      Thanks to everyone for all the input. I have bad habits a-go-go from trying to learn this stuff too fast and I am getting sloppy.

RE: reg ex matching 101
by 2501 (Pilgrim) on Oct 07, 2000 at 07:13 UTC
    Broken: while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } }</I>
    change that to be:
    while(<READTHIS>){ if($_ =~ /cat/) { print "Yeppers!\n" } else{ print $_; }
    Keep in mind, you are going to get any match on cat like "catching" a cold...I dunno if you wanted that or not. If you don't mind me asking, what was your intent for the script? I was trying to figure out what you were up to with the search and replace lines. In some of those you are searching for a set of characters and replacing them with the identical data.
    btw, go home and get some pizza:P
      Good call on the loop, but it's still not gonna work unless you knock out print <READTHIS>; I haven't looked at it closely, but the stuff up top seems to be homemade CGI; and there's a hard-to-see space in s/~!/ ~!/g
      What's wrong with the original?

      It doesn't handle word boundaries, as you mention, but I don't see much of a difference between the magical assignment to $_ and the explicit assignment to $line.

      Besides that, you could just use print "Yeppers!" if /cat/; and print;.

      If I'm missing something here, please enlighten me.

        the use of $_ was a 'code to taste' manuever on my part. I use the magical assignments all the time:)
        What I saw was the file being printed out, then the while loop trying to print 'yeppers' each time cat was found.
        While I was unaware that printing the file stream and then trying to call it in the while would fail, I imagined the output to be the full file printing, and then any number of 'Yeppers' one after the other. I was assuming what he wanted to do was print yeppers along with the text instead of one after the other.
RE: reg ex matching 101
by turnstep (Parson) on Oct 07, 2000 at 17:44 UTC
    All I want to do is print 'yeppers' if the output from a text file has the word 'cat'.

    Ignoring the whole CGI issue, one way to do what you are asking is:

    open(READTHIS, "< $readthis") or die "I can't open $readthis: $!\n"; while(<READTHIS>) { if (/\bcat\b/i) { print "yeppers!"; last; } } close(READTHIS);

    Note that your original code forgot to add a dollar sign to the 'readthis' variable in the die statement. The \b indicates a word boundary, so that we do not grab "catalog" as pointed out above. We don't need a =~ because we are matching the default $_ created by the while(<READTHIS>) expression. The last is there so that once we find the "cat", we stop looking, and just close the file right away.

Re: reg ex matching 101
by NotProud (Novice) on Oct 07, 2000 at 07:33 UTC
    Thanks for the response. I just started messing around with perl a week or two ago and most of the good code I stole from a 'real' perl programmer, so I do not really know what is going on in lines 14-19. The original script just printed the output of a form so that you knew what was going into it. However, I have stumbled my way through the rest of the code and I am working with reg-ex now - learning. I will continue this tomorrow. Nap time.
A reply falls below the community's threshold of quality. You may see it by logging in.