pravlm has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I want to store some data for example a "number". When my perl script runs it uses this number and based on the value of it, it does some task and after that it performs some operation on the number and exits.

The number has to be persistent. And if the perl script is ran in a different machine it should work seemlessly.

Regards
-PM
  • Comment on How to use shared/global emory kind of feature in perl

Replies are listed 'Best First'.
Re: How to use shared/global emory kind of feature in perl
by tilly (Archbishop) on Mar 30, 2009 at 19:17 UTC
    You need to store the number somewhere and access it somehow. There are many ways to do this. I have personally solved problems of this nature with saving the number in a file on a file server, using DBI, and using Cache::Memcached. All are appropriate solutions in the right situation.

    The trade-offs are that the file server is the easiest to implement, the database is the most flexible (particularly as you start to have more complicated data structures), and the third is probably going to run fastest.

    I would personally lean towards either the file server or the database.

Re: How to use shared/global emory kind of feature in perl
by zentara (Cardinal) on Mar 30, 2009 at 19:18 UTC
    Read "perldoc perlipc" for a start. You "could use shared memory segments, but considering your level of Perl experience, a simpler solution is to put the number into a database....or....just write it to a file somewhere, to retain it's value from script to script.

    There are many fancy ways to do this, like auto spawing the next script, passing the current number as a parameter.

    But show some code as to what your idea is.....I smell homework.


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
      If you wish to share between machines, you cannot use shared memory segments.
        Sure, you can..... the number can still be stored on one master machine in shared mem, controlled by a daemon, and have the daemon listen for external socket connections. The program can run over the socket.

        I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: How to use shared/global emory kind of feature in perl
by VinsWorldcom (Prior) on Mar 30, 2009 at 19:31 UTC

    Granted my experience with polymorphic code is non-existent, but could you do something like the following particularly useless piece of code:

    #!/usr/bin/perl use strict; my $var = 1; print "$var\n"; open (IN, "test.pl"); my @lines = <IN>; close (IN); open (OUT, ">temp.pl"); foreach (@lines) { if ($_ =~ /^my \$var = /) { my @parts = split (/\s+/, $_); printf OUT "my \$var = %i;\n", ++$parts[@parts - 1] } else { print OUT $_; } } close (OUT); system ("mv temp.pl test.pl");

    Basically, I was just going to say "write the variable to a file and read it", but since you say from "machine to machine", I'm not even sure shared memory would be the answer.

Re: How to use shared/global emory kind of feature in perl
by ig (Vicar) on Mar 31, 2009 at 08:22 UTC

    You might find that one of the Tie modules meets your needs. Perhaps Tie::Persistent would be an easy one to start with or, if you need to deal with concurrent access to the data perhaps Tie::Concurrent. There are Tie modules that tie to a variety of file formats and various databases.