Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: peek at STDIN, to determine data type and then pass STDIN to a parser

by aral (Acolyte)
on Jan 08, 2015 at 09:43 UTC ( [id://1112631]=note: print w/replies, xml ) Need Help??


in reply to Re: peek at STDIN, to determine data type and then pass STDIN to a parser
in thread peek at STDIN, to determine data type and then pass STDIN to a parser

Excellent solution as well - I just tested it and it worked out of the box like this:

use FileHandle::Unget; my $fh = FileHandle::Unget->new(\*STDIN) or die "Cannot open filehandle: $!"; my $testline = <$fh>;; $fh->ungets($testline); print "$.: $testline"; for (my $i = 0; $i < 3; $i++) { $testline = <$fh>; print "$.: $testline"; }

Output for "cat xmlfile | ./perscript.pl" is:

1: <?xml version="1.0" encoding="UTF-8"?> 1: <?xml version="1.0" encoding="UTF-8"?> 2: <MFOP> 3: <Basics>

Thank you very much! FileHandle::Unget is *the* answer to my original question.

@ikegami: Unfortunately, the install script (Makefile) for IO::Unread fails with error messages, and there seems to be no debian packet for it available in jessie - so I was not able to test this.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1112631]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-28 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found