[12800] in Perl-Users-Digest
Perl-Users Digest, Issue: 210 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 21 09:07:17 1999
Date: Wed, 21 Jul 1999 06:05: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, 21 Jul 1999 Volume: 9 Number: 210
Today's topics:
Re: basename regexp? (elephant)
Re: lexical $_ with threads question? <tchrist@mox.perl.com>
problem with hash of arrays <badri@wpi.edu>
Digest Administrivia (Last modified: 1 Jul 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 21 Jul 1999 22:46:14 +1000
From: e-lephant@b-igpond.com (elephant)
Subject: Re: basename regexp?
Message-Id: <MPG.12006c22c95e62989b60@news-server>
Bart Lateur writes ..
>elephant wrote:
>>is this just an ActiveState thing .. anyone else ? .. should we be
>>reporting a bug ?
>
>No, I think it's even worse than that. It depends on how you called the
>script, andI think this is the general rule. If you use a relative path
>when invoking the script, $0 will probably contain a relative path.
seems that inconsistency is the name of the game with ye olde $0 .. I'm
sticking with the module
--
jason - remove all hyphens for email reply -
------------------------------
Date: 21 Jul 1999 06:35:44 -0700
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: lexical $_ with threads question?
Message-Id: <3795bea0@cs.colorado.edu>
[courtesy cc of this posting mailed to cited author]
In comp.lang.perl.misc,
Olivier Dehon <dehon_olivier@jpmorgan.com> writes:
:Tom Christiansen <tchrist@mox.perl.com> writes:
:
:> :If lexicals don't do anything special when a new thread is created,
:> :what was the reason for turning $_ and @_ into lexicals
:> Certainly without per-thread versions, things like grep/sort/foreach
:> would interfere with other threads in a very nasty way. Imagine
:> one thread changing them out from under another thread's execution.
:
:As $_ is turned into a lexical, does that mean that one can create
:closures using $_ (eg. in a foreach loop),
Apparently. See below.
:or are there restrictions?
Like what?
Here's the example:
#!/usr/local/bin/filsperl
use Thread qw/async/;
$_ = 17;
printf "thread %d has dollar underscore value $_\n",
Thread->self->tid();
for (1 .. 4) {
push @t, async {
sleep 1;
printf "thread %d has dollar underscore value $_\n",
Thread->self->tid();
}
}
$_ *= 2;
print "main joining\n";
for (@t) { $_->join() }
printf "thread %d has dollar underscore value $_\n",
Thread->self->tid();
Which produces this output:
thread 0 has dollar underscore value 17
main joining
thread 1 has dollar underscore value 1
thread 3 has dollar underscore value 3
thread 2 has dollar underscore value 2
thread 4 has dollar underscore value 4
thread 0 has dollar underscore value 34
And just so traditional multitasking people don't feel left out,
here's a forking version (sounds obscene, doesn't it? :-)
#!/usr/local/bin/perl
sub async(&) {
my $pid = fork();
return $pid if $pid;
die "$0 $$: cannot fork: $!" unless defined $pid;
my $code = shift;
&$code();
exit;
}
$_ = 17;
printf "pid $$ has dollar underscore value $_\n";
for (1 .. 4) {
push @pids, async {
sleep 1;
printf "pid $$ has dollar underscore value $_\n";
}
}
$_ *= 2;
print "main waiting\n";
for (@pids) { waitpid($_, 0) }
printf "pid $$ has dollar underscore value $_\n";
Which produces (when run from a pipe from my editor):
pid 19989 has dollar underscore value 17
pid 19990 has dollar underscore value 1
pid 19989 has dollar underscore value 17
pid 19992 has dollar underscore value 3
pid 19989 has dollar underscore value 17
pid 19991 has dollar underscore value 2
pid 19989 has dollar underscore value 17
pid 19993 has dollar underscore value 4
pid 19989 has dollar underscore value 17
main waiting
pid 19989 has dollar underscore value 34
Uh oh! I'm glad that Sarathy fixed that so I don't have to remember
to set $| to avoid duplicate output to set $| to avoid duplicate output
from the fork.
Here's the same program, run under 5.005_57 instead.
pid 20018 has dollar underscore value 17
pid 20019 has dollar underscore value 1
pid 20022 has dollar underscore value 4
pid 20021 has dollar underscore value 3
pid 20020 has dollar underscore value 2
main waiting
pid 20018 has dollar underscore value 34
In the mean time, you can set $| = 1 to produce the same effect.
--tom
--
"Make is like Pascal: everybody likes it, so they go in and change it. "
--Dennis Ritchie
------------------------------
Date: Tue, 20 Jul 1999 19:58:14 -0400
From: badri <badri@wpi.edu>
Subject: problem with hash of arrays
Message-Id: <37950D15.52A6928A@wpi.edu>
Hi,
This a piece of code that I used to create a hash of arrays. Each array
has 256 elements. For any new key, when I try to create an initial list
of numbers it works fine. But when I try to retrieve a reference to the
elements of an already present key from the hash to manipulate the
list, it gives a segmentation fault.
--- Code starts here ----
#!/usr/bin/perl -w
# Initialization of the variables ;
$pos = 0;
$number = 0;
use MLDBM 'DB_File';
use Fcntl;
tie (%hash, 'MLDBM', 'testfile.db', O_CREAT|O_RDWR, 0660) or die $!;
# to construct a meaningful hash, we first get a "key" from the user
print "Please enter a Key: ";
chomp($key = <STDIN>);
print "Enter the index to be modified: ";
chomp($pos = <STDIN>);
print "Enter the new number: ";
chomp($number = <STDIN>);
# and now two values associated with that key
if (!defined($hash{$key}))
{
print "New key..\n";
$#temp = 255;
$i = 0;
while ($i < 256)
{
$temp[$i++] = 0;
}
$temp[$pos] = $number;
$hash{$key} = [@temp];
}
else
{
print "Old Key...\n";
$list = $hash{$key};
print "before insertion\n";
$list->[$pos] = $number;
print "after insertion\n";
$hash{$key} = $list;
}
untie %hash;
# Done ...
The output is :
when a key is entered for the first time. It works fine. No problems.
when an already present key is entered, and it goes into the "old key"
portion of the code, it seems to fail after printing Old Key... Does not
print "before insertion".
The same code works fine for lower sized arrays upto 126 i.e. $#temp =
125.
Can anyone please tell me what I am missing?
thanks in advance
regards
badri
--- Code ends here -----
The same program works fine when the size of the array is lower than
128.
------------------------------
Date: 1 Jul 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 1 Jul 99)
Message-Id: <null>
Administrivia:
The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc. For subscription or unsubscription requests, send
the single line:
subscribe perl-users
or:
unsubscribe perl-users
to almanac@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V9 Issue 210
*************************************