[32209] in Perl-Users-Digest
Perl-Users Digest, Issue: 3474 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Aug 17 06:09:19 2011
Date: Wed, 17 Aug 2011 03:09:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 17 Aug 2011 Volume: 11 Number: 3474
Today's topics:
Re: correct syntax if hash does not exist <rweikusat@mssgmbh.com>
Re: correct syntax if hash does not exist <rweikusat@mssgmbh.com>
Re: correct syntax if hash does not exist <jimsgibson@gmail.com>
Fork (and exec) in a threaded script. <koszalekopalek@interia.pl>
Re: Fork (and exec) in a threaded script. <anfi@onet.eu>
Re: Fork (and exec) in a threaded script. <koszalekopalek@interia.pl>
Re: Fork (and exec) in a threaded script. <koszalekopalek@interia.pl>
Re: Fork (and exec) in a threaded script. <rweikusat@mssgmbh.com>
Re: gui <nickbergeronsemail@gmail.com>
Re: help bad thoughts phones <nickbergeronsemail@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 16 Aug 2011 12:04:22 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: correct syntax if hash does not exist
Message-Id: <8762lxhb2h.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> "John" <john1949@yahoo.com> writes:
>> Part of code.
>>
>> If hash does not exist make it null.
>>
>> Is this the correct syntax?
>>
>> my %data; my $hash=\%data;
>> .......
>> my $error=$hash->{error} or '';
>
> It is correct, but more than a little weird:
There's also a possible precedence problem here: The precedence of
'or' is lower than that of =, meaning,
my $error = $hash->{error} or '';
equivalent to
(my $error = $hash->{error}) or '';
and Perl will quite appropriately warn[*] about that:
[rw@sapphire]~ $perl -cwe 'my $hash; my $error = $hash->{error} or "";'
Useless use of a constant in void context at -e line 1.
-e syntax OK
What was likely intended would look like this:
my $error = $hash->{error} || '';
------------------------------
Date: Tue, 16 Aug 2011 12:07:36 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: correct syntax if hash does not exist
Message-Id: <871uwlhax3.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mssgmbh.com> writes:
> Rainer Weikusat <rweikusat@mssgmbh.com> writes:
[...]
> and Perl will quite appropriately warn[*] about that:
Forgotten footnote: Not using perl compile-time warnings is IMHO very
unwise because (as opposed to, say, gcc warnings) they usually point
at actual problems.
------------------------------
Date: Tue, 16 Aug 2011 10:52:36 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: correct syntax if hash does not exist
Message-Id: <160820111052365731%jimsgibson@gmail.com>
In article <j2d1e6$4vp$1@news.albasani.net>, John <john1949@yahoo.com>
wrote:
> "Rainer Weikusat" <rweikusat@mssgmbh.com> wrote in message
> news:87pqk69609.fsf@sapphire.mobileactivedefense.com...
> >
> > Also, for a sufficiently recent version of Perl,
> >
> > my $error = $hash->{error} // '';
> >
>
> Cannot find any reference to //.
>
> I might as well stick to if (not defined ..
It is new in Perl 5.10. See 'perldoc perlop' and search for "C-style
Logical Defined-Or".
The statement
$x //= $y;
is equivalent to:
$x = defined($x) ? $x : $y;
--
Jim Gibson
------------------------------
Date: Tue, 16 Aug 2011 07:56:37 -0700 (PDT)
From: Koszalek Opalek <koszalekopalek@interia.pl>
Subject: Fork (and exec) in a threaded script.
Message-Id: <e34388f1-db08-4d8e-9a27-4665d17b1ed0@c19g2000yqe.googlegroups.com>
I know that one has to be cautious when using threads and forks in one
script/program (and that applies not only to perl). On the other hand,
a fork() followed immediately be exec() looks like a sensible thing to
do. Is there a way to do it safely in perl?
The script below uses two threads -- one spawns new processes and the
other watches the processes in the %PIDS hash. (It could be easily
rewritten without threads but that is beside the point). The script
usually crashes after just a few seconds under perl 5.8.9. (verified
on Linux/Fedora and FreeBSD). The crash happens also after commenting
out the warn statements.
Is it possible to fix this?
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;
use POSIX ":sys_wait_h";
use constant {
RUNNING => 1,
ERROR => 2
};
my %PIDS : shared = ();
my $STATE : shared = RUNNING;
$| = 1;
$SIG{'CHLD'} = \&reaper;
sub reaper {
lock( %PIDS );
while( my $pid = waitpid( -1, WNOHANG ) ) {
if( WIFEXITED( $? )) {
delete $PIDS{$pid};
}
}
$SIG{'CHLD'} = \&reaper;
}
sub watcher_loop {
warn sprintf( "Thread created: %s", (caller 0)[3]);
my $state = RUNNING;
while( $state == RUNNING ) {
{
lock( %PIDS );
my( $pid, $time );
while (my( $pid, $time ) = ( each %PIDS )) {
if( ! kill 0, $pid ) {
warn sprintf( "Process %d disappeared from the
process list", $pid );
lock( $STATE );
$STATE = ERROR;
}
}
}
{
lock( $STATE );
$state = $STATE;
cond_timedwait( $STATE, 3 );
}
}
}
sub spawn_loop {
warn sprintf( "Thread created: %s", (caller 0)[3]);
my $state = RUNNING;
while( $state == RUNNING ) {
my $cnt;
{
lock( %PIDS );
$cnt = scalar( keys %PIDS );
}
if( $cnt < 3 ) {
warn "$cnt processes running. Will spawn a new process
now.";
my $pid;
if( $pid = fork ) {
lock( %PIDS );
$PIDS{$pid} = time;
}
else {
exec( '( ls -l; sleep 1 ) > /dev/null' );
}
}
{
lock( $STATE );
$state = $STATE;
cond_timedwait( $STATE, 3 );
}
}
}
my @threads = (
threads->new( \&watcher_loop ),
threads->new( \&spawn_loop ),
);
warn sprintf( "Joining %d threads.", scalar @threads );
for( @threads ) {
$_->join;
}
------------------------------
Date: Tue, 16 Aug 2011 17:24:06 +0200
From: Andrzej Adam Filip <anfi@onet.eu>
Subject: Re: Fork (and exec) in a threaded script.
Message-Id: <phz0tjemt1-B8G@jennifer.huge.strangled.net>
Koszalek Opalek <koszalekopalek@interia.pl> wrote:
> I know that one has to be cautious when using threads and forks in one
> script/program (and that applies not only to perl). On the other hand,
> a fork() followed immediately be exec() looks like a sensible thing to
> do. Is there a way to do it safely in perl?
>
> The script below uses two threads -- one spawns new processes and the
> other watches the processes in the %PIDS hash. (It could be easily
> rewritten without threads but that is beside the point). The script
> usually crashes after just a few seconds under perl 5.8.9. (verified
> on Linux/Fedora and FreeBSD). The crash happens also after commenting
> out the warn statements.
>
> Is it possible to fix this?
> [...]
Wording of "man threads" suggest setting signals handlers *in threads*
themselves - you seem to set the handlers only in main thread.
--
[pl>en Andrew] Andrzej A. Filip : anfi@onet.eu : Andrzej.Filip@gmail.com
Never put off till run-time what you can do at compile-time.
-- D. Gries
------------------------------
Date: Tue, 16 Aug 2011 09:17:44 -0700 (PDT)
From: Koszalek Opalek <koszalekopalek@interia.pl>
Subject: Re: Fork (and exec) in a threaded script.
Message-Id: <0b1f0c62-63ef-4b81-859d-bc0e4eecfd9b@br5g2000vbb.googlegroups.com>
On Aug 16, 6:24=A0pm, Andrzej Adam Filip <a...@onet.eu> wrote:
> Wording of "man threads" suggest setting signals handlers *in threads*
> themselves - you seem to set the handlers only in main thread.
It only says it's possible to send signals to individual threads.
Anyway, I was unable to make this work for SIG{'CHLD'}. (Ignoring
the signal in one thread and handling it with waitpid in another
left me with zoombie processes.)
K.
------------------------------
Date: Tue, 16 Aug 2011 09:34:38 -0700 (PDT)
From: Koszalek Opalek <koszalekopalek@interia.pl>
Subject: Re: Fork (and exec) in a threaded script.
Message-Id: <64e16eb2-83cc-4a59-b45a-593c6f9fa10c@c19g2000yqe.googlegroups.com>
On Aug 16, 5:56=A0pm, Koszalek Opalek <koszalekopa...@interia.pl> wrote:
> I know that one has to be cautious when using threads and forks in one
> script/program (and that applies not only to perl). On the other hand,
> a fork() followed immediately be exec() looks like a sensible thing to
> do. Is there a way to do it safely in perl?
I am unable to crash the script I posted on perl 5.12.
However, after a while it gets stuck and the following process tree:
$ ps -o pid,etime,state,args -t pts/18 --forest
PID ELAPSED S COMMAND
2775 35:36 S /bin/bash
9318 00:02 S \_ /usr/bin/perl ./aa.pl
9333 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9335 00:00 S | \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9339 00:00 S | \_ sleep 1
9334 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9338 00:00 S | \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9341 00:00 S | \_ sleep 1
9336 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9342 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
9344 00:00 S \_ sleep 1
is changed into something like this:
$ ps -o pid,etime,state,args -t pts/18 --forest
PID ELAPSED S COMMAND
2775 35:31 S /bin/bash
9060 01:36 S \_ /usr/bin/perl ./aa.pl
9078 01:35 S \_ /usr/bin/perl ./aa.pl
9205 01:21 S \_ /usr/bin/perl ./aa.pl
9250 01:11 Z \_ [sh] <defunct>
So is it me or perl?
K.
------------------------------
Date: Tue, 16 Aug 2011 17:38:37 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Fork (and exec) in a threaded script.
Message-Id: <8762lxfh0y.fsf@sapphire.mobileactivedefense.com>
Koszalek Opalek <koszalekopalek@interia.pl> writes:
> On Aug 16, 5:56 pm, Koszalek Opalek <koszalekopa...@interia.pl> wrote:
>
>> I know that one has to be cautious when using threads and forks in one
>> script/program (and that applies not only to perl). On the other hand,
>> a fork() followed immediately be exec() looks like a sensible thing to
>> do. Is there a way to do it safely in perl?
>
>
> I am unable to crash the script I posted on perl 5.12.
>
> However, after a while it gets stuck and the following process tree:
>
> $ ps -o pid,etime,state,args -t pts/18 --forest
> PID ELAPSED S COMMAND
> 2775 35:36 S /bin/bash
> 9318 00:02 S \_ /usr/bin/perl ./aa.pl
> 9333 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9335 00:00 S | \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9339 00:00 S | \_ sleep 1
> 9334 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9338 00:00 S | \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9341 00:00 S | \_ sleep 1
> 9336 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9342 00:00 S \_ sh -c ( ls -l; sleep 1 ) > /dev/null
> 9344 00:00 S \_ sleep 1
>
> is changed into something like this:
>
> $ ps -o pid,etime,state,args -t pts/18 --forest
> PID ELAPSED S COMMAND
> 2775 35:31 S /bin/bash
> 9060 01:36 S \_ /usr/bin/perl ./aa.pl
> 9078 01:35 S \_ /usr/bin/perl ./aa.pl
> 9205 01:21 S \_ /usr/bin/perl ./aa.pl
> 9250 01:11 Z \_ [sh] <defunct>
>
> So is it me or perl?
You are quite obviously using POSIX threads and this means that a
signal sent to the process can be handled by any thread not currently
blocking it. This includes your 'main program' thread that blocks
forever in join.
------------------------------
Date: Tue, 16 Aug 2011 09:05:59 -0700 (PDT)
From: Miro <nickbergeronsemail@gmail.com>
Subject: Re: gui
Message-Id: <fed9763c-a311-4c46-bdce-baaac3b9d69f@f7g2000vba.googlegroups.com>
On Aug 15, 12:31=A0am, robin <r...@thevoid1.net> wrote:
> anyone know of any NEW perl gui toolkits?
> r...@thevoid1.net
> -r
wX is pretty nice.
------------------------------
Date: Tue, 16 Aug 2011 09:02:48 -0700 (PDT)
From: Miro <nickbergeronsemail@gmail.com>
Subject: Re: help bad thoughts phones
Message-Id: <6c11d3d2-e992-4a9c-8d4a-9d6860372997@j1g2000yqn.googlegroups.com>
On Aug 15, 9:26=A0pm, robin <r...@thevoid1.net> wrote:
> stop them with curing charm rigs thoughts phones :-)
What does this have anything to do with Perl? Don't tell me this is
the new RegEx system or something.
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3474
***************************************