Wanted: Example of asynchronous bidirectional socket client (a socket chatprogram)
Wanted: Example of asynchronous bidirectional socket client (a socket chatprogram)
am 27.07.2011 19:07:50 von Siegfried Heintze
Sorry if this appears twice. Since it bounced back to me -- probably=0Abeca=
use of the HTML format -- I'm sending it again.
I did some google sear=
ching and I could not find an example of a=0Abidirectional asynchronous soc=
ket client. A telnet client is an example=0Aof a bidirectional asynchronous=
socket client.
I don't specifically want source to a telnet client --=
that would be=0Amuch fancier than what I require and would not be helpful =
if the perl=0Aonly called C++. I just want an example in pure perl (or ruby=
or=0Apython).
=0ABy asynchronous I mean that reads and writes can occ=
ur at any time in=0Aany order with no warning.
=0AAsynchronous could =
also mean that we don't block while the write is in=0Aprogress. Blocking w=
hile the write is in progress is fine.
=0AI hope it is possible to wri=
te such a beast in perl.
Let's suppose I spawn two threads, one to blo=
ck on a read socket and the=0Aother to block on the keyboard with $inp=3D
TDIN> (so it can print to the=0Awrite socket when it receives input form th=
e keyboard). Can the first=0Athread print to the console if the second thre=
ad is blocking on=0A$inp=3D?
=0ASurely, someone has posted sou=
rce to such a beast somewhere on the=0Ainternet!
Thanks,=0ASieg=
fried=0A
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Wanted: Example of asynchronous bidirectional socket client (asocket chat program)
am 27.07.2011 20:31:00 von Jim Gibson
On 7/27/11 Wed Jul 27, 2011 10:07 AM, "siegfried@heintze.com"
scribbled:
> Sorry if this appears twice. Since it bounced back to me -- probably
> because of the HTML format -- I'm sending it again.
>
> I did some google searching and I could not find an example of a
> bidirectional asynchronous socket client. A telnet client is an example
> of a bidirectional asynchronous socket client.
>
> I don't specifically want source to a telnet client -- that would be
> much fancier than what I require and would not be helpful if the perl
> only called C++. I just want an example in pure perl (or ruby or
> python).
>
>
> By asynchronous I mean that reads and writes can occur at any time in
> any order with no warning.
>
>
> Asynchronous could also mean that we don't block while the write is in
> progress. Blocking while the write is in progress is fine.
>
>
> I hope it is possible to write such a beast in perl.
>
> Let's suppose I spawn two threads, one to block on a read socket and the
> other to block on the keyboard with $inp= (so it can print to the
> write socket when it receives input form the keyboard). Can the first
> thread print to the console if the second thread is blocking on
> $inp=?
>
>
> Surely, someone has posted source to such a beast somewhere on the
> internet!
I do not know of such an example, nor have I ever written one in Perl. I
have some experience in this type of application in other languages (C++,
Java).
Have you read 'perldoc perlipc'? Specifically the socket sections of that
document?
Your basic choices are to use multitasking, with one process or thread per
i/o handle, or wait on multiple i/o handles in one task.
The former approach requires the use of processes or threads. Processes are
created by the fork function, and are well-supported by Perl, at least under
Unix. However, processes do not share data, so you have an interprocess
communication problem.
Threads can share data structures, but threads are not well-supported by
Perl. I have read a lot of warnings about using threads under Perl, and I
have avoided using them for that reason.
The select function is the way to wait on multiple file handles under Unix.
It may also be supported under Windows. Figuring out how to use it is not
easy, but the effort can be rewarding. There is a built-in function select
that gives direct access to the Unix system call (see 'perldoc -f select').
There is also a module IO::Select that puts an object-oriented layer on top
of the select function.
Having used both approaches in other languages, but not Perl, my inclination
would be to take the 'select' approach, but you might want to try
multitasking. Once you figure out how to use select (or IO::Select), it
works well.
Good luck!
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Add line feed to line
am 27.07.2011 21:19:56 von Tim Lewis
I am attempting to add a line feed to the end of each line. When I do this, a carriage return is also added. My code lines are:
$currentLine = $currentLine . "\x{0A}";
$finalOutput = $finalOutput . $currentLine;
There has to be a way to do this. Also, is there a better way to concatentate?
Thanks for any suggestions on this.
Tim
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 27.07.2011 22:03:32 von Tim Lewis
I found an answer that I thought I would share.
I am using ActivePerl on Windows server 2003. ActivePerl translates 0A as CR\LF. The print statement was causing the issue. To stop this, I added binmode to my file handle:
open(OUTPUT,">$outputFileName");
binmode OUTPUT;
It works great now.
---- Tim Lewis wrote:
> I am attempting to add a line feed to the end of each line. When I do this, a carriage return is also added. My code lines are:
>
> $currentLine = $currentLine . "\x{0A}";
> $finalOutput = $finalOutput . $currentLine;
>
> There has to be a way to do this. Also, is there a better way to concatentate?
>
> Thanks for any suggestions on this.
>
> Tim
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 27.07.2011 22:59:19 von timothy adigun
--0022159f045e70302804a9135224
Content-Type: text/plain; charset=ISO-8859-1
Tim,
>>>>>>check this if it answers ur #1 question:
#!/usr/bib/perl -w
$\="\n"; # with output record separator used you don't ve to use
# $currentLine = $currentLine . "\x{0A}"; in ur code again
my @arr=qw(item1 item2 item3);
for(@arr){
print $_; # used $_ default argument
# your items are separated without stress
}
>>>>>Also, is there a better way to concatentate?
You can use join like this:
#!/usr/bib/perl -w
use strict;
my @arr=qw(item1 item2 item3); # u can put ur values into an array
$arr=join "*,*",@arr; # use join to ouput all values as a single scalar
value
# concatentate any separator u want, here I
used ","
# in red colour
print $arr;
Thanks
On Wed, Jul 27, 2011 at 9:03 PM, Tim Lewis wrote:
> I found an answer that I thought I would share.
>
> I am using ActivePerl on Windows server 2003. ActivePerl translates 0A as
> CR\LF. The print statement was causing the issue. To stop this, I added
> binmode to my file handle:
>
> open(OUTPUT,">$outputFileName");
> binmode OUTPUT;
>
> It works great now.
>
>
> ---- Tim Lewis wrote:
> > I am attempting to add a line feed to the end of each line. When I do
> this, a carriage return is also added. My code lines are:
> >
> > $currentLine = $currentLine . "\x{0A}";
> > $finalOutput = $finalOutput . $currentLine;
> >
> > There has to be a way to do this. Also, is there a better way to
> concatentate?
> >
> > Thanks for any suggestions on this.
> >
> > Tim
> >
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
--0022159f045e70302804a9135224--
Re: Add line feed to line
am 28.07.2011 12:20:48 von Emeka
--001636e0ba3cc1d99604a91e8466
Content-Type: text/plain; charset=ISO-8859-1
Timo,
#!/usr/bib/perl -w What is "-w" doing here?
Emeka
On Wed, Jul 27, 2011 at 9:59 PM, timothy adigun <2teezperl@gmail.com> wrote:
> Tim,
> >>>>>>check this if it answers ur #1 question:
> #!/usr/bib/perl -w
>
> $\="\n"; # with output record separator used you don't ve to use
> # $currentLine = $currentLine . "\x{0A}"; in ur code again
>
> my @arr=qw(item1 item2 item3);
>
> for(@arr){
> print $_; # used $_ default argument
> # your items are separated without stress
>
> }
> >>>>>Also, is there a better way to concatentate?
> You can use join like this:
> #!/usr/bib/perl -w
> use strict;
>
> my @arr=qw(item1 item2 item3); # u can put ur values into an array
>
> $arr=join "*,*",@arr; # use join to ouput all values as a single scalar
> value
> # concatentate any separator u want, here I
> used ","
> # in red colour
> print $arr;
>
> Thanks
>
> On Wed, Jul 27, 2011 at 9:03 PM, Tim Lewis wrote:
>
> > I found an answer that I thought I would share.
> >
> > I am using ActivePerl on Windows server 2003. ActivePerl translates 0A
> as
> > CR\LF. The print statement was causing the issue. To stop this, I added
> > binmode to my file handle:
> >
> > open(OUTPUT,">$outputFileName");
> > binmode OUTPUT;
> >
> > It works great now.
> >
> >
> > ---- Tim Lewis wrote:
> > > I am attempting to add a line feed to the end of each line. When I do
> > this, a carriage return is also added. My code lines are:
> > >
> > > $currentLine = $currentLine . "\x{0A}";
> > > $finalOutput = $finalOutput . $currentLine;
> > >
> > > There has to be a way to do this. Also, is there a better way to
> > concatentate?
> > >
> > > Thanks for any suggestions on this.
> > >
> > > Tim
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > > For additional commands, e-mail: beginners-help@perl.org
> > > http://learn.perl.org/
> > >
> > >
> >
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
> >
>
--
*Satajanus Nig. Ltd
*
--001636e0ba3cc1d99604a91e8466--
Re: Add line feed to line
am 28.07.2011 13:25:58 von Tim Lewis
Thanks!
---- timothy adigun <2teezperl@gmail.com> wrote:
> Tim,
> >>>>>>check this if it answers ur #1 question:
> #!/usr/bib/perl -w
>
> $\="\n"; # with output record separator used you don't ve to use
> # $currentLine = $currentLine . "\x{0A}"; in ur code again
>
> my @arr=qw(item1 item2 item3);
>
> for(@arr){
> print $_; # used $_ default argument
> # your items are separated without stress
>
> }
> >>>>>Also, is there a better way to concatentate?
> You can use join like this:
> #!/usr/bib/perl -w
> use strict;
>
> my @arr=qw(item1 item2 item3); # u can put ur values into an array
>
> $arr=join "*,*",@arr; # use join to ouput all values as a single scalar
> value
> # concatentate any separator u want, here I
> used ","
> # in red colour
> print $arr;
>
> Thanks
>
> On Wed, Jul 27, 2011 at 9:03 PM, Tim Lewis wrote:
>
> > I found an answer that I thought I would share.
> >
> > I am using ActivePerl on Windows server 2003. ActivePerl translates 0A as
> > CR\LF. The print statement was causing the issue. To stop this, I added
> > binmode to my file handle:
> >
> > open(OUTPUT,">$outputFileName");
> > binmode OUTPUT;
> >
> > It works great now.
> >
> >
> > ---- Tim Lewis wrote:
> > > I am attempting to add a line feed to the end of each line. When I do
> > this, a carriage return is also added. My code lines are:
> > >
> > > $currentLine = $currentLine . "\x{0A}";
> > > $finalOutput = $finalOutput . $currentLine;
> > >
> > > There has to be a way to do this. Also, is there a better way to
> > concatentate?
> > >
> > > Thanks for any suggestions on this.
> > >
> > > Tim
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > > For additional commands, e-mail: beginners-help@perl.org
> > > http://learn.perl.org/
> > >
> > >
> >
> >
> > --
> > To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> > For additional commands, e-mail: beginners-help@perl.org
> > http://learn.perl.org/
> >
> >
> >
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 29.07.2011 13:13:17 von Shlomi Fish
On Wed, 27 Jul 2011 15:19:56 -0400
Tim Lewis wrote:
> I am attempting to add a line feed to the end of each line. When I do thi=
s, a
> carriage return is also added. My code lines are:
>=20
> $currentLine =3D $currentLine . "\x{0A}";
> $finalOutput =3D $finalOutput . $currentLine;
>=20
> There has to be a way to do this. Also, is there a better way to
> concatentate?
Yes, there is - see the ".=3D" operator:
$current_line .=3D "\x{0A}";
And this code should not add a carriage return. so you're doing something e=
lse
wrong. Also, in Perl the customary style is to use
$word_separated_by_underscore instead of $camelCase.
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/
Better be a tail for the lions, than the head of the jackals.
â=94 Pirkei Avot, 4 15
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 29.07.2011 13:15:55 von Shlomi Fish
On Wed, 27 Jul 2011 16:03:32 -0400
Tim Lewis wrote:
> I found an answer that I thought I would share.
>=20
> I am using ActivePerl on Windows server 2003. ActivePerl translates 0A as
> CR\LF. =20
That's the case for most Windows Perls.
> The print statement was causing the issue. To stop this, I added
> binmode to my file handle:
>=20
> open(OUTPUT,">$outputFileName");
> binmode OUTPUT;
There's a lot wrong with this code:
1. You're using package-global filehandles.
2. You're using two-args open.
3. You're not checking that open succeeded.
The correct form is:
open my $output_fh, '>', $output_filename
or die "Cannot open '$output_filename' for writing - $!";
binmode($output_fh);
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Original Riddles - http://www.shlomifish.org/puzzles/
Linux â=94 Because Software Problems Should not Cost Money.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 29.07.2011 14:14:37 von Shawn H Corey
On 11-07-29 07:15 AM, Shlomi Fish wrote:
> open my $output_fh, '>', $output_filename
> or die "Cannot open '$output_filename' for writing - $!";
>
> binmode($output_fh);
These can be combined into one statement:
open my $output_fh, '>:raw', $output_filename
or die "Could not open '$output_filename' for writing - $!";
This is one advantage of using the three-argument open.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
"Make something worthwhile." -- The Dear Hunter
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Wanted: Example of asynchronous bidirectional socket client (asocket chat program)
am 29.07.2011 19:36:42 von derykus
On Jul 27, 10:07=A0am, siegfr...@heintze.com wrote:
> Sorry if this appears twice. Since it bounced back to me -- probably
> because of the HTML format -- I'm sending it again.
>
> I did some google searching and I could not find an example of a
> bidirectional asynchronous socket client. A telnet client is an example
> of a bidirectional asynchronous socket client.
>
> I don't specifically want source to a telnet client -- that would be
> much fancier than what I require and would not be helpful if the perl
> only called C++. I just want an example in pure perl (or ruby or
> python).
>
> By asynchronous I mean that reads and writes can occur at any time in
> any order with no warning.
>
> Asynchronous could also mean that we don't block while the =A0write is in
> progress. Blocking while the write is in progress is fine.
>
> I hope it is possible to write such a beast in perl.
>
> Let's suppose I spawn two threads, one to block on a read socket and the
> other to block on the keyboard with $inp=3D (so it can print to th=
e
> write socket when it receives input form the keyboard). Can the first
> thread print to the console if the second thread is blocking on
> $inp=3D?
>
> Surely, someone has posted source to such a beast somewhere on the
> internet!
Although I don't have experience with it., POE ( http://poe.perl.org)
offers extensive resources for this type of application including
tutorials, sample cookbook programs, etc.:
From the POE page above:
POE is a Perl framework for writing reactive programs.
Cooperatively multitasked programs and networking programs
are overlapping subsets of reactive programs.
POE implements a single API and bridges from it to other event
loops. A program using POE can run under any event loop that
POE supports, with little or no modification.
--
Charles DeRykus
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
RE: Add line feed to line
am 03.08.2011 18:41:35 von Tim Lewis
First Microsoft decides that Hungarian Notation is no longer a standard =
in VB; I am still going through withdrawal from that. Now camel Case =
is gone? I will adapt my programming to use the underscores.
Thanks Shlomi. On a side note, the Perl website that you put together =
is fantastic.
-----Original Message-----
From: Shlomi Fish [mailto:shlomif@shlomifish.org]=20
Sent: Friday, July 29, 2011 7:13 AM
To: Tim Lewis
Cc: beginners@perl.org
Subject: Re: Add line feed to line
On Wed, 27 Jul 2011 15:19:56 -0400
Tim Lewis wrote:
> I am attempting to add a line feed to the end of each line. When I do =
this, a
> carriage return is also added. My code lines are:
>=20
> $currentLine =3D $currentLine . "\x{0A}";
> $finalOutput =3D $finalOutput . $currentLine;
>=20
> There has to be a way to do this. Also, is there a better way to
> concatentate?
Yes, there is - see the ".=3D" operator:
$current_line .=3D "\x{0A}";
And this code should not add a carriage return. so you're doing =
something else
wrong. Also, in Perl the customary style is to use
$word_separated_by_underscore instead of $camelCase.
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/
Better be a tail for the lions, than the head of the jackals.
â=94 Pirkei Avot, 4 15
Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 03.08.2011 20:12:02 von Shlomi Fish
On Wed, 3 Aug 2011 12:41:35 -0400
"Tim Lewis" wrote:
> First Microsoft decides that Hungarian Notation is no longer a standard in
> VB; I am still going through withdrawal from that. Now camel Case is go=
ne?
> I will adapt my programming to use the underscores. Thanks Shlomi. On a =
side
> note, the Perl website that you put together is fantastic.
>=20
I'm glad you like it (=3D http://perl-begin.org/ ).
Regards,
Shlomi Fish
--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Apple Inc. is Evil - http://www.shlomifish.org/open-source/anti/apple/
There is an IGLU Cabal, but its only purpose is to deny the existence of an
IGLU Cabal.=20
â=94 Martha Greenberg
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 03.08.2011 21:02:59 von Jim Gibson
On 8/3/11 Wed Aug 3, 2011 9:41 AM, "Tim Lewis"
scribbled:
> First Microsoft decides that Hungarian Notation is no longer a standard in VB;
> I am still going through withdrawal from that. Now camel Case is gone? I
> will adapt my programming to use the underscores.
CamelCase is not gone. Perl will allow you to use any case characters and
underscores in variable names. Shlomi prefers to use lower-case letters and
underscores, and he tells everybody to do likewise. However, you are free to
use whichever style you wish.
People should be careful not to confuse personal preferences with good
programming practices. While there is a benefit to using a consistent style,
the exact style used should be chosen by the people involved, not outsiders.
My advice is to listen to the experienced programmers, then do what you
think is best. Do not adhere to some advice without knowing the reason --
and agreeing with that reason.
Ask the person giving the advice: what is the advantage of underscores over
mixed-case variable names? If it is "readability", why is one style more
readable than the other. Maybe it is just what you are used to.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Add line feed to line
am 03.08.2011 21:38:40 von Rob Dixon
On 03/08/2011 20:02, Jim Gibson wrote:
> On 8/3/11 Wed Aug 3, 2011 9:41 AM, "Tim Lewis"
> scribbled:
>
>> First Microsoft decides that Hungarian Notation is no longer a standard in VB;
>> I am still going through withdrawal from that. Now camel Case is gone? I
>> will adapt my programming to use the underscores.
>
> CamelCase is not gone. Perl will allow you to use any case characters and
> underscores in variable names. Shlomi prefers to use lower-case letters and
> underscores, and he tells everybody to do likewise. However, you are free to
> use whichever style you wish.
>
> People should be careful not to confuse personal preferences with good
> programming practices. While there is a benefit to using a consistent style,
> the exact style used should be chosen by the people involved, not outsiders.
> My advice is to listen to the experienced programmers, then do what you
> think is best. Do not adhere to some advice without knowing the reason --
> and agreeing with that reason.
>
> Ask the person giving the advice: what is the advantage of underscores over
> mixed-case variable names? If it is "readability", why is one style more
> readable than the other. Maybe it is just what you are used to.
I agree that everybody is free to use the style that they prefer, but
unless they are certain that no one else will ever read their software
it is very useful if a program follows a popular standard. Especially on
a beginners' list, where the express purpose is to show people your code
and ask for advice, it is a very good thing if the layout and coding
standards are as transparent as possible.
perldoc perlstyle
is a reasonable authority, and on this matter it says
> * While short identifiers like $gotit are probably ok, use underscores
> to separate words in longer identifiers. It is generally easier to
> read $var_names_like_this than $VarNamesLikeThis, especially for
> non-native speakers of English. It's also a simple rule that works
> consistently with "VAR_NAMES_LIKE_THIS".
>
> Package names are sometimes an exception to this rule. Perl
> informally reserves lowercase module names for "pragma" modules like
> "integer" and "strict". Other modules should begin with a capital
> letter and use mixed case, but probably without underscores due to
> limitations in primitive file systems' representations of module
> names as files that must fit into a few sparse bytes.
However it also says
> Each programmer will, of course, have his or her own preferences in
> regards to formatting, but there are some general guidelines that will
> make your programs easier to read, understand, and maintain.
Cheers,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/