Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

pipe perl

by mouser13 (Novice)
on Oct 30, 2012 at 18:40 UTC ( #1001536=perlquestion: print w/replies, xml ) Need Help??
mouser13 has asked for the wisdom of the Perl Monks concerning the following question:

Hello I have a old program that need very specific format for input and takes a very long time to load in the web world anyway (20 secs). So I want to use the open and pipe to send information to it. I have a server(c++) simmilar to cgi_speedy, but I never really worked with the open and pipe.
open(old, "| /bin/old"); foreach my $INPUT(@DATA) { my ($LAST_NAME,$FIRSTNAME,$ID_NUM,$BOOK) = split(/\s+/,$INPUT); print old "$LAST_NAME\t$FIRSTNAME\n" print old "\t$ID_NUM\t$BOOK\n" }
The code doesn't work but if I do it by hand inputing via keyboard it works? Is their sometype of formating I need to do on the tab or newlines.

Replies are listed 'Best First'.
Re: pipe perl
by VinsWorldcom (Parson) on Oct 30, 2012 at 18:58 UTC

    See open. Then try this:

    open(my $old, '|-', "/bin/old"); select((select($old), $| = 1)[0]); foreach my $INPUT(@DATA) { my ($LAST_NAME,$FIRSTNAME,$ID_NUM,$BOOK) = split(/\s+/,$INPUT); print $old "$LAST_NAME\t$FIRSTNAME\n"; print $old "\t$ID_NUM\t$BOOK\n" }
      A very important element for this solution (which goes unmentioned above) $| = 1. This turns off buffering on the output stream, or "sets the pipe to hot" (see perlvar). If you are having buffering issues, you can also close your file handle to clear the buffer.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: pipe perl
by golux (Hermit) on Oct 30, 2012 at 20:37 UTC
    Hi mouser13,

    You also have a syntax error -- the 2 print statements are missing the terminal semi-colon:

    print old "$LAST_NAME\t$FIRSTNAME\n" print old "\t$ID_NUM\t$BOOK\n"
    should be:
    print old "$LAST_NAME\t$FIRSTNAME\n"; print old "\t$ID_NUM\t$BOOK\n";
    The second one isn't technically mandatory, since it is followed by the brace, but it's good programming practice.

    Edit:   The reply above from VinsWorldcom did not originally have the necessary semi-colon ';', but he has now quietly added it to make it appear that his code was correct from the beginning. However, it's still better programming practice (as I mentioned) to add it to both lines.

    say  substr+lc crypt(qw $i3 SI$),4,5

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001536]
Front-paged by Corion
[Corion]: I think I saw virtualsue more recently in person than online :-D
[virtualsue]: It's true that there was a bigger gap between the 2 yr old post and the one before it
[ambrus]: yes, I probably just didn't see you two years ago, because I'm not in perlmonks much
[virtualsue]: Nor am I :-)
[talexb]: virtualsue Heh .. long time no see! :)
[virtualsue]: I have a quiet Perl themed slack workspace
[virtualsue]: The person who talks to me on it the most *hates* Perl but she hates everything

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-09-26 13:54 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (295 votes). Check out past polls.