Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  Erlang ports and Perl

Sean.Hinde at one2one.co.
Posted: Wed Feb 23, 2000 4:17 pm Reply with quote
Guest
Hi all,

I'm having a little difficulty with using a perl script as a port program:

#!/usr/local/bin/perl
while(1) {
$cmd = <STDIN>;
print STDERR "Received OK";
print "ok";
print STDERR "Sent";
}

My erlang port program:

Port = open_port({spawn, "perlscript"}, [stream]),
Port ! {self(), {command, "hello"}},
receive
Data ->
Data
after 10000
timedout
end

The result of this is that perl receives the hello message and prints
"Received OK" and "Sent" on stderr, but my erlang process never receives
"ok". Tracing confirms that the hello message is sent to the port but
nothing is received. Using an equivalent C program as the port program with
the same erlang code works fine.

Anyone any idea?

Sean


Post generated using Mail2Forum (http://m2f.sourceforge.net)
spearce at spearce.org
Posted: Wed Feb 23, 2000 4:33 pm Reply with quote
Guest
Sean,

Perl output is typically line-buffered by default by Perl. This means
that the underlying OS will not receive any information to pass onto
another program (such as Erlang) unless a newline ("
") is printed in
Perl. Try doing the following before your while(1) loop starts:

select((select(STDOUT),$| = 1)[0]);
select((select(STDERR),$| = 1)[0]);

to unbuffer both the STDOUT and STDERR streams and see if that makes
a difference to your little program.

Sean Hinde <Sean.Hinde_at_one2one.co.uk> scrawled:
> Hi all,
>
> I'm having a little difficulty with using a perl script as a port program:
>
> #!/usr/local/bin/perl
> while(1) {
> $cmd = <STDIN>;
> print STDERR "Received OK";
> print "ok";
> print STDERR "Sent";
> }
>
> My erlang port program:
>
> Port = open_port({spawn, "perlscript"}, [stream]),
> Port ! {self(), {command, "hello"}},
> receive
> Data ->
> Data
> after 10000
> timedout
> end
>
> The result of this is that perl receives the hello message and prints
> "Received OK" and "Sent" on stderr, but my erlang process never receives
> "ok". Tracing confirms that the hello message is sent to the port but
> nothing is received. Using an equivalent C program as the port program with
> the same erlang code works fine.


--
Shawn.

``If this had been a real
life, you would have
received instructions
on where to go and what
to do.''


Post generated using Mail2Forum (http://m2f.sourceforge.net)
bobsh at internetnw.net
Posted: Wed Feb 23, 2000 4:54 pm Reply with quote
Guest
The "ok" is probably stuck in a buffer, and you need to flush it. There's
at least one Perlism for that... I don't have a perl manual handy (and I
use Perl only rarely, so don't remember), but I do have some code that
needed to do that, and it uses "autoflush" and also "$|" might have
something to do with it.

On Wed, 23 Feb 2000, Sean Hinde wrote:

> Hi all,
>
> I'm having a little difficulty with using a perl script as a port program:
>
> #!/usr/local/bin/perl
> while(1) {
> $cmd = <STDIN>;
> print STDERR "Received OK";
> print "ok";
> print STDERR "Sent";
> }
>
> My erlang port program:
>
> Port = open_port({spawn, "perlscript"}, [stream]),
> Port ! {self(), {command, "hello"}},
> receive
> Data ->
> Data
> after 10000
> timedout
> end
>
> The result of this is that perl receives the hello message and prints
> "Received OK" and "Sent" on stderr, but my erlang process never receives
> "ok". Tracing confirms that the hello message is sent to the port but
> nothing is received. Using an equivalent C program as the port program with
> the same erlang code works fine.
>
> Anyone any idea?
>
> Sean
>



Post generated using Mail2Forum (http://m2f.sourceforge.net)
borys at bill3.ncats.net
Posted: Wed Feb 23, 2000 5:00 pm Reply with quote
Guest
> #!/usr/local/bin/perl
> while(1) {
> $cmd = <STDIN>;
> print STDERR "Received OK";
> print "ok";
> print STDERR "Sent";
> }

> The result of this is that perl receives the hello message and prints
> "Received OK" and "Sent" on stderr, but my erlang process never receives
> "ok". Tracing confirms that the hello message is sent to the port but
> nothing is received. Using an equivalent C program as the port program with
> the same erlang code works fine.


Try turning line buffering off by either setting $| to 1 or by
STDIN->autoflush(1). I imagine libc is waiting for a newline before it
*really* sends the data.


> Sean
Trannie


Post generated using Mail2Forum (http://m2f.sourceforge.net)
Sean.Hinde at one2one.co.
Posted: Wed Feb 23, 2000 5:28 pm Reply with quote
Guest
This one seems to work. Thanks for all your answers.

Sean

-----Original Message-----
From: Shawn Pearce [mailto:spearce_at_spearce.org]
Sent: 23 February 2000 16:34
To: erlang-questions_at_erlang.org
Subject: Re: Erlang ports and Perl


Sean,

Perl output is typically line-buffered by default by Perl. This means
that the underlying OS will not receive any information to pass onto
another program (such as Erlang) unless a newline ("
") is printed in
Perl. Try doing the following before your while(1) loop starts:

select((select(STDOUT),$| = 1)[0]);
select((select(STDERR),$| = 1)[0]);

to unbuffer both the STDOUT and STDERR streams and see if that makes
a difference to your little program.

Sean Hinde <Sean.Hinde_at_one2one.co.uk> scrawled:
> Hi all,
>
> I'm having a little difficulty with using a perl script as a port program:
>
> #!/usr/local/bin/perl
> while(1) {
> $cmd = <STDIN>;
> print STDERR "Received OK";
> print "ok";
> print STDERR "Sent";
> }
>
> My erlang port program:
>
> Port = open_port({spawn, "perlscript"}, [stream]),
> Port ! {self(), {command, "hello"}},
> receive
> Data ->
> Data
> after 10000
> timedout
> end
>
> The result of this is that perl receives the hello message and prints
> "Received OK" and "Sent" on stderr, but my erlang process never receives
> "ok". Tracing confirms that the hello message is sent to the port but
> nothing is received. Using an equivalent C program as the port program
with
> the same erlang code works fine.


--
Shawn.

``If this had been a real
life, you would have
received instructions
on where to go and what
to do.''


Post generated using Mail2Forum (http://m2f.sourceforge.net)
cheung
Posted: Tue Feb 21, 2012 3:30 am Reply with quote
Guest
Rick Nike Air Max 95 Santorum was Nike Air Max endorsed in the Republican presidential race by Ohio Air Max Attorney General Mike DeWine, another sign he Nike Air Max 90 is gaining ground on rival Mitt Romney.Standing next to Nike Air Max 90 Cheap Santorum yesterday on the steps of the Ohio statehouse in Columbus, DeWine said he backed Romney last October because he thought the former Massachusetts governor was the best candidate to defeat President Barack Obama. DeWine said he no longer believes that after the campaign run by Romney, Cheap Air Max 95 with support from an outside political action committee, Restore Air Max UK Our Future. “To be elected president, you have to do more than tear down your opponent; you have to give Nike Free Shoes the American people a reason to vote for you,” DeWine Nike Free 3.0 V2 said. “Rick Santorum has done that. Nike Free 3.0 V3 Sadly, Governor Romney has Nike Free 3.0 not.”The endorsement is a boost for Santorum, Nike Free Running whose Nike Free standing in the polls rose after wins in three states last Nike Free Run week. He is now leading in polls in Michigan, which holds its primary on Feb. 28, and in Ohio, one of 11 states with a contest on March 6, Super Tuesday.A victory for Santorum in Michigan, Romney’s native state, would position him as the Air Max 90 UK leading candidate among the party’s anti-abortion-rights and anti-tax activists.Republican presidential Air Max 95 candidate Nike Air Max 95 Cheap Rick Santorum said Air Max 90 he may not be from Michigan, but his Pennsylvania roots Cheap Air Max 90 make him at least Air Max 90 Trainers close to the hearts of Michiganders.As a result, he said, he offers the best chance to beat President Barack Obama in November."I've always Cheap Nike Air Max 90 run as a conservative," Santorum told a crowd of about 1,500 people packed into the Palazzo Grande banquet center in Shelby Township on Friday. "If you're looking for the candidate in this race who has been the most successful ... he happens to be here."
wuji
Posted: Mon Sep 17, 2012 7:18 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
is too afraid to borrow the money," says Chai Ling, Ling, cheap replica designer *beep* Ling, "because she has no idea how she could pay
back." Nor does she have any guarantee the government won't won't [h4]jordan 11[/h4] won't ask for more money in the future. "That is
psychological pressure she is under," says Chai Ling. "It becomes becomes jordan concord becomes a money making operation for the government."Cao told ABC
she very much wanted to keep her baby, but she she cheap jordan shoes she was unsure of what do to. For Cao, waiting
the Saturday deadline is torture, but every moment is also also [h3]jordan concord[/h3] also precious for the expectant mother.Obama Defends Immigrant Deportation Rules
as PoliticalThe President Announces a Change Likely to Please Hispanic Hispanic cheap replica designer *beep* Hispanic VotersBy MATT NEGRIN AND PIERRE THOMASJune 15, 2012 President
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum