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

I have written the following code to find a particular word in a doc file located on one of my drives.But I wonder if I can access a webpage (by providing its link to my code)and then I can check if a particular word exists in the webpage.Below is my code for local file on my drive.

use strict; use warnings; use diagnostics; use 5.010000; open('FILE','E:\Data.doc') or die $!; my $find; # the word to be searched print"What are you searching for ?"; chomp($find=<STDIN>); my $count=0; # It will count the word if it exists while(<FILE>){ my $string = $_; my @temp = split(' ',$string); foreach my $loop(@temp){ $count++ if($loop =~ m/$find/i);} } print"\n$find occcured $count times in your file."

Replies are listed 'Best First'.
Re: Perl code to check and count count if a word exists on a webpage
by Marshall (Canon) on Jan 29, 2012 at 11:22 UTC
    If you want to see if a word is on a webpage, the first step is to get the webpage. There are many Perl tools for doing that. The most basic of these is: LWP::Simple. There are other tools, Mechanize is one, also LWP::UserAgent.

    This whole subject can get complicated if you have to fill in forms and do more complex types of interactions with the web server. You didn't provide any information about what kind of webpage's you want to access. But basically it is possible to get any page that you as a human could while clicking around on a website. LWP Cookbook may be useful. Let us know how you get on after trying some simple stuff.

    Update:
    As possible alternate to your code, see below. No need to split, you can use a match global. The \Q\E pair says to interpret text in $word literally (in case funny characters are in there). The \b says to match on a word boundary - this is why the "thisthis" does not match.

    #!/usr/bin/perl -w use strict; my $count; my $word = "this"; while (<DATA>) { my @matches = /\b\Q$word\E\b/gi; $count += @matches; # # or just do this: # $count+= () = /\b\Q$word\E\b/gi; } print "$count"; #prints 3 # note that the last "thisthis" on line 1 # doesn't count! Also note that multiple "this" on # same line are counted. __DATA__ this is the text that this thisthis abd this
    This is of course just the functionality of command line grep. There are Windows variants free on the web - search Windows grep, or gnu grep.
Re: Perl code to count the number of words on a webpage
by Anonymous Monk on Jan 29, 2012 at 07:02 UTC
    No need to wonder, if you Try It To See, you'll see that you can't do it :)
Re: Perl code to count the number of words on a webpage
by JavaFan (Canon) on Jan 29, 2012 at 07:15 UTC
    Did you try? What was the result?
Re: Perl code to check and count count if a word exists on a webpage
by Anonymous Monk on Jan 29, 2012 at 11:51 UTC
    Claims to be a grad student, has poor communication skills, can't show any attempt at research or solving the problem for themselves. -- Searching this forum or the internet would have taken less time than posting this attempt and got all the resources you'd need. If you are not interested enough to do it, why bother?

      Hi, Don't act smart. This is not a place to discuss grammatical skills or communications skills. I guess every one is free to join perlmonk.org irrespective of their linguistic backgrounds. Also how come you know that I haven't worked on this problem.After lot of thinking and meditation I posted my question. If you can't answer don't leave sarcastic comments.Also being a graduate student never means that you know every thing. Even You also didn't learned Perl in 24 hours.I have an electrical background and I have interest in Perl.I agree that you have got a wonderful experience in Perl but can't discourage someone from learning Perl by digressing from the main concept to communication skills. I am sorry if you felt bad but this is the truth.

        Well, if you have worked on the problem (the code you posted doesn't show any attempt to search the contents of a web page), you should show us what you've tried along with any errors. Did your period of thinking and meditation include some research? A google search returns many results relevant to your question, including perlfaq9, which has many questions on the subject of interacting with web pages, and their associated answers.

        Communication skills do matter, the ability to ask a question in a way which people can understand, being able to show the effort you've made and the resulting problems matters. Surely you've experienced this in academia, regardless which field you study? Each time you post there are links to advice on posting here, I won't repeat them, as you're new to Perl and PerlMonks I suggest you invest the time to read and understand:

        Learn to filter out which posts merit a reply, and which are best avoided. I offer this advice for all forums, not just this one.

        Update: fixed perlfaq link. Thanks anon.