in reply to Fetching meta tag info

You have to use the right tool for the job, here is how with one of them:
use HTML::TokeParser::Simple; my $p = new HTML::TokeParser::Simple ( \ qq{ <META NAME="Company.HTML.author" CONTENT="Carl Jones"> <META NAME="Company.HTML.author" CONTENT="Carl Jones"> <META NAME="web.author" CONTENT="Mike Simmons"> }); while(defined( my $t = $p->get_tag('meta') ) ) { my $at = $t->return_attr; if($$at{name} =~ /author/i) { print "$$at{name}=$$at{content}\n"; } } __END__ Company.HTML.author=Carl Jones Company.HTML.author=Carl Jones web.author=Mike Simmons
update:
That's cause HTML::TokeParser::Simple is sooo easy, i'm able to write gems like these in under a minute ;)

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: Re: Fetching meta tag info
by mirod (Canon) on Nov 12, 2002 at 15:54 UTC

    Darn! You beat me to it! Here is what I came up with, using HTML::TokeParser, using Perl & LWP as a reference:

    #!/usr/bin/perl -w use strict; use HTML::TokeParser; $/="\n\n"; while( my $html=<DATA>) { my $author= get_author( $html); print "Author: $author\n"; } exit; sub get_author { my $html= shift; my $stream= HTML::TokeParser->new( \$html); while( my $token= $stream->get_token) { my $token_type= shift @$token; next unless $token_type eq 'S'; my( $tag, $attribute_hashref, $attribute_order_arrayref, $sour +ce)= @$token; next unless $tag eq 'meta'; if( grep { m{\.author$} } values %$attribute_hashref) { return $attribute_hashref->{content}; } } return 'unknown'; } __DATA__ <html> <head> <META NAME="HTML.author" CONTENT="Joe Smith"> <META NAME="whatever" CONTENT="foo"> </head> <body><p>dummy</p></body> </html> <html> <head><meta name="Company.HTML.author" content="Carl Jones"></head> <body><p>dummy</p></body> </html> <html> <head><META NAME="web.author" CONTENT="Mike Simmons"></head> <body><p>dummy</p></body> </html> <html> <head> <META NAME="whatever" CONTENT="foo"> </head> <body><p>dummy</p></body> </html>