Solved

Printing to a file

Posted on 2004-08-11
7
234 Views
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");

$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
Comment
Question by:dprasad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 84

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
ozo earned 200 total points
ID: 11770533
(sorry, temorary blindness)
Did you want to write to "outputfile" or "converted.html" ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Assisted Solution

by:davorg
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.
0
 
LVL 2

Assisted Solution

by:PaulS_III
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
0
 
LVL 5

Assisted Solution

by:ITcrow
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with bash and/or perl commands on OS X Terminal 9 111
Port 80 requests 16 104
Excel to CSV conversion with specific columns 5 98
Export Variables in Perl 3 108
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

730 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