http://qs321.pair.com?node_id=89690


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?$/; }

HTH

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"'