Re: [Thanks]: Re: Objects, threads and so on
david wrote: > 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, > OK, then. One subject at a time. It helps in focusing your efforts, both in posting to a list, and in designing your code. > > 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. Got it. > 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. Hmmm, I;m not sure about that. It seems to me that a common handler for such a 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 = ) { 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 = ) and !($line =~ /UNCLE/i)) { my $spec = $line; chomp $spec; my $value = ; 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
[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... > 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. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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... > 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. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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. not sure what you mean by that but looks like you want to have a global 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. > > -- 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); } } threads->yield is usually a better choice to than sleep if you want the OS to know that it should pay attention to another 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. forking a different process is different than making a new thread. obviously, very little is shared between a parent and a child process which makes sharing a global variables before multiple process a bit difficult. > > Is there any way to make this work? > if i understand your question correctly, see if the following helps: #!/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,015001450154015401570040016701570162015401440041,,*,=*|=*_,split+local$"; map{~$_&1&&{$,<<=1,[EMAIL PROTECTED]||3])=>~}}0..s~.~~g-1;*_=*#, goto=>print+eval -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Objects, threads and so on
Fernando wrote: > > I'm quite new to Perl so please bear with me :) We have no choice: this is, after all, perl.beginners! > I've experience in Delphi so I thought I knew about objects... it > seems I don't :( Yes you do :) But you're confusing methodologies with implementations. > 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. Threads and forks are something layered on top of Perl, and therefore implicitly non-standard, beyond an explicit Perl version. > use FOO; Which will try to compile FOO.pm and to execute FOO::import. > FOO->make(); > FOO->make(); The constructor for a Perl object is, traditionally, 'new'. But 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. > 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); } } Again I am disconcerted, as you have an object instantion in each of two threads, which is immediately discarded. > while (1) {} > > -- FILE FOO.pm > > package FOO; > > %FOO::e = {}; This wouldn't compile if you wore the helmet of Perl 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). > sub make { > my $self = shift; > %FOO::e->{'something'} = %FOO:e->{'something'} + 1; > print %FOO::e->{'something'}."\n"; > } This declares 'FOO::make' because of your 'package' statement 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: > So I can access them and change them from within threads/forks Within a single thread, you could declare or require another 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]