>  -------Original Message-------
>  From: anirudh nair <[email protected]>
>  Subject: [twincling] Perl query
>  Sent: 14 Jun '09 9:59pm
>  
>  Hello guys
>  
>  #!/usr/bin/perl
>  sub total_add{
>          state @array;
>          state $sum=0;
>          foreach $x (@_){
>                  push @array,$x;
>                  $sum+=$x;
>          }
>          print "The sum of (@array) is $sum\n";
>  }
>  
>  Now, when I run this code I get the following error
>  
>  anir...@ani-lap:~/code/perl$ ./state.plx
>  Array found where operator expected at state.plx line 4, at end of line
>      (Missing operator before ?)
>  syntax error at state.plx line 4, near "state @array"
>  Execution of state.plx aborted due to compilation errors.
>  
>  Now when i add 'use 5.010' to the code it works fine
>  I use Perl 5.10.0.
>  
>  What is the reason for this behaviour?
>  
>  cheers
>  Anirudh

Anirudh,

Beginning with perl 5.9.4, you can declare variables with the state keyword in 
place of my. For that to work, though, you must have enabled that feature 
beforehand, either by using the feature pragma, or by using -E on one-liners.

For example, the following code maintains a private counter, incremented each 
time the gimme_another() function is called:

    use feature 'state';
    sub gimme_another { state $x; return ++$x }

Hope this helps!

Best Regards,
Sumit

Reply via email to