[23449] in Perl-Users-Digest
Perl-Users Digest, Issue: 5664 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 15 14:10:54 2003
Date: Wed, 15 Oct 2003 11:10:12 -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, 15 Oct 2003 Volume: 10 Number: 5664
Today's topics:
Re: How to update entries in a file <kuujinbo@hotmail.com>
Re: How to update entries in a file <mgjv@tradingpost.com.au>
Re: How to update entries in a file (Tad McClellan)
Re: Perl on AS400 <jwillmore@remove.adelphia.net>
Re: Perl scripts for Unix on my windows machine <jwillmore@remove.adelphia.net>
Re: Q: Problem figuring out versions of DBI <Juha.Laiho@iki.fi>
Re: Q: Problem figuring out versions of DBI <jwillmore@remove.adelphia.net>
string substitution problem <dawn.schepleng@jhuapl.edu>
Re: string substitution problem <pinyaj@rpi.edu>
Re: Threads and OO Question <Ed+nospam@ewildgoose.demon.co.uk@>
Re: Threads and OO Question (Bill)
Re: Threads and OO Question <Ed+nospam@ewildgoose.demon.co.uk@>
Re: Unexpected alteration of array's content (Roy Johnson)
Re: Unexpected alteration of array's content (Anno Siegel)
Re: <bwalton@rochester.rr.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 15 Oct 2003 21:51:39 +0900
From: ko <kuujinbo@hotmail.com>
Subject: Re: How to update entries in a file
Message-Id: <bmjg12$sja$1@pin3.tky.plala.or.jp>
John wrote:
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnbootdd.57a.tadmc@magna.augustmail.com...
>
>>John <no@spam.here> wrote:
>>
>>
>>> open DATA, ">>test.xml"; # append to file
>>
>>
>>You should always, yes *always*, check the return value from open():
>>
>> open DATA, '>>test.xml' or die "could not open 'test.xml' $!";
>>
>>
>>
>>> print DATA @lines [0]; # print the first element in the array
>>
>>
>>You should enable warnings when developing Perl code!
>>
>>
>>
>>>while <$addr> { # this is the input
>>
>>
>>That is not Perl code. You are wasting the time of *thousands* of
>>people around the world because you cannot be troubled to
>>provide actual code!
>>
>>That's it. You've used up all your coupons.
>>
>>So long.
>>
>>
>>--
>> Tad McClellan SGML consulting
>> tadmc@augustmail.com Perl programming
>> Fort Worth, Texas
[snip explanation of hoework specs, etc]
> So pls feel free to flame me when my search for clues is wasting your time
> cause the code examples I provide are irrelevant but spare yourself from
> judging the way in which I do my search. There will be those who support the
> Monarchy and others who support the Republic. I'm probably somewhere in the
> middle.
It may be hard to sort out all the advice offered when it seems like
you're getting flamed solely for this reason, but you're not helping
yourself. What I mean is that people have offered *good* advice, which
has been ignored in your *posted* code.
> while <$addr> { # reads input from client
^^^^^^^^^^^^^^^^
Here's an example. As Tad pointed out in your quoted text, this is not
Perl code. It won't compile, try it. I'm assuming you have read the
posting guidelines (http://mail.augustmail.com/~tadmc/clpmisc.shtml), so
try and put yourself in the shoes of the regulars/experts. They are busy
people and take time every day to offer advice to many people. Its only
natural that you may get flamed or ignored if you can't at least show
some kind of effort - at the very least post code that compiles.
Granted, everyone makes mistakes, but in this case you did it at least
twice, even after someone took the time to point it out. Actually, its
obvious that instead of copy/paste, you're typing in your code snippets:
> print $_; # this prints to the screen the
> first line of the file passed by the client
> @lines <$addr>; # I want this array to capture the whole
> file passed from the client
> }
> fileupdate (); # calling function
> ************
>
> Q: Why isn't line 1 passed into the array?
> print $_ echoes line 1 on the server side [so we know it's been received]
> and 'cat FILE_B' only shows:
> line 2
> line 3
The above code won't pass anything into the array because, as is, it
won't compile. Since you're gettting results the code you are running
must be different than what was posted above. Your code is probably
something like this:
while (<$addr>) {
print $_;
@lines = <$addr>;
...
}
Which, if you wanted to assign all lines in the file to an array, should
be done like this:
@lines = <$addr>;
'perldoc perlop', - 'I/O Operators' for details.
> Everything was working fine when I only had this:
> ************
> while <$addr> {
> open DATA, ">>test.xml" or die "Could not open test.xml $!\n";
> print DATA $_;
> close DATA;
> }
> ************
And this won't compile either.
And none of what has been written is meant to be critical. What you need
to understand is that the people helping you can't read your mind, and
that no one can help you if you don't first help yourself (use warnings,
use strict, etc). Once again, put yourself in the shoes of the regulars.
Would you want/be able to sort through a bunch of incomprehensible
code that doesn't even compile? Its expected that you put some kind of
effort in what you post *before* you post, and more simply, just common
sense/courtesy. On a more positive note, I agree with your opinion that
everyone learns differently - I think its hard for some people to
remember what its like learning your first programming language :)
HTH - keith
------------------------------
Date: Wed, 15 Oct 2003 23:16:59 +1000
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: How to update entries in a file
Message-Id: <slrnboqi6b.fvf.mgjv@martien.heliotrope.home>
On Wed, 15 Oct 2003 06:24:39 GMT,
John <no@spam.here> wrote:
>
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnbootdd.57a.tadmc@magna.augustmail.com...
>> John <no@spam.here> wrote:
>> > while <$addr> { # this is the input
>>
>>
>> That is not Perl code. You are wasting the time of *thousands* of
>> people around the world because you cannot be troubled to
>> provide actual code!
[snip loads of justifications in which I am not interested]
> while <$addr> { # reads input from client
Did you actually read what Tad wrote?
make sure you provide _real_ Perl code. It is rather disrespectful of
you to expect the people here to do the work of the Perl parser and
compiler for you, when you have a computer that can do it for you.
Once you have code that actually compiles, and utilises the strict and
warnings pragmas, feel free to come back. Until then, especially since
this is homework, work on it.
> print $_; # this prints to the screen the
> first line of the file passed by the client
You don't need the $_ there. You should make sure that your comments
don't wrap, resulting in more invalid code.
> @lines <$addr>; # I want this array to capture the whole
> file passed from the client
This does not even look vaguely like Perl code.
> fileupdate (); # calling function
> ************
>
> Q: Why isn't line 1 passed into the array?
You ave no code to run, so that's why nothing happens.
> print $_ echoes line 1 on the server side [so we know it's been received]
I doubt it, sincerely. The stuff you posted does not compile, so the
print has never been executed.
> Everything was working fine when I only had this:
> ************
> while <$addr> {
You are not telling us the truth. Not even after Tad told you to make
sure to submit your real code.
I am not at all surprised that Tad is no longer willing to assist you.
Martien
--
|
Martien Verbruggen |
| In a world without fences, who needs Gates?
|
------------------------------
Date: Wed, 15 Oct 2003 08:50:54 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: How to update entries in a file
Message-Id: <slrnboqk5u.7cn.tadmc@magna.augustmail.com>
John <no@spam.here> wrote:
> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnbootdd.57a.tadmc@magna.augustmail.com...
>> John <no@spam.here> wrote:
>> > while <$addr> { # this is the input
>>
>>
>> That is not Perl code. You are wasting the time of *thousands* of
>> people around the world because you cannot be troubled to
>> provide actual code!
> For those of us who write books, not everything is covered in them
But everything _is_ covered for those of us that don't write books?
What relevance does authorship have?
> So pls feel free to flame me when my search for clues is wasting your time
Your search for clues is wasting our time!
> cause the code examples I provide are irrelevant
It isn't the irrelevancy, it is the It's Not Perl Code in the first place.
> Everything was working fine when I only had this:
I doubt that that is an accurate statement.
> while <$addr> {
Looks like you cannot be troubled to provide actual code even when
asked to provide actual code.
Sheesh!
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Wed, 15 Oct 2003 17:09:48 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Perl on AS400
Message-Id: <20031015131011.1e3968f3.jwillmore@remove.adelphia.net>
On 15 Oct 2003 02:27:49 -0700
mbr@petercam.be (Michel) wrote:
> Can someone help me with this problem
> I am using perl on as400 V5r1 with CCSID 00500, locale EN_BE_LOCALE
> when I try this:
>
> perl -e 'use DBI;'
>
> Unrecognized character \112 at /usr/local/lib/perl5/5.00502/Carp.pm
> line 79. BEGIN failed--compilation aborted at
> /usr/local/lib/perl5/5.00502/DBI.pm line
> 137.
>
> BEGIN failed--compilation aborted at -e line 1.
>
>
> this is what I got.
>
> Any help is welcome
If the AS400 is anything like the S390 in any way, you _may_ want to
check the translation table for the machine. And also make sure the
Perl binary was installed properly.
This is only a guess and _may_ be way off.
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
"The eleventh commandment was `Thou Shalt Compute' or `Thou Shalt
Not Compute' -- I forget which." -- Epigrams in Programming,
<ACM SIGPLAN Sept. 1982
------------------------------
Date: Wed, 15 Oct 2003 17:19:43 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Perl scripts for Unix on my windows machine
Message-Id: <20031015132006.141d4c92.jwillmore@remove.adelphia.net>
On 15 Oct 2003 05:51:08 -0700
reneap@hotmail.com (Ren Patterson) wrote:
> > If you really mean that, then please start quoting your
> > followups properly.
>
> What are you talking about? quoting your followups properly? what is
> wrong with this guy mike?
Top posting is rude. Please don't do it :-)
> > > none of these system(), exec(), ``, qx//, or a pipe open().
> > >
> > > seem to be on my cgi-perl Unix scripts. Does that mean they
> > > should be able to run from my Windows web server cgi-bin folder?
> >
> >
> > Probably, though there _are_ a few less obvious potential
> > problems.
> >
> > What happened when you tried it?
>
> I have not tried yet, I wanted to know before hand if I would be
> wasting my time for maybe there was no way they worked. I will give
> it a shot and post my results. Thanks.
Why not? Oh, you wanted to prevent an issue before it happened.
The first line of your script _may_ present an issue. Consider this:
#!/usr/bin/perl -w
This is no /usr/bin on many Windows boxes (unless you're using Cygwin,
which it appears you're not).
I believe there's a FAQ on portability. Try the documentation.
You could try _reading_ the various newsgroups as well. This is a
common topic. Google is your friend :-)
HTH
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Malek's Law: Any simple idea will be worded in the most
<complicated way.
------------------------------
Date: Wed, 15 Oct 2003 15:57:00 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Q: Problem figuring out versions of DBI
Message-Id: <bmjqj1$tpe$1@ichaos.ichaos-int>
dpetrou@cs.cmu.edu said:
>Hi. I have a bunch of DBI questions I'm hoping ya'll can help me
>with. They are real simple questions, but I'm stymied after searching
>the usual channels for answers.
...
>The reason why I'm trying to figure out my DBI version is so that I
>can upgrade, if needed to 2.1022 which is recommended for the version
>of MySQL that I have installed.
Hmm.. don't know whether this is the correct way, but at least works
for my antique DBI:
perm -MDBI -e 'print $DBI::VERSION;'
... but then, I took a look through DBI.pm to find out this.
>Question #3: If I install my own DBI, does it come with all the DBD
>drivers? Or do I need to hunt down appropriately versioned DBD's for
>the databases I'm interacting with?
DBI doesn't contain DBDs, so you'll have to pick the ones important
for you. But then, you'll find them at your closest CPAN mirror, so
no hunting needed.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
------------------------------
Date: Wed, 15 Oct 2003 17:06:37 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Q: Problem figuring out versions of DBI
Message-Id: <20031015130656.4e445f54.jwillmore@remove.adelphia.net>
On 14 Oct 2003 22:16:00 GMT
David Petrou <dpetrou@ece.cmu.edu> wrote:
<snip>
> The reason why I'm trying to figure out my DBI version is so that I
> can upgrade, if needed to 2.1022 which is recommended for the
> version of MySQL that I have installed.
perl -MDBI -e 'print $DBI::VERSION,"\n";'
to get the version of DBI. To get the available drivers, there's a
different method. Read the DBI documentation for more information.
> Question #2: Is there a way to tell DBD::MySQL to look for the MySQL
> binaries in a specific place? I want to interact with a remote
> MySQL server of version foo, but the MySQL client installed locally
> in/usr/local/bin (which I don't have permission to change) is
> version bar. I installed version foo in off of ~ and I'd like DBI
> to use that stuff.
Ouch! You're trying to install DBD::mysql on a machine that does
_not_ have MySQL installed on it? If that's the case, no, it won't
install untill you install the MySQL binaries. You _could_, as a
"workaround", use DBD::Proxy. I'm also thinking there's a DBD module
that uses sockets versus the binaries. In other words, the script
opens a connection from the client machine to the MySQL server using
the MySQL port and issues commands directly to the server through that
port. You'll have to check on that (http://search.cpan.org - search
for mysql).
> Question #3: If I install my own DBI, does it come with all the DBD
> drivers? Or do I need to hunt down appropriately versioned DBD's
> for the databases I'm interacting with?
Each different database system has it's own set of drivers. So, yes,
you have to hunt for the proper drivers.
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
According to Kentucky state law, every person must take a bath at
least once a year.
------------------------------
Date: Wed, 15 Oct 2003 11:22:28 -0400
From: Dawn Schepleng <dawn.schepleng@jhuapl.edu>
Subject: string substitution problem
Message-Id: <3F8D6634.6A4E1052@jhuapl.edu>
--------------FB7E27A1E2DB3352CD062D64
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi :)
I am a novice Perl programmer, and have encountered the following problem.
I can make the following string substitution work:
$high_level_dir = "/ceswbl2/installed/bin"
$low_level_dir = "/ceswbl2/src/sparc/bin";
$line =~ s{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2};
This will correctly take a line like:
/ceswbl2/src/sparc/bin/cec_sim/radar/sps48/cecsimlm
and make substitutions such that $line then equals:
/ceswbl2/installed/bin/cecsimlm.
I'm using \w+ to represent a word with numbers and underscores, and am
using "(", ")", and "/" to be literal, and using the substitution format of
"s{}{}" vice "s///" to be more readable.
However, when I take this working perl substitution and try to invoke it as a
system call like:
system("perl -pi -e 's{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2}'
$real_run_file");
It no longer works. The error occurs in a first-pass parse of the perl script,
and says something line "Unrecognized escape \w passed through
in cepSetGoEnv.pl at line 31".
With the help of a co-worker, we've tried changing "{" "}" to "|" and we've
also tried escaping out the "(" and ")" with no luck.
What is the subtle detail that makes this work in-line, but not when the script
is run w/i the system call?
Any ideas would be appreciated!
Thanks,
Dawn Schepleng
JHU/APL
--------------FB7E27A1E2DB3352CD062D64
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<br>Hi :)
<p>I am a novice Perl programmer, and have encountered the following problem.
<br>I can make the following string substitution work:
<p><i>$high_level_dir = "/ceswbl2/installed/bin"</i>
<br><i>$low_level_dir = "/ceswbl2/src/sparc/bin";</i>
<br><i>$line =~ s{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2};</i><i></i>
<p>This will correctly take a line like:
<p>/ceswbl2/src/sparc/bin/cec_sim/radar/sps48/cecsimlm
<p>and make substitutions such that $line then equals:
<p>/ceswbl2/installed/bin/cecsimlm.
<p>I'm using \w+ to represent a word with numbers and underscores, and
am
<br>using "(", ")", and "/" to be literal, and using the substitution format
of
<br>"s{}{}" vice "s///" to be more readable.
<p>However, when I take this working perl substitution and try to invoke
it as a
<br>system call like:
<p><i>system("perl -pi -e 's{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2}'</i>
<br><i> $real_run_file");</i>
<p>It no longer works. The error occurs in a first-pass parse of
the perl script,
<br>and says something line <b>"Unrecognized escape \w passed through</b>
<br><b>in cepSetGoEnv.pl at line 31"</b>.
<p>With the help of a co-worker, we've tried changing "{" "}" to "|" and
we've
<br>also tried escaping out the "(" and ")" with no luck.
<p>What is the subtle detail that makes this work in-line, but not when
the script
<br>is run w/i the system call?
<p>Any ideas would be appreciated!
<p>Thanks,
<p> Dawn Schepleng
<br> JHU/APL</html>
--------------FB7E27A1E2DB3352CD062D64--
------------------------------
Date: Wed, 15 Oct 2003 12:31:11 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Dawn Schepleng <dawn.schepleng@jhuapl.edu>
Subject: Re: string substitution problem
Message-Id: <Pine.SGI.3.96.1031015122303.36159B-100000@vcmr-64.server.rpi.edu>
[posted & mailed]
On Wed, 15 Oct 2003, Dawn Schepleng wrote:
>$high_level_dir = "/ceswbl2/installed/bin"
Missing semicolon, by the way.
>$low_level_dir = "/ceswbl2/src/sparc/bin";
>$line =~ s{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2};
That works as expected, which is good.
>system("perl -pi -e 's{$low_level_dir(/\w+)+/(\w+)}{$high_level_dir/$2}'
> $real_run_file");
>
>It no longer works. The error occurs in a first-pass parse of the perl
>script, and says something line "Unrecognized escape \w passed through in
>cepSetGoEnv.pl at line 31".
Right. You've made a double quoted string "perl -pi ..." and it has
backslashed characters in it. In a *regex*, \w means something. In a
double-quoted string, it doesn't. You'll need to DOUBLE the backslash.
print "m/(\\w+)/"; # prints: m/(\w+)/
The other problem you'll encounter is that Perl interpolates $2 when you
make that string you pass to system(). You don't want Perl to do that;
you want Perl to send the literal string '$2' to the system() command so
that when system() runs perl, perl sees $2.
system("perl -pi -e 's{foo(/\\w+)+(/\\w+)}bar\$2}' file");
But here's the nice thing: you don't need to call system() here. The -p
and -i flags just add wrappers around the code you give. If you know what
those wrappers look like, you can write a program that DOES what -pi does.
They're explained in the 'perlrun' manpage:
perldoc perlrun
I'm pretty sure there's an exact example of how to do what -pi does in
your program. Look for the documentation of the -i switch.
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
------------------------------
Date: Wed, 15 Oct 2003 16:14:52 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Re: Threads and OO Question
Message-Id: <0oejb.2966972$Bf5.404650@news.easynews.com>
> Can anyone point me to any other references on the new perl threads. I
have
> read the man pages, and perlthrtut. I'm basically confused as to how to
> share variables between threads when the variable to be shared is a class.
> I'm aware this is fairly limited at the moment, but advice on how to make
> something like the below share variables correctly would be appreciated:
Partly answering my own question, it seems I can do the following:
package MyStats;
use threads::shared;
sub new {
my $class = shift;
my $this = { bytes_in => 0,
bytes_out => 0};
share($this);
return bless($this, $class);
}
sub increment {
my $this = shift;
$this->{bytes_in} += shift;
$this->{bytes_out} += shift;
}
From reading the documentation it wasn't clear that this was valid, or even
correct (ie sharing the whole $this variable). Presumably this breaks if
anything in the $this is either a) not a scalar, or b) not itself shared?
Is there a more in-depth tutorial on this anywhere? It's kind of slow going
having to write all these experimental bits of code to test stuff out
first... (yeah, yeah, thread safe programming isn't supposed to be easy. I
know, I know!)
Thanks all
Ed W
------------------------------
Date: 15 Oct 2003 09:50:27 -0700
From: wherrera@lynxview.com (Bill)
Subject: Re: Threads and OO Question
Message-Id: <239ce42f.0310150850.5f4edd7b@posting.google.com>
"Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@> wrote in message news:<9g9jb.2951223$Bf5.402549@news.easynews.com>...
> read the man pages, and perlthrtut. I'm basically confused as to how to
> share variables between threads when the variable to be shared is a class.
>
>
> <scribble mode on>
>
> package main;
>
> my $worker = Worker->new();
> my $thr = threads->new(sub{$worker->start();});
> while (1) {
> my $stats = $worker->CheckStats();
> useful_function( $stats->Do_Something_With_Object() );
> }
>
> ##############################
> package Worker;
>
> sub new {
> my $class;
> return bless( {Stats => MyStats->new(), $class}
> }
>
Good question, I think. Maybe Arthur Bergman knows the answer.
Do you have to share the class, or could you create a hash containing
the data you want shared and share the hash?
I say this because the docs on threads::shared (have you looked at
threads::shared?) says that
" bless is not supported on shared references. In the current version,
bless will only bless the thread local reference and the blessing will
not propagate to the other threads. This is expected to be implemented
in a future version of Perl."
------------------------------
Date: Wed, 15 Oct 2003 17:49:39 GMT
From: "Edward Wildgoose" <Ed+nospam@ewildgoose.demon.co.uk@>
Subject: Re: Threads and OO Question
Message-Id: <TMfjb.6107219$mA4.867092@news.easynews.com>
> Good question, I think. Maybe Arthur Bergman knows the answer.
Sorry for my ignorance. Who is this person? Aha, google turns up "who" he
is, but does he run a web page of useful info? (Anyone?)
> Do you have to share the class, or could you create a hash containing
> the data you want shared and share the hash?
Nope, I just learned to use classes on Perl and I went wild...
However, this then brings us full circle back to the problem. In the main
thread how do I talk back to the worker thread in order to ask it to
populate the shared hash? (I want to avoid nasty IPC stuff since I already
wrote a socket communicator to the other process and I'm trying this method
to avoid all that junk!) Because it seems to me that if the main thread
calls some function, say, "getSomeInternalState", then this function cannot
access the non-shared objects in order to populate the shared hash....
So if the worker thread is using shared hashes to proxy data back to the
main thread then I might as well not bother with my Statistics encapsulation
class at all since it's just a duplication...
(Did that make any sense?)
Yes, I can use semaphores to communicate to the worker process and get it to
populate the shared hash, but so much nicer if I could just share the
internal state object in the first place!
> I say this because the docs on threads::shared (have you looked at
> threads::shared?) says that
>
> " bless is not supported on shared references. In the current version,
> bless will only bless the thread local reference and the blessing will
> not propagate to the other threads. This is expected to be implemented
> in a future version of Perl."
Yeah, agree. However, see my other post because that code works on
Activestate 5.8 (whatever the latest is as of today. build 806?)
I have included the whole test script below:
...Should this work? Does it work under linux...? What the heck is going
on...? Help!
Thanks all
Ed W
>>>>>>>>>>>>>>>>>>>>>>>>
use threads;
use strict;
package MyStats;
use threads::shared;
sub new {
my $class = shift;
my $this = { bytes_in => 0,
bytes_out => 0};
share($this);
return bless($this, $class);
}
sub increment {
my $this = shift;
$this->{bytes_in} += shift;
$this->{bytes_out} += shift;
}
package MyPackage;
use threads::shared;
my $counter : shared;
sub new {
my $class = shift;
my $this = {STATS => MyStats->new()};
share($this->{STATS});
return bless($this, $class);
}
sub start {
my $this = shift;
while (1) {
$counter += 1;
$this->{STATS}->increment(1,2);
sleep 1;
print STDOUT "In Counter: $counter \r\n";
}
}
sub getCounter {
my $this = shift;
return $this->{STATS};
# return $counter;
}
package main;
use threads::shared;
sub MainLoop {
$| = 1;
my $a;
my $a = MyPackage->new();
my $thr = threads->new(sub{$a->start();});
sleep 1;
while (1) {
my $count = $a->getCounter();
print STDOUT $count;
sleep 1;
}
my @ReturnData = $thr->join;
print "Thread returned @ReturnData";
}
MainLoop();
------------------------------
Date: 15 Oct 2003 07:50:33 -0700
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Unexpected alteration of array's content
Message-Id: <3ee08638.0310150650.713449b7@posting.google.com>
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<bmj5v9$sd7$1@mamenchi.zrz.TU-Berlin.DE>...
> Reason or no, the use of [] to make an array copy, and of {} for a hash
> copy, are pretty well established.
There are reasons for those uses.
> From there it is only a step to "@{[@ar]}".
A step with no reason. A misstep, as it were.
I'm not sure what point you're trying to make.
------------------------------
Date: 15 Oct 2003 15:55:48 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Unexpected alteration of array's content
Message-Id: <bmjqm4$h7h$1@mamenchi.zrz.TU-Berlin.DE>
Roy Johnson <rjohnson@shell.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message
> news:<bmj5v9$sd7$1@mamenchi.zrz.TU-Berlin.DE>...
> > Reason or no, the use of [] to make an array copy, and of {} for a hash
> > copy, are pretty well established.
>
> There are reasons for those uses.
>
> > From there it is only a step to "@{[@ar]}".
>
> A step with no reason. A misstep, as it were.
>
> I'm not sure what point you're trying to make.
Only that I don't share your dislike of "@{[@ar]}". If you want an anonymous
copy of an array, it's the way to go. Since that doesn't happen often,
there is no danger of it becoming an idiom. No big deal altogether.
Anno
------------------------------
Date: Sat, 19 Jul 2003 01:59:56 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re:
Message-Id: <3F18A600.3040306@rochester.rr.com>
Ron wrote:
> Tried this code get a server 500 error.
>
> Anyone know what's wrong with it?
>
> if $DayName eq "Select a Day" or $RouteName eq "Select A Route") {
(---^
> dienice("Please use the back button on your browser to fill out the Day
> & Route fields.");
> }
...
> Ron
...
--
Bob Walton
------------------------------
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:
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.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.
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 V10 Issue 5664
***************************************