[32764] in Perl-Users-Digest
Perl-Users Digest, Issue: 4028 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 7 14:09:37 2013
Date: Sat, 7 Sep 2013 11:09:06 -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 Sat, 7 Sep 2013 Volume: 11 Number: 4028
Today's topics:
eval package <gravitalsun@hotmail.foo>
Re: eval package <gravitalsun@hotmail.foo>
Re: eval package <rvtol+usenet@xs4all.nl>
exec and named pipe questions <dave@invalid.invalid>
Re: exec and named pipe questions <rweikusat@mobileactivedefense.com>
Re: exec and named pipe questions <dave@invalid.invalid>
skipping blank array items <cartercc@gmail.com>
Re: skipping blank array items <rweikusat@mobileactivedefense.com>
Re: skipping blank array items <glex_no-spam@qwest-spam-no.invalid>
Re: skipping blank array items <cartercc@gmail.com>
Re: skipping blank array items <cartercc@gmail.com>
Re: skipping blank array items <rweikusat@mobileactivedefense.com>
Re: skipping blank array items <rweikusat@mobileactivedefense.com>
Re: skipping blank array items <gravitalsun@hotmail.foo>
Re: skipping blank array items <glex_no-spam@qwest-spam-no.invalid>
Re: skipping blank array items <cartercc@gmail.com>
Re: skipping blank array items <ben@morrow.me.uk>
Re: skipping blank array items <rweikusat@mobileactivedefense.com>
Re: skipping blank array items <rweikusat@mobileactivedefense.com>
Re: thread problem <rweikusat@mobileactivedefense.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 07 Sep 2013 01:18:11 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: eval package
Message-Id: <l0dkb4$1i9h$1@news.ntua.gr>
I have a module /SomeDir/SomeCode.pm with a package name retrieved from
its dynamic name, but when I use eval
( my $PackageName = __FILE__ ) =~s/(?i)^.*?([^\/]+?)\.pm$/$1/;
eval "package $PackageName;";
...
the calling script can not find its subroutines.
( the $PackageName has the correct value "SomeCode" )
If i remove eval and just say
package SomeCode;
...
everything is ok; What happens here ?
------------------------------
Date: Sat, 07 Sep 2013 18:58:28 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: eval package
Message-Id: <l0fif6$30ma$1@news.ntua.gr>
Στις 7/9/2013 01:18, ο/η George Mpouras έγραψε:
> I have a module /SomeDir/SomeCode.pm with a package name retrieved from
> its dynamic name, but when I use eval
>
>
> ( my $PackageName = __FILE__ ) =~s/(?i)^.*?([^\/]+?)\.pm$/$1/;
> eval "package $PackageName;";
>
> ...
>
>
> the calling script can not find its subroutines.
> ( the $PackageName has the correct value "SomeCode" )
> If i remove eval and just say
>
>
> package SomeCode;
> ...
>
> everything is ok; What happens here ?
i do not think there is a solution to that. eval is pushing the
evaluated code one scope deeper than the current, so actually it is not
the same package any more.
------------------------------
Date: Sat, 07 Sep 2013 18:31:05 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: eval package
Message-Id: <522b54c9$0$15873$e4fe514c@news2.news.xs4all.nl>
On 07/09/2013 17:58, George Mpouras wrote:
> Στις 7/9/2013 01:18, ο/η George Mpouras έγραψε:
>> I have a module /SomeDir/SomeCode.pm with a package name retrieved from
>> its dynamic name, but when I use eval
>>
>> ( my $PackageName = __FILE__ ) =~s/(?i)^.*?([^\/]+?)\.pm$/$1/;
>> eval "package $PackageName;";
>>
>> the calling script can not find its subroutines.
>> ( the $PackageName has the correct value "SomeCode" )
>> If i remove eval and just say
>>
>> package SomeCode;
>>
>> everything is ok; What happens here ?
>
>
>
> i do not think there is a solution to that. eval is pushing the
> evaluated code one scope deeper than the current, so actually it is not
> the same package any more.
Just put the code in the same string. But what on earth are you trying
to achieve? Maybe you are looking for AutoLoader?
--
Ruud
------------------------------
Date: Sat, 7 Sep 2013 14:14:24 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-7C2NL4RQbJPu@paddington.bear.den>
I am writing a perl daemon on a Raspberry Pi.
The perl script talks and listens to another process that it has
started via fork/exec.
Normally when one forks it is usual to close unneeded file handles -
the first question then is should one close *all* the open handles if
you are going to call exec anyway?
Secondly, I was under the impression that it did not matter in which
order named pipes are opened. The forked process is reading one named
pipe and writing to a second. But more often than not my perl script
hangs trying to open one.
open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
$!";
never returns.
With two xterms I can "echo hi > pipe" and "cat < pipe" and it matters
not which order I do them in - the first waits until the second runs.
But surely open should not be trying to read should it?
TIA
--
Regards
Dave Saville
------------------------------
Date: Sat, 07 Sep 2013 16:29:13 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: exec and named pipe questions
Message-Id: <87ppsksl9y.fsf@sapphire.mobileactivedefense.com>
"Dave Saville" <dave@invalid.invalid> writes:
> I am writing a perl daemon on a Raspberry Pi.
>
> The perl script talks and listens to another process that it has
> started via fork/exec.
>
> Normally when one forks it is usual to close unneeded file handles -
> the first question then is should one close *all* the open handles if
> you are going to call exec anyway?
Unintended filehandle inheritance across exec can cause serious
problems which are difficult to debug, especially as the new program
could, in turn, pass an accidentally inherited handle further on to
other program started by it and so forth. The 'usual' problem case
would be a file handle 'sitting' on some global resource the
original program needs as well, eg, a listening TCP socket bound to
some address: When such a filehandle has been leaked to some random
other process and the process it originated from terminates, the
program which created the listening socket can't be started until the
stray file descriptor has been found and eliminated.
The 'simple' solution to this problem is to set the FD_CLOEXEC flag
for all long-lived open filehandles of a process except if they're
supposed to be inherited accross an exec. Usually, perl does this
automatically (see perlvar documentation for $^F for more details
about that).
There's generally no reason to close file handles explictly in Perl as
this will either be done automatically when the corresponding file
handle variable goes out of scope or during 'global destruction',
traditional nonsense in certain Linux man pages notwithstanding
(close(2) does not imply flushing kernel buffers, hence, close
returning 'everythings fine' does not mean silent loss of data won't
happen).
> Secondly, I was under the impression that it did not matter in which
> order named pipes are opened. The forked process is reading one named
> pipe and writing to a second. But more often than not my perl script
> hangs trying to open one.
>
> open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> $!";
Opening a FIFO for reading will block until 'something else' opens it
for writing.
------------------------------
Date: Sat, 7 Sep 2013 15:50:17 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: exec and named pipe questions
Message-Id: <fV45K0OBJxbE-pn2-jOFEY8PdqqMX@paddington.bear.den>
On Sat, 7 Sep 2013 15:29:13 UTC, Rainer Weikusat
<rweikusat@mobileactivedefense.com> wrote:
<snip>
> The 'simple' solution to this problem is to set the FD_CLOEXEC flag
> for all long-lived open filehandles of a process except if they're
> supposed to be inherited accross an exec. Usually, perl does this
> automatically (see perlvar documentation for $^F for more details
> about that).
<snip>
Thank you for the explanation - will look into that reference.
> > Secondly, I was under the impression that it did not matter in which
> > order named pipes are opened. The forked process is reading one named
> > pipe and writing to a second. But more often than not my perl script
> > hangs trying to open one.
> >
> > open my $_STDOUT, '<', 'fifo_stdout' or die "Can't open fifo_stdout
> > $!";
>
> Opening a FIFO for reading will block until 'something else' opens it
> for writing.
I had worked that out by now :-) - Google suggests cheating by opening
+< which seems to work fine.
--
Regards
Dave Saville
------------------------------
Date: Fri, 6 Sep 2013 11:56:30 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: skipping blank array items
Message-Id: <1d97a566-d8d0-4d6a-a347-af2553b2d4e2@googlegroups.com>
I have a csv file with 10K items. The header looks something like this:
CustNo,Name,FirstPaidItem,FirstOrderedItem,FirstShippedItem,Items
The first five fields are single valued. The last field (Items) has many it=
ems. Lines may look like this:
1,Joe,a,a,a,a,b,c,d,e,f,g,h,i
2,Jane,a,a,b,a,,,,,,a,b,c,d,e
3,Jim,b,a,a,a,b,c,d,e,f,g,h,i
4,Jill,b,b,b,b,,,,,c,,b,,e,f,g,h
I parse the file like this, putting the first five values in scalars and th=
e remainder in the @items array:
foreach line
my ($custno,$name,$paid,$ordered,$shipped,@items) =3D split on the commas
The objective is to take the first item in the @items array and match it to=
one (or more) of $paid, $ordered, or $shipped. I just want to grab the fir=
st item in the @items array that has a value, IOW, skip all the blank items=
(as in line 2 above) that precede the first actual value.
Is there a quick and dirty way to do this without having to do this>
my $item =3D '';
foreach my $ele (@items)
{
$item =3D $ele if $ele =3D~ /\w/;
last;
}
Thanks, CC.
------------------------------
Date: Fri, 06 Sep 2013 20:45:42 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: skipping blank array items
Message-Id: <87k3itag49.fsf@sapphire.mobileactivedefense.com>
ccc31807 <cartercc@gmail.com> writes:
> I have a csv file with 10K items. The header looks something like this:
> CustNo,Name,FirstPaidItem,FirstOrderedItem,FirstShippedItem,Items
>
> The first five fields are single valued. The last field (Items) has many items. Lines may look like this:
>
> 1,Joe,a,a,a,a,b,c,d,e,f,g,h,i
> 2,Jane,a,a,b,a,,,,,,a,b,c,d,e
> 3,Jim,b,a,a,a,b,c,d,e,f,g,h,i
> 4,Jill,b,b,b,b,,,,,c,,b,,e,f,g,h
>
> I parse the file like this, putting the first five values in scalars and the remainder in the @items array:
> foreach line
> my ($custno,$name,$paid,$ordered,$shipped,@items) = split on the commas
>
> The objective is to take the first item in the @items array and match it to one (or more) of $paid, $ordered, or $shipped. I just want to grab the first item in the @items array that has a value, IOW, skip all the blank items (as in line 2 above) that precede the first actual value.
>
> Is there a quick and dirty way to do this without having to do this>
>
> my $item = '';
> foreach my $ele (@items)
> {
> $item = $ele if $ele =~ /\w/;
> last;
> }
use List::Util qw(first);
$item = first { /\w/ } @items;
$item = first { length() } @items;
But this is also trivial without using any module:
length() and $item = $_, last for @items;
(/\w/) and $item = $_, last for @items;
------------------------------
Date: Fri, 06 Sep 2013 14:47:08 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: skipping blank array items
Message-Id: <522a313c$0$9078$815e3792@news.qwest.net>
On 09/06/13 13:56, ccc31807 wrote:
> I have a csv file with 10K items. The header looks something like this:
> CustNo,Name,FirstPaidItem,FirstOrderedItem,FirstShippedItem,Items
>
> The first five fields are single valued. The last field (Items) has many items. Lines may look like this:
>
> 1,Joe,a,a,a,a,b,c,d,e,f,g,h,i
> 2,Jane,a,a,b,a,,,,,,a,b,c,d,e
> 3,Jim,b,a,a,a,b,c,d,e,f,g,h,i
> 4,Jill,b,b,b,b,,,,,c,,b,,e,f,g,h
>
> I parse the file like this, putting the first five values in scalars and the remainder in the @items array:
> foreach line
> my ($custno,$name,$paid,$ordered,$shipped,@items) = split on the commas
If items doesn't need to be an array...
my ($custno,$name,$paid,$ordered,$shipped,$items) = split(/,/,$line,6);
my ( $item ) = $items =~ /(\w)/;
>
> The objective is to take the first item in the @items array and match it to one (or more) of $paid, $ordered, or $shipped. I just want to grab the first item in the @items array that has a value, IOW, skip all the blank items (as in line 2 above) that precede the first actual value.
>
> Is there a quick and dirty way to do this without having to do this>
>
> my $item = '';
> foreach my $ele (@items)
> {
> $item = $ele if $ele =~ /\w/;
> last;
> }
>
> Thanks, CC.
------------------------------
Date: Fri, 6 Sep 2013 13:06:29 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: skipping blank array items
Message-Id: <ee65cd68-337e-4486-8883-a3607eace471@googlegroups.com>
On Friday, September 6, 2013 3:45:42 PM UTC-4, Rainer Weikusat wrote:
> use List::Util qw(first);
>
>
>
> $item = first { /\w/ } @items;
> $item = first { length() } @items;
>
>
>
> But this is also trivial without using any module:
>
>
>
> length() and $item = $_, last for @items;
> (/\w/) and $item = $_, last for @items;
FOR iterates through the array. I have not looked at List::Util but I will tomorrow. I wanted to avoid having to iterate through the array, which may have 1000 items, and the first item with an actual value may be 500 places down.
Thanks, CC.
------------------------------
Date: Fri, 6 Sep 2013 13:15:11 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: skipping blank array items
Message-Id: <ae15c70e-0370-4407-9e36-dc0b6eeaf3a8@googlegroups.com>
On Friday, September 6, 2013 3:47:08 PM UTC-4, J. Gleixner wrote:
> If items doesn't need to be an array...
@items does not need to be an array, it could be a string. The database I m=
ainly use is UniData, a multi-valued database where fields do not contain s=
calar values. Over the years, as a general rule, I find that using arrays r=
ather than strings gives more consistent results overall, but there is no t=
echnical reason for using an array rather than a string.
> my ($custno,$name,$paid,$ordered,$shipped,$items) =3D split(/,/,$line,6);
> my ( $item ) =3D $items =3D~ /(\w)/;
Actually, all my 'items' consist of two digits, a forward slash, a characte=
r and another digit, like this: 13/T1. So the regex would be something like=
this:
$items =3D~ m!,(\d\d/\w\d),!;
$item =3D $1;
Just off hand, is using a regex much faster than iterating through arrays? =
My script takes about 30 seconds to run and speed is not critical (I have a=
ll day to run it). What offended me was having to iterate through an array =
with potentially 1,000 elements just to grab the first one with a value.
Thanks, CC.
------------------------------
Date: Fri, 06 Sep 2013 21:21:16 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: skipping blank array items
Message-Id: <87fvthaegz.fsf@sapphire.mobileactivedefense.com>
ccc31807 <cartercc@gmail.com> writes:
> On Friday, September 6, 2013 3:45:42 PM UTC-4, Rainer Weikusat wrote:
>> use List::Util qw(first);
>>
>>
>>
>> $item = first { /\w/ } @items;
>> $item = first { length() } @items;
>>
>>
>>
>> But this is also trivial without using any module:
>>
>>
>>
>> length() and $item = $_, last for @items;
>> (/\w/) and $item = $_, last for @items;
>
> FOR iterates through the array. I have not looked at List::Util but
> I will tomorrow. I wanted to avoid having to iterate through the
> array, which may have 1000 items, and the first item with an actual
> value may be 500 places down.
Eh ... provided you have an array with a number of empty leading
elements you want to skip but you don't know how many, how do you
propose to do that without 'looping through the array'?
------------------------------
Date: Fri, 06 Sep 2013 21:33:18 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: skipping blank array items
Message-Id: <87bo45adwx.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> ccc31807 <cartercc@gmail.com> writes:
>> On Friday, September 6, 2013 3:45:42 PM UTC-4, Rainer Weikusat wrote:
[...]
>>> length() and $item = $_, last for @items;
>>> (/\w/) and $item = $_, last for @items;
>>
>> FOR iterates through the array. I have not looked at List::Util but
>> I will tomorrow. I wanted to avoid having to iterate through the
>> array, which may have 1000 items, and the first item with an actual
>> value may be 500 places down.
>
> Eh ... provided you have an array with a number of empty leading
> elements you want to skip but you don't know how many, how do you
> propose to do that without 'looping through the array'?
Byzantinely recursive implementation:
sub fne { return (shift or &fne); }
NB: This will also skip over zeroes.
------------------------------
Date: Fri, 06 Sep 2013 23:49:44 +0300
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: skipping blank array items
Message-Id: <l0df58$1409$1@news.ntua.gr>
Στις 6/9/2013 23:06, ο/η ccc31807 έγραψε:
> On Friday, September 6, 2013 3:45:42 PM UTC-4, Rainer Weikusat wrote:
>> use List::Util qw(first);
>>
>>
>>
>> $item = first { /\w/ } @items;
>> $item = first { length() } @items;
>>
>>
>>
>> But this is also trivial without using any module:
>>
>>
>>
>> length() and $item = $_, last for @items;
>> (/\w/) and $item = $_, last for @items;
>
> FOR iterates through the array. I have not looked at List::Util but I will tomorrow. I wanted to avoid having to iterate through the array, which may have 1000 items, and the first item with an actual value may be 500 places down.
>
> Thanks, CC.
>
what you can do is to run the two scripts:
a) one that building a trie index from your csv file
b) a second that give you the answers instantly without "fors" from the
index of of the a) script
but since you want only one execution per day I do not if this make
sense, also it is not quick and dirty
------------------------------
Date: Fri, 06 Sep 2013 16:47:52 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: skipping blank array items
Message-Id: <522a4d88$0$73610$815e3792@news.qwest.net>
On 09/06/13 15:15, ccc31807 wrote:
> On Friday, September 6, 2013 3:47:08 PM UTC-4, J. Gleixner wrote:
>> If items doesn't need to be an array...
>
> @items does not need to be an array, it could be a string. [...]
>> my ($custno,$name,$paid,$ordered,$shipped,$items) = split(/,/,$line,6);
>> my ( $item ) = $items =~ /(\w)/;
>
> Actually, all my 'items' consist of two digits, a forward slash, a character and another digit, like this: 13/T1.
Oh, I guess my ESP isn't working again. Had you
provided that in your original post, you may have
received a more accurate solution. Regardless, we
don't really need to know your correct regex, you can
adjust it as needed without re-posting.
>So the regex would be something like this:
> $items =~ m!,(\d\d/\w\d),!;
Nope, the ',' in there avoid matching the first or last value. No need
to include the separator, in the regex, in this case.
> $item = $1;
Always test that the match was successful, never blindly assign $1.
dumb example...
$str = '123';
$str =~ /(\d+)/;
$val = $1;
$str =~ /(blah)/;
$val2 = $1; #oops
>
> Just off hand, is using a regex much faster than iterating through arrays? My script takes about 30 seconds to run and speed is not critical (I have all day to run it). What offended me was having to iterate through an array with potentially 1,000 elements just to grab the first one with a value.
You can answer that yourself using the Benchmark module.
------------------------------
Date: Fri, 6 Sep 2013 18:26:43 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: skipping blank array items
Message-Id: <b1c76330-4b18-418a-8895-aef37012bd03@googlegroups.com>
On Friday, September 6, 2013 4:33:18 PM UTC-4, Rainer Weikusat wrote:
> sub fne { return (shift or &fne); }
I like that.
It reminds me of how I discovered to transpose a matrix in Common Lisp.
(defun transpose (matrix) (apply #'mapcar #'list matrix))
CC.
------------------------------
Date: Sat, 7 Sep 2013 02:58:00 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: skipping blank array items
Message-Id: <8qdqfa-adu.ln1@anubis.morrow.me.uk>
Quoth ccc31807 <cartercc@gmail.com>:
>
> Actually, all my 'items' consist of two digits, a forward slash, a
> character and another digit, like this: 13/T1. So the regex would be
> something like this:
> $items =~ m!,(\d\d/\w\d),!;
> $item = $1;
my ($item) = $items =~ m!(?:^|,)(\d\d/\w\d)(?:,|$)!a;
Add /x as desired, or use
my $comma = qr/^|$|,/;
> Just off hand, is using a regex much faster than iterating through
> arrays? My script takes about 30 seconds to run and speed is not
> critical (I have all day to run it). What offended me was having to
> iterate through an array with potentially 1,000 elements just to grab
> the first one with a value.
Using a regex will be faster than splitting the string and then
iterating over the results, since you only have to scan the string once.
It should be fairly obvious that you can't avoid scanning the string as
far as the first valid item, at minimum, so the pattern match will be as
fast as anything can be.
I would consider the split/first solution cleaner than the pattern
match.
Ben
------------------------------
Date: Sat, 07 Sep 2013 12:01:00 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: skipping blank array items
Message-Id: <8738pgkiab.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth ccc31807 <cartercc@gmail.com>:
>>
>> Actually, all my 'items' consist of two digits, a forward slash, a
>> character and another digit, like this: 13/T1. So the regex would be
>> something like this:
>> $items =~ m!,(\d\d/\w\d),!;
>> $item = $1;
>
> my ($item) = $items =~ m!(?:^|,)(\d\d/\w\d)(?:,|$)!a;
>
> Add /x as desired, or use
>
> my $comma = qr/^|$|,/;
$items =~ /([^,])+/
?
------------------------------
Date: Sat, 07 Sep 2013 17:11:10 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: skipping blank array items
Message-Id: <8761uc8vdt.fsf@sapphire.mobileactivedefense.com>
Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
> Rainer Weikusat <rweikusat@mobileactivedefense.com> writes:
>> ccc31807 <cartercc@gmail.com> writes:
>>> On Friday, September 6, 2013 3:45:42 PM UTC-4, Rainer Weikusat wrote:
>
> [...]
>
>>>> length() and $item = $_, last for @items;
>>>> (/\w/) and $item = $_, last for @items;
>>>
>>> FOR iterates through the array. I have not looked at List::Util but
>>> I will tomorrow. I wanted to avoid having to iterate through the
>>> array, which may have 1000 items, and the first item with an actual
>>> value may be 500 places down.
>>
>> Eh ... provided you have an array with a number of empty leading
>> elements you want to skip but you don't know how many, how do you
>> propose to do that without 'looping through the array'?
[...]
> sub fne { return (shift or &fne); }
>
> NB: This will also skip over zeroes.
It will also recurse forever (until perl aborts the recursion) if the
argument list didn't contain something which is regarded as true when
used in a boolean expression. This could be fixed with the even more
byzantine
sub fne { return @_ && (shift || &fne) || undef; }
------------------------------
Date: Fri, 06 Sep 2013 16:21:16 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: thread problem
Message-Id: <871u52ascz.fsf@sapphire.mobileactivedefense.com>
"Dave Saville" <dave@invalid.invalid> writes:
[...]
> use strict;
> use warnings;
> use threads;
> use threads::shared;
> my $foo = 'foo';
> threads->create({'void' => 1}, 'try');
> sleep 2;
> print "$foo - done\n";
> sleep 5;
> sub try
> {
> threads->detach();
> $foo = 'bar';
> sleep 5;
> return;
> }
>
> Why isn't $foo changing in the main thread?
Because you didn't declare it as shared variable.
----------
use strict;
use warnings;
use threads;
use threads::shared;
my $foo : shared = 'foo';
threads->create({'void' => 1}, 'try');
sleep 2;
print "$foo - done\n";
sleep 5;
sub try
{
threads->detach();
$foo = 'bar';
sleep 5;
return;
}
---------
[...]
> What I am trying to do is use a thread to essentially tail a pipe and
> raise a flag in main if it sees a certain string appear.The main
> thread can't tail it because I don't want to block.
Using threads in this way, especially perl ithreads, is almost
certainly not a good idea. What's your main thread doing? If it is
'handling other I/O', you should consider using select or a module
providing access to a similar system call. The IO::Poll
implementations is beyond braindead. OTOH, IO::EPoll is only 'slightly
bizarre', eg, it uses 3 hashes to map file handles to various things
instead of putting all "per filehandle" attributes into a suitable
object and let the kernel maintain a pointer to that, something which
also works in perl 5 because its automatich memory management system
wasn't built on the wrong assumption that pointer to objects cannot
exist outside of the address space of the process which created them.
------------------------------
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 4028
***************************************