uuoc has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks, I am seeking guidance on preprocessing STDIN without first putting it all into an @array or writing it to a file, which offends my sensibilities.
Consider the following CODE:
if ( $opt_e ) { # Preprocess STDIN and strip email header $start_of_body=0; while ( !($start_of_body) ){ $line=<STDIN>; if($line eq "\n") { $start_of_body=1; } } $twig->parse( \*STDIN); } else { print "Reading from STDIN, press CTRL+D when finished.\n"; $twig->parse( \*STDIN); } }

The purpose of this code is to allow email that contains XML in the body to be piped and processed by the script. However, the XML parser only handles XML and I must strip off the email header. I am reading STDIN one line at a time and when I detect that I have reached the end of the header, I pass the *FileHandle for STDIN to the XML parser method. The *FileHandle should now be at the start of the XML or email body, but it appears empty.

If I call the parse method and pipe just straight XML to the script, all is well as seen in the else code branch.

Can anyone explain this behavior?

Humbly,
UUOC

Replies are listed 'Best First'.
Re (tilly) 1: Preprocessing STDIN
by tilly (Archbishop) on Sep 08, 2001 at 07:16 UTC
    That looks like it should work, unless the email in question was generated by Outlook in which case it will have a non-conformant header. (What? You don't know all of the headers that could possibly be contained in an email message and don't know to break out at the first thing that doesn't fit? Then you are probably a Netscape client and we want to make you crash. So not sorry if you are merely a trusting Perl script...)

    So I don't know what the issue is, but I would suggest that the first thing you should do is take XML::Twig out of it. Just try to print the contents of the file after the header and see if that looks reasonable. Print it to a file and see if XML::Twig is happy with that. Then proceed from there.