I haven't used XML::Parser, so please excuse if this is an ignorant question, but I don't see anywhere here where the output of $parser->parsefile($xmlfile); is being saved to INTFILEout. Something appears to be missing here. I guess in the Start, End and Char routines that aren't shown.

This unless -f $intfile stuff is bizarre. There are plenty of reasons why an open could fail on an existing file. I also would take that out.

Update: On second thought, it could be that the "unless clause" is preventing the open() from even happening! I.e. the unless may apply to the open and not to the "or die" part. The kind of open() that you have will create a new file if none exists, if it does already exist, it will be essentially be "zeroed" out and a new blank file of the same name is open. In any event, I don't see the need or the intent of this "unless -f $intfile" part. See additional update below with some test code.

# open intermediate file for pass of XML file open (INTFILEout, '>', $intfile) or die ("Cannot open file $intfile") unless -f $intfile; # parse thru XML file into intermediate file my $parser = new XML::Parser; $parser->setHandlers( Start => \&startElement, End => \&endElement, Char => \&characterData, Default => \&default); $parser->parsefile($xmlfile); # close intermediate file from first pass of XML file close INTFILEout; ### is anything in INFILEout at this point? If so, how ### would the parser know how to put it there? ### stop the code here and cat or type the contents of INFILEout
The close and re-open part looks ok to me. The "unless" stuff is weird.
Closing a temporary file for write and re-opening it for read is a very normal thing to do and I think you have that part right.

What are you trying to accomplish here:

while ($linein = <INTFILEin>){ chomp $linein; print FMTFILE "$linein\n"; }
Doesn't look like it does much. Something like this is useful to translate between Unix and Windows line endings (when a fiddling with a file moved between systems), but doesn't look like that is what is happening here. I guess this dummy code for some other function, but its not clear to me why you can't get the parsed XML output into the desired format in the first place without having to re-parse some intermediate file?

Another UPDATE: yep this unless is causing trouble:

#!/usr/bin/perl -w use strict; my $x; my $y=1; sub setx {$x =1;} #### setx() or die "can't set X" unless $y; print "$x"; #Use of uninitialized value $x in string at C:\TEMP\testunless.pl line + 8.
If $y=1, the setx() routine (analog to the open()) does not run. The code will not open the file for output if if it already exists. I would presume that if you had strict and warnings in force, some kind of "attempted write to an unopened file handle" error would have resulted?

In reply to Re: Reopen a closed file for read by Marshall
in thread Reopen a closed file for read by twdgt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.