Static variables for separate command-line calls?

Discussion in 'Perl Misc' started by Ishmael, Jan 15, 2006.

  1. Ishmael

    Ishmael Guest

    I'd like to store a variable across separate function calls from the
    command line.
    For example, I now have this code:

    --- myfunction.pl ----
    #!/usr/bin/perl
    my $num = 0;
    print "$num\n";
    $num++;
    -------------------------

    >From the command line, I type:


    > perl myfunction.pl


    I would like the value of $num to be stored 'statically', so it is
    initialized only the first time the function is called and incremented
    every time thereafter. Is there some keyword that will allow me to do
    this?

    Thanks for your help!
    Ishmael, Jan 15, 2006
    #1
    1. Advertising

  2. Ishmael

    John Bokma Guest

    "Ishmael" <> wrote:

    > I'd like to store a variable across separate function calls from the
    > command line.
    > For example, I now have this code:
    >
    > --- myfunction.pl ----
    > #!/usr/bin/perl
    > my $num = 0;
    > print "$num\n";
    > $num++;
    > -------------------------
    >
    >>From the command line, I type:

    >
    >> perl myfunction.pl

    >
    > I would like the value of $num to be stored 'statically', so it is
    > initialized only the first time the function is called and incremented
    > every time thereafter. Is there some keyword that will allow me to do
    > this?


    In order to be statically it has to be stored somewhere. Where do you want
    to store it?

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
    John Bokma, Jan 15, 2006
    #2
    1. Advertising

  3. Ishmael

    Dr.Ruud Guest

    John Bokma:
    > Ishmael:


    >> I'd like to store a variable across separate function calls from the
    >> command line. [...]
    >> I would like the value of $num to be stored 'statically', so it is
    >> initialized only the first time the function is called and
    >> incremented every time thereafter. Is there some keyword that will
    >> allow me to do this?

    >
    > In order to be statically it has to be stored somewhere. Where do you
    > want to store it?


    In grandfather's locker. ;)

    http://home.planet.nl/~lind0399/naarbed.html

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Jan 15, 2006
    #3
  4. Ishmael

    Eric Bohlman Guest

    "Ishmael" <> wrote in news:1137352950.435311.274580
    @z14g2000cwz.googlegroups.com:

    > I'd like to store a variable across separate function calls from the
    > command line.
    > For example, I now have this code:
    >
    > --- myfunction.pl ----
    > #!/usr/bin/perl
    > my $num = 0;
    > print "$num\n";
    > $num++;
    > -------------------------
    >
    >>From the command line, I type:

    >
    >> perl myfunction.pl

    >
    > I would like the value of $num to be stored 'statically', so it is
    > initialized only the first time the function is called and incremented
    > every time thereafter. Is there some keyword that will allow me to do
    > this?


    You're not talking about function calls here, you're talking about
    separate process invocations. This means you need to use some form of
    persistent storage; each invocation has to look for a disk file stored by
    the previous one in order to initialize its variables. Look at
    Data::Dumper and Storable.
    Eric Bohlman, Jan 15, 2006
    #4
  5. Ishmael

    Guest

    "Ishmael" <> wrote:
    > I'd like to store a variable across separate function calls from the
    > command line.
    > For example, I now have this code:
    >
    > --- myfunction.pl ----
    > #!/usr/bin/perl
    > my $num = 0;
    > print "$num\n";
    > $num++;
    > -------------------------
    >
    > >From the command line, I type:

    >
    > > perl myfunction.pl

    >
    > I would like the value of $num to be stored 'statically', so it is
    > initialized only the first time the function is called and incremented
    > every time thereafter.


    Well, if you only it initialized the first time, you had better get rid
    of the unconditional "$num = 0".

    > Is there some keyword that will allow me to do
    > this?


    Not that I know of, but there are several "tie" modules you could use,
    which will serialize the variable to disk.

    I'll use DBM::Deep, not because it is necessarily the best one for this
    particular job, but becaus I'm too lazy to look into it enough to decide if
    it is the best for this task. (One consequence is that you have to an hash
    or an array, even if you only use one slot of that hash or array.)

    use DBM::Deep;
    tie my %num, "DBM::Deep", "numvalue.db" or die $!;
    $num{num}++;
    print $num{num};

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jan 15, 2006
    #5
  6. Eric Bohlman wrote:
    > "Ishmael" <> wrote in news:1137352950.435311.274580
    > @z14g2000cwz.googlegroups.com:
    >
    >> I'd like to store a variable across separate function calls from the
    >> command line.
    >> For example, I now have this code:
    >>
    >> --- myfunction.pl ----
    >> #!/usr/bin/perl
    >> my $num = 0;
    >> print "$num\n";
    >> $num++;
    >> -------------------------
    >>
    >> >From the command line, I type:

    >>
    >>> perl myfunction.pl

    >> I would like the value of $num to be stored 'statically', so it is
    >> initialized only the first time the function is called and incremented
    >> every time thereafter. Is there some keyword that will allow me to do
    >> this?

    >
    > You're not talking about function calls here, you're talking about
    > separate process invocations. This means you need to use some form of
    > persistent storage; each invocation has to look for a disk file stored by
    > the previous one in order to initialize its variables. Look at
    > Data::Dumper and Storable.


    Cache::Cache might also be worth checking out.

    Mark
    Mark Clements, Jan 15, 2006
    #6
  7. Ishmael

    Eric Guest

    Ishmael wrote:
    > I'd like to store a variable across separate function calls from the
    > command line.
    > For example, I now have this code:
    >
    > --- myfunction.pl ----
    > #!/usr/bin/perl
    > my $num = 0;
    > print "$num\n";
    > $num++;
    > -------------------------
    >
    > >From the command line, I type:

    >
    > > perl myfunction.pl

    >
    > I would like the value of $num to be stored 'statically', so it is
    > initialized only the first time the function is called and incremented
    > every time thereafter. Is there some keyword that will allow me to do
    > this?
    >
    > Thanks for your help!


    How about File:CounterFile (Persistent counter class)
    http://search.cpan.org/dist/File-CounterFile/CounterFile.pm
    Eric, Jan 15, 2006
    #7
  8. Ishmael

    Ishmael Guest

    Data::Dumper is perfect.
    Thanks also for correcting my terminology - "invocation" is exactly the
    right word.
    Ishmael, Jan 15, 2006
    #8
  9. Ishmael

    DJ Stunks Guest

    Ishmael wrote:
    > I'd like to store a variable across separate function calls from the
    > command line.
    > For example, I now have this code:
    >
    > --- myfunction.pl ----
    > #!/usr/bin/perl
    > my $num = 0;
    > print "$num\n";
    > $num++;
    > -------------------------
    >
    > >From the command line, I type:

    >
    > > perl myfunction.pl

    >
    > I would like the value of $num to be stored 'statically', so it is
    > initialized only the first time the function is called and incremented
    > every time thereafter. Is there some keyword that will allow me to do
    > this?
    >
    > Thanks for your help!


    I used DB_File (http://search.cpan.org/~pmqs/DB_File-1.814/DB_File.pm)
    as outlined in O'Reilly's "Programming the Perl DBI" for a similar
    task.

    -jp
    DJ Stunks, Jan 16, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EvgueniB
    Replies:
    1
    Views:
    623
    Anthony Borla
    Dec 15, 2003
  2. Honne Gowda A
    Replies:
    2
    Views:
    872
    Karl Heinz Buchegger
    Oct 31, 2003
  3. Frank Fredstone
    Replies:
    1
    Views:
    436
    Jean-Francois Briere
    Jun 27, 2006
  4. johnny
    Replies:
    4
    Views:
    1,496
    Dennis Lee Bieber
    Dec 8, 2006
  5. ssylee
    Replies:
    2
    Views:
    728
    James Kanze
    Jan 28, 2008
Loading...

Share This Page