Ask a Question related to PERL Beginners, Design and Development.
-
Lists Perl Org #1
Objects, threads and so on
Hi all,
I'm quite new to Perl so please bear with me :)
I've experience in Delphi so I thought I knew about objects... it
seems I don't :(
I want to have a class I won't instanciate (static members and
variables), with global vars so I can access them and change them from
withing threads/forks.
-- FILE test.pl
use FOO;
FOO->make();
FOO->make();
use Thread;
my $t = new Thread \&th1;
my $u = new Thread \&th2;
sub th1() { while (1) { FOO->make(); sleep(1); } }
sub th2() { while (1) { print "\t"; FOO->make(); sleep(2); } }
while (1) {}
-- FILE FOO.pm
package FOO;
%FOO::e = {};
sub make {
my $self = shift;
%FOO::e->{'something'} = %FOO:e->{'something'} + 1;
print %FOO::e->{'something'}."\n";
}
-- RESULTS --
1
2
3
3
4
5
4
6
7
5
8
....
Obviously it doesn't work.
I have tried a lot more of things and I don't know how to make it
work. The same applies if I use fork() instead threads.
Is there any way to make this work?
Thank you very much in advance!
Fernando Najera
Lists Perl Org Guest
-
Newbie Question? Aligning Objects to other Objects?
Hi, I think this a newbie question and I will try to explain it as best as possible! I have a hollow circle (no fill, or stroke) and x amount of... -
Storing Objects/Arrays in Stored Objects
Hello All, I recently came across a very frustrating issue when trying to create and store arrays within objects in a Shared object. It took me... -
Saving Threads
Is there a way to save all the messages in a thread of do you have to save them from the forum one at a time? Dick -- Using M2, Opera's... -
Using threads in ASPX objects
..Net is not limited in this way as java. ..About the only thing you need to be concerned about is the usual threading concurrency issues. Remember... -
Max worker threads
Looking at the server characteristics I suppose this worker process threads is in consequential as far as your database is concerned. They seem to... -
Ramprasad A Padmanabhan #2
Re: Objects, threads and so on
Lists Perl Org wrote:
> Hi all,
>
> I'm quite new to Perl so please bear with me :)
>
> I've experience in Delphi so I thought I knew about objects... it
> seems I don't :(
>
> I want to have a class I won't instanciate (static members and
> variables), with global vars so I can access them and change them from
> withing threads/forks.
>
> -- FILE test.pl
>
> use FOO;
>
> FOO->make();
> FOO->make();
>
> use Thread;
>
> my $t = new Thread \&th1;
> my $u = new Thread \&th2;
>
> sub th1() { while (1) { FOO->make(); sleep(1); } }
> sub th2() { while (1) { print "\t"; FOO->make(); sleep(2); } }
>
> while (1) {}
>
> -- FILE FOO.pm
>
> package FOO;
>
> %FOO::e = {};
>
> sub make {
> my $self = shift;
> %FOO::e->{'something'} = %FOO:e->{'something'} + 1;
> print %FOO::e->{'something'}."\n";
> }
>
> -- RESULTS --
>
> 1
> 2
> 3
> 3
> 4
> 5
> 4
> 6
> 7
> 5
> 8
> ...
>
> Obviously it doesn't work.
>
> I have tried a lot more of things and I don't know how to make it
> work. The same applies if I use fork() instead threads.
>
> Is there any way to make this work?
>
> Thank you very much in advance!
>
> Fernando Najera
>
>
>
Maybe you would do better to learn perl-syntax well before you jump into
your actual application. A common problem for people conversant with
many languages is that they always try translate their work in all
languages though their command on all languages is not the same
A hash array reference is always written with $sign not %
so change
%FOO::e = {}; ==> $FOO::e
in all places
I havent gone thru the entire code , But I think you can take it further
Ram
Ramprasad A Padmanabhan Guest
-
Rob Dixon #3
Re: Objects, threads and so on
Fernando wrote:
We have no choice: this is, after all, perl.beginners!>
> I'm quite new to Perl so please bear with me :)
Yes you do :) But you're confusing methodologies with implementations.> I've experience in Delphi so I thought I knew about objects... it
> seems I don't :(
Threads and forks are something layered on top of Perl, and therefore> I want to have a class I won't instanciate (static members and
> variables), with global vars so I can access them and change them from
> within threads/forks.
implicitly non-standard, beyond an explicit Perl version.
Which will try to compile FOO.pm and to execute FOO::import.> use FOO;
The constructor for a Perl object is, traditionally, 'new'. But> FOO->make();
> FOO->make();
this is not bound by the language and can have any name that you
want. However this looks like two instantiations, which you said
you weren't doing, and both of which are discarded (in either
Delphi or Perl) so I don't see what you're trying to do here.
Again I am disconcerted, as you have an object instantion> use Thread;
>
> my $t = new Thread \&th1;
> my $u = new Thread \&th2;
>
> sub th1() { while (1) { FOO->make(); sleep(1); } }
> sub th2() { while (1) { print "\t"; FOO->make(); sleep(2); } }
in each of two threads, which is immediately discarded.
This wouldn't compile if you wore the helmet of Perl> while (1) {}
>
> -- FILE FOO.pm
>
> package FOO;
>
> %FOO::e = {};
salvation, being:
use strict; # always
use warnings; # usually
You're trying to set a hash (being a list of paired values)
to a single value (which is an empty hash reference).
This declares 'FOO::make' because of your 'package' statement> sub make {
> my $self = shift;
> %FOO::e->{'something'} = %FOO:e->{'something'} + 1;
> print %FOO::e->{'something'}."\n";
> }
above. Call it 'FOO::new' to make others in the Perl world
feel at home.
Any constructor parameters are values to help form the object.
Your constructor cannot have a $self parameter as, if
it is useful at all, it is to copy values from an existing
object of the same type or of a subclass. It will usually be
called 'clone'.
Back to your top line:
Within a single thread, you could declare or require another> So I can access them and change them from within threads/forks
module containing
package MODULE;
our $var1;
our $var2;
etc.
Which will be accessible externally as $MODULE::var1,
$MODULE::var2. You could also 'use' that code as an external
module which will let you access synonyms to those values
in the current package as $var1, $var2 etc. Unless you have
a comprehensive requirement IMO this is not worth considering.
Say more about your application and we will help further.
HTH,
Rob
Rob Dixon Guest
-
David #4
Re: Objects, threads and so on
Lists Perl Org wrote:
not sure what you mean by that but looks like you want to have a global> Hi all,
>
> I'm quite new to Perl so please bear with me :)
>
> I've experience in Delphi so I thought I knew about objects... it
> seems I don't :(
>
> I want to have a class I won't instanciate (static members and
> variables), with global vars so I can access them and change them from
> withing threads/forks.
variable where the threads can manipulate and all the threads will see the
same value once a thread modified the vairable. if so you need to lock the
variable before letting the threads to access that variable.
threads->yield is usually a better choice to than sleep if you want the OS>
> -- FILE test.pl
>
> use FOO;
>
> FOO->make();
> FOO->make();
>
> use Thread;
>
> my $t = new Thread \&th1;
> my $u = new Thread \&th2;
>
> sub th1() { while (1) { FOO->make(); sleep(1); } }
> sub th2() { while (1) { print "\t"; FOO->make(); sleep(2); } }
to know that it should pay attention to another thread.
forking a different process is different than making a new thread.>
> while (1) {}
>
> -- FILE FOO.pm
>
> package FOO;
>
> %FOO::e = {};
>
> sub make {
> my $self = shift;
> %FOO::e->{'something'} = %FOO:e->{'something'} + 1;
> print %FOO::e->{'something'}."\n";
> }
>
> -- RESULTS --
>
> 1
> 2
> 3
> 3
> 4
> 5
> 4
> 6
> 7
> 5
> 8
> ...
>
> Obviously it doesn't work.
>
> I have tried a lot more of things and I don't know how to make it
> work. The same applies if I use fork() instead threads.
obviously, very little is shared between a parent and a child process which
makes sharing a global variables before multiple process a bit difficult.
if i understand your question correctly, see if the following helps:>
> Is there any way to make this work?
>
#!/usr/bin/perl -w
use strict;
use threads;
use threads::shared;
package AnotherNameSpace;
#--
#-- $var is global to AnotherNameSpace and will
#-- be shared among all threads
#--
my $var : shared = 1;
sub inc{
#--
#-- let only one thread access $var at a time
#--
{
lock($var);
$var++;
print STDERR "$_[0]$var\n";
}
}
package main;
sub get1{
while(1){
AnotherNameSpace::inc('');
threads->yield;
}
}
sub get2{
while(1){
AnotherNameSpace::inc("\t");
threads->yield;
}
}
my $t1 = threads->new(\&get1);
my $t2 = threads->new(\&get2);
$t1->join;
$t2->join;
__END__
prints:
10
11
12
13
14
15
....
538
539
540
541
542
543
....
lines begin with a tab is printed by one thread and those that do not being
with the tab are printed by another thread. as you can see, they all syn
up. without the locking, you might see something like:
601
598
602
599
603
600
604
601
which probably isn't what you expect.
david
--
$_=q,015001450154015401570040016701570162015401440 041,,*,=*|=*_,split+local$";
map{~$_&1&&{$,<<=1,$#.=qq~\x63\x68\x72\x28@_[$_..$||3])=>~}}0..s~.~~g-1;*_=*#,
goto=>print+eval
David Guest
-
Lists Perl Org #5
Re[2]: Objects, threads and so on
Hello all,
(david, sorry if you receive this several times, I've had a hard time
with the email client).
Thank you all for your attention. I'll answer here for the three
who wrote about this thread.
The example David wrote is wonderful. I was missing ": shared".
That was exactly what I wanted to do, but better than what I
wrote :). My skills on Google must be disappearing, 'cause I didn't
find it and really looked for it several times...
Here I was badly trying to define a set of functions, procedures> I want to have a class I won't instanciate
and attributes which you can use from anywhere in your program,
and which are collected under a common class, but something you
don't have to instanciate => which, in Perl, seems to be a
namespace or package. An example could be a "Config class" which
has methods like WriteConfig or ReadConfig -- like for reading
..ini files, and it has no sense (for me) to instanciate such a
class. My English is not very good and I can't manage to explain
better :( Anyway, I think I have understood what should I do.
About "use strict", I know and I use it in the actual program, but
made a test-case quickly to write the email and I didn't check for
this. I really prefer the way David wrote the global (shared)
variable; I was messing all I have read about perl objects all
around :(
In any case, I will read and study perl-syntax because I see Perl
is a powerful but non-trivial-to-write language... hehe.
Thank you for your help. I'll read this list with attention :)
Con fecha lunes, 29 de septiembre de 2003, 21:49:21, escribió:
d> ... if i understand your question correctly, see if the
d> following
d> helps: ...
--
Best regards,
Fernando Najera
Lists Perl Org Guest
-
David #6
[Thanks]: Re: Objects, threads and so on
Fernando wants to send the following thanks to all people who helped him on
this topic and some explanation of what he really wants to do. He somehow
sent it to myself only.
#------------- forward message started -------------#
Hello all,
Thank you all for your attention. I'll answer here for the three who
wrote about this thread.
The example David wrote is wonderful. I was missing ": shared". That
was exactly what I wanted to do, but better than what I wrote :). My
skills on Google must be disappearing, 'cause I didn't find it and I
really looked for it several times...
Here I was badly trying to define a set of functions, procedures and> I want to have a class I won't instanciate
attributes which you can use from anywhere in your program, and which
are collected under a common class, but something you don't have to
instanciate => which, in Perl, seems to be a namespace or package. An
example could be a "Config class" which has methods like WriteConfig
or ReadConfig -- like for reading .ini files, and it has no sense (for
me) to instanciate such a class. My English is not very good and I
can't manage to explain better :( Anyway, I think I have understood
what should I do.
About "use strict", I know and I use it in the actual program, but I
made a test-case quickly to write the email and I didn't check for
this. I really prefer the way David wrote the global (shared)
variable; I was messing all I have read about perl objects all around
:(
In any case, I will read and study perl-syntax because I see Perl is a
powerful but non-trivial-to-write language... hehe.
Thank you for your help. I'll read this list with attention :)
Con fecha lunes, 29 de septiembre de 2003, 21:49:21, escribió:
d> ... if i understand your question correctly, see if the following
d> helps: ...
--
Best regards,
Fernando Najera
David Guest
-
R. Joseph Newton #7
Re: [Thanks]: Re: Objects, threads and so on
david wrote:
OK, then. One subject at a time. It helps in focusing your efforts, both in> Fernando wants to send the following thanks to all people who helped him on
> this topic and some explanation of what he really wants to do. He somehow
> sent it to myself only.
>
> #------------- forward message started -------------#
>
> Hello all,
>
posting to a list, and in designing your code.
Got it.>> > I want to have a class I won't instanciate
> Here I was badly trying to define a set of functions, procedures and
> attributes which you can use from anywhere in your program, and which
> are collected under a common class, but something you don't have to
> instanciate => which, in Perl, seems to be a namespace or package.
Hmmm, I;m not sure about that. It seems to me that a common handler for such a> An
> example could be a "Config class" which has methods like WriteConfig
> or ReadConfig -- like for reading .ini files, and it has no sense (for
> me) to instanciate such a class.
purpose would allow it to be used for many programs, or for many users of the
same program. If you want to make your preferences more granular,
instantiation could be a benefit...but that is not what you are asking for.
There is no reason that you cannot make a package to hold appropriately global
data. We seldom encourae such approaches, because usually students are just
avoiding certain challenges, such as strict compilation, thinking out scopes,
etc.
Never the less here is one that has not a hint of instances, and writes and
reads an ini file.
RJNConfig.pm:
package RJNConfig; # less likely to step on existing modules than a
# generic name like Config alone
use strict;
use warnings;
use Exporter;
our @ISA = ('Exporter');
our @EXPORT_OK = ();
our @EXPORT = qw(get_specifications write_specifications $specifications);
our $specifications = {};
use constant PROGRAM_NAME => 'test_single_config';
my $program_name = PROGRAM_NAME;
sub get_specifications {
open IN, "$program_name.ini" or return 0, 'Could not open program initiation
file';
while (my $spec_line = <IN>) {
chomp $spec_line;
my ($specification, $value) = split /\s*=\s*/, $spec_line;
$specifications->{$specification} = $value;
}
close IN;
return $specifications, 0;
}
sub write_specifications {
our $specifications = $_[0];
open OUT, ">$program_name.ini" or return;
foreach (keys %$specifications) {
my $spec_line = "$_ = $specifications->{$_}\n";
print OUT $spec_line;
}
close OUT;
}
#!perl
use strict;
use warnings;
use RJNConfig;
my ($specs, $error) = get_specifications();
foreach (keys %$specs) {
print "the $_ of this program is $specs->{$_}\n";
}
print "Now you set the specs, user-guy, since it IS your machine.
Cry UNCLE when you've had enough\n";
my %specs;
my $line;
while (defined ($line = <STDIN>) and !($line =~ /UNCLE/i)) {
my $spec = $line;
chomp $spec;
my $value = <STDIN>;
chomp $value;
$specs{$spec} = $value;
}
write_specifications(\%specs)
Testing at the command-line:
Greetings! E:\d_drive\perlStuff>test_single_config.pl
the Program name of this program is test_single_config
the Purpose of this program is To mess wit ya, man
Now you set the specs, user-guy, since it IS your machine.
Cry UNCLE when you've had enough
Favorite Movie
Lassie Come Home
Favorite song
Mr. Rogers' Neighborhood
UNCLE
Greetings! E:\d_drive\perlStuff>test_single_config.pl
the Favorite song of this program is Mr. Rogers' Neighborhood
the Favorite Movie of this program is Lassie Come Home
Now you set the specs, user-guy, since it IS your machine.
Cry UNCLE when you've had enough
Some silly-ass global spec
A happenstance value sure to get me in trouble
Another spec that came out of nowhere
By serindipity, a value that actually makes sense.
UNCLE
Greetings! E:\d_drive\perlStuff>
Joseph
R. Joseph Newton Guest



Reply With Quote

