Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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
[erix]: oh, I see... it's pretty obvious really
[choroba]: What do you mean by "anchors"?
[erix]: anchors
[erix]: I have figured it out. I actually constructed them correctly but used a faulty url *headdesk*

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2018-06-19 12:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.