you're using the wrong tool for the job. use HTML::Parser or HTML::TokeParser or something similar (XML::Parser or what have you)

$l starts being "initialized" only after the first match because you're assigning $1 to it. $1 will only contain a value after you match something, which happens after you match something, so until you match something, $1 will not be defined ( it will be uninitialized). addd

Code

use re 'debug';

to the top of your script to see extra debugging output

You've chosen the wrong approach, and that hairy regex ain't gonna cut it (it can be fooled). please go to CPAN and get yourself a HTML parser, they're very easy to use, easier than Regular Expressions