Objects, threads and so on

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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


    Rob Dixon Guest

  5. #4

    Default 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,015001450154015401570040016701570162015401440 041,,*,=*|=*_,split+local$";
    map{~$_&1&&{$,<<=1,$#.=qq~\x63\x68\x72\x28@_[$_..$||3])=>~}}0..s~.~~g-1;*_=*#,

    goto=>print+eval
    David Guest

  6. #5

    Default 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

    Lists Perl Org Guest

  7. #6

    Default [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
    David Guest

  8. #7

    Default 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 = <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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139