Well, I hate to be the bad guy, but I think a comment is needed here. There are several problems with the above code:
- It doesn't work, even as designed (it's only getting the first line from the file, because <DOC> is in scalar context)
- It's bad style
- Aside from the error in reading the file, the binmode is dubious
- Assuming the intent is really to slurp the entire file, that's generally not a good thing to do -- and that's not a good idiom to use to do it
- Use of .*? in a REx is almost always a bad choice -- here it should be [^>]*
- It's shockingly inefficient -- if you're up for some fun, run it through re 'debug' some time
- Finally, and most importantly, it's wrong. I added a print statement and closed the while (and fixed the slurping error) and ran the following perfectly valid HTML through it:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD><TITLE>Test</TITLE></HEAD>
<BODY>
<a href ="foobaz"></a>
<a name="href=foo"></a>
<a name="foo>bar" href="foobar"></a>
</BODY>
</HTML>
And it missed the two valid href links (the first and the last) and wrongly flagged the second one as an href link.
I think it's very important to realize that while doing this sort of thing seems easy, it isn't. There are a lot of cases that you will miss if you try. Use LinkExtor. Use HTML::Parser. Use HTML::TokeParser. Heck, use URI::Find. Just don't "do it yourself" unless you're prepared to devote quite a bit of time developing, honing, and fixing your solution.
I think that Dermot knows all of this, judging from his comment that using the CPAN is probably the best way to go, but I wanted to make sure no one decided to use this instead because it was "easier". Do not.
I leave you with something I posted to Usenet not too long ago -- a script that correctly finds all anchor links in a document -- in 4 lines of Perl. It's that easy to do with HTML::Parser or one of the other tools made for such things.
-dlc
#!/usr/bin/perl -wl
use strict;use HTML::Parser;my $p=HTML::Parser->new(api_version
=>3);$p->handler(start=>sub{print shift->{href}if shift eq 'a'},
'tagname,attr');local $/;$p->parse(<>);#Just another URI finder
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.