dprasad
asked on
Printing to a file
I'm trying to parse an html file and print ascii to an outside file. When I do the following, nothing happens and no file is generated. I can get it to print to the console if I print directly without an outputfile. See anything wrong?
# Define module to use
use HTML::Parser();
# Create instance
$p = HTML::Parser->new(start_h => [\&start_rtn, 'tag'],
text_h => [\&text_rtn, 'text'],
end_h => [\&end_rtn, 'tag']);
# Start parsing the following HTML string
my $outputfile = "converted.html";
open (OUTPUTFILE, ">outputfile");
$p->parse('
//SEVERAL LINES OF HTML
');
sub start_rtn {
# Execute when start tag is encountered
foreach (@_) {
print OUTPUTFILE "===\nStart: $_\n";
}
}
sub text_rtn {
# Execute when text is encountered
foreach (@_) {
print OUTPUTFILE"\tText: $_\n";
}
}
sub end_rtn {
# Execute when the end tag is encountered
foreach (@_) {
print OUTPUTFILE "End: $_\n";
}
}
close (OUTPUTFILE);
THANKS!
# Define module to use
use HTML::Parser();
# Create instance
$p = HTML::Parser->new(start_h => [\&start_rtn, 'tag'],
text_h => [\&text_rtn, 'text'],
end_h => [\&end_rtn, 'tag']);
# Start parsing the following HTML string
my $outputfile = "converted.html";
open (OUTPUTFILE, ">outputfile");
$p->parse('
//SEVERAL LINES OF HTML
');
sub start_rtn {
# Execute when start tag is encountered
foreach (@_) {
print OUTPUTFILE "===\nStart: $_\n";
}
}
sub text_rtn {
# Execute when text is encountered
foreach (@_) {
print OUTPUTFILE"\tText: $_\n";
}
}
sub end_rtn {
# Execute when the end tag is encountered
foreach (@_) {
print OUTPUTFILE "End: $_\n";
}
}
close (OUTPUTFILE);
THANKS!
You might also try opening the file before starting the parser
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
open (OUTPUTFILE, ">$outputfile") or die "Can't open $outputfile $!";