Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Searching directories for HTML title tags

by arturo (Vicar)
on Jun 19, 2001 at 20:16 UTC ( #89690=note: print w/replies, xml ) Need Help??

in reply to Searching directories for HTML title tags

I'll just give you some structural information, as I haven't used the latest version of HTML::Parser, a CPAN module that you should seriously consider using.

One component is File::Find. You can use that to find .html, .htm, .shtml, and whatever extensions consistute "being an HTML file" as far as your webserver is concerned. File::Find</code> will recurse through subdirectories, and it's easy enough to get it to return an array of filenames. As far as finding <title> tags, the most robust solution would be to use HTML::Parser, which takes a lot of different oddities of HTML code into account (e.g. what if the content of the tag extends over two lines?). When you say you want to return the data "in an array", I'm assuming that what you want to do is store two pieces of data for each file: the name of the file, and the content of the title tag therein. Depending on your needs, you might try storing this information as a hash, where the keys are the filenames and the values are the corresponding titles. The following code will get you an array of HTML files:

Update code below now uses the correct $File::Find::name, which contains the full path to the file, rather than $_, which is just the name of the file.

use File::Find; use HTML::Parser; # this code doesn't make use of the module, but I r +eally think you should use it in your code =) my @data; find(\&html_files, "/base/path"); # now process @data, which is a list of filenames sub html_files { push @data, $File::Find::name if /\.s?html?$/; }


perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://89690]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2021-04-21 17:50 GMT
Find Nodes?
    Voting Booth?

    No recent polls found