static(?) variables?

i use a script that initialises some values to
be zero....then during the script these values are incremented but seeing as i have the values defined
to be zero first, when the script is called again
the values are reset!
is there a way to define a variable to be zero
and then if it changes it will always stay changed

thanks
paul
paulwhelanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mjswartCommented:
You want a global variable. Most variables in perl are global by default.

From programming Perl:
....global variables ... work a bit like C's file static variables or C++'s class static variables.

What you need to do then is initialise the variable at the top of the file where it will be executed first and more importantly only once.

perhaps even:

BEGIN { $myvar = 0 }

at the top of the file

0
paulwhelanAuthor Commented:
i put the variables in like this BEGIN { $myvar = 0;$myvar2=0; etc }
but they still get set to zero when the script runs again
paul
 
0
mjswartCommented:
When you run a script more than once, subsequent executions appear brand new to the interpreter. What's to distinguish the first time a script get's run from a second time a script gets run?

Perhaps you could store the value when the script exits and load the value when the script starts.
0
paulwhelanAuthor Commented:
"Perhaps you could store the value when the script exits and load the
                     value when the script starts."
yes but how do u do that?
paul
0
sludinCommented:
There are a couple of ways.  A simple one would be to read/write the values to a file:

BEGIN
{
  open ( FILE, "vars.txt" ) || die $!;
  my $line = <FILE>;
  chomp $line;
  $myvar1 = $line;
  $line = <FILE>;
  chomp $line;
  $myvar2 = $line;
  close( FILE );
}

END
{
  open ( FILE, ">vars.txt" ) || die $!;
  print FILE $myvar1, "\n";
  print FILE $myvar2, "\n";
  close( FILE );
}

Or some variation on that.

-stephen

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.