• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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!


0
dprasad
Asked:
dprasad
4 Solutions
 
ozoCommented:
#what happens if you try
open (OUTPUTFILE, ">$outputfile") or die "Can't open $outputfile $!";
0
 
ozoCommented:
You might also try opening the file before starting the parser
0
 
ozoCommented:
(sorry, temorary blindness)
Did you want to write to "outputfile" or "converted.html" ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
davorgCommented:
Try checking the results of the "open" call. Also turn on "use warnings" so you get warnings if you try to write to a file that hasn't been opened successfully.
0
 
PaulS_IIICommented:
I think ozo is correct. from what I understan of this, your OUTPUTFILE variable is your handle to the opened file. So in your line open(OUTPUTFILE, ">outputfile"); you are openeing a file simply named outputfile. Search your system for that file name, I bet you'll find it. If you want to right to converted.html then you need to put the $ infront of outputfile, like this open(OUTPUTFILE, ">$outputfile");  This will write to the converted.html file
0
 
ITcrowCommented:

my $outputfile = "converted.html";
open (OUTPUTFILE, ">outputfile");

Like previous poster suggested, change the lines mentioned above to:

my $outputfile = "converted.html";
open (OUTPUTFILE, ">$outputfile") || die "Failed to open $outputfile. $!\n";

Then expect a file called converted.html generated.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now