Printing to a file

Posted on 2004-08-11
Last Modified: 2010-03-05
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");




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";



Question by:dprasad
LVL 84

Expert Comment

ID: 11770465
#what happens if you try
open (OUTPUTFILE, ">$outputfile") or die "Can't open $outputfile $!";
LVL 84

Expert Comment

ID: 11770522
You might also try opening the file before starting the parser
LVL 84

Accepted Solution

ozo earned 200 total points
ID: 11770533
(sorry, temorary blindness)
Did you want to write to "outputfile" or "converted.html" ?
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Assisted Solution

davorg earned 100 total points
ID: 11770762
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.

Assisted Solution

PaulS_III earned 100 total points
ID: 11772205
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

Assisted Solution

ITcrow earned 100 total points
ID: 11868091

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.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question