Static variables for separate command-line calls?

I

Ishmael

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!
 
J

John Bokma

Ishmael said:
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++;
-------------------------



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?
 
D

Dr.Ruud

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
 
E

Eric Bohlman

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++;
-------------------------



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.
 
X

xhoster

Ishmael said:
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++;
-------------------------



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
 
M

Mark Clements

Eric said:
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
 
E

Eric

Ishmael said:
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++;
-------------------------



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
 
I

Ishmael

Data::Dumper is perfect.
Thanks also for correcting my terminology - "invocation" is exactly the
right word.
 
D

DJ Stunks

Ishmael said:
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++;
-------------------------



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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top