Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
XP is just a number
 
PerlMonks  

variable declaration

by Priti24 (Novice)
on Dec 13, 2011 at 05:09 UTC ( #943247=perlquestion: print w/ replies, xml ) Need Help??
Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

is it possible to declare a variable that can persist its value until the end of program. Actually i have a program which consist a lots of subroutine , i think u can understand my problem through this example very well

my ($i , $j); $i = 1; $j = $i - 1; my abc{}; my bca{ my $records_to_be_feched = $end - (20 * $i) - ($start - (20 * ($j))); $i = $i+1; $j = $j+1; }

firstly, variable i will go with value 1.and after that that it will increment with value 1 . but when it will go out of subroutine its value will be 1 according to global declaration. so can it be possible that first time i will be one and after that it increments by 1 and become 2...

Comment on variable declaration
Download Code
Re: variable declaration
by NetWallah (Monsignor) on Dec 13, 2011 at 05:28 UTC
    Your code does not compile, and does not make sense. (Your communication also leaves a lot to the imagination.)

    Try this code instead:

    use strict; use warnings; my ($i , $j); $i = 1; $j = $i - 1; sub bca{ $i = $i+1; $j = $j+1; } print "Before SUB call \$i=$i\n"; bca(); print "After SUB call \$i=$i\n";

                "XML is like violence: if it doesn't solve your problem, use more."

Re: variable declaration
by ansh batra (Friar) on Dec 13, 2011 at 05:29 UTC
    my ($i , $j); $i = 1; $j = $i - 1; print "before sub value of i=$i\n"; sub abc { $i = $i+1; print "inside sub $i\n"; $j = $j+1; } abc(); print "after sub $i\n";
    out put
    before sub value of i=1 inside sub 2 after sub 2
    prints the edited value.
Re: variable declaration
by GrandFather (Cardinal) on Dec 13, 2011 at 05:32 UTC

    As written your code is not Perl or any other language that I recognise. However, piecing together hints from your rather incoherent description and your totally random code it may be that the following is somewhere near what you want:

    use strict; use warnings; my $obj = bless {i => 1, j => 0}; $obj->run (1100, 20); sub run { my ($self, $end, $start) = @_; my $records_to_be_feched = $end - 20 * $self->{i}++ - ($start - 20 * $self->{j}++); }

    There are many ways this could be achieved. I have chosen to use a light weight object to carry the counters around rather than make them global variables. I've no idea what start and end should be so I've just passed them in as parameters.

    True laziness is hard work
Re: variable declaration
by TJPride (Pilgrim) on Dec 13, 2011 at 09:09 UTC
    I have no idea what you're trying to do, since you didn't really tell us, so I'm going to ignore your code. The short answer is that yes, you can have variables local to a sub that last through multiple calls:

    use strict; use warnings; { my $persistent; sub myFunc { return ++$persistent; }} print myFunc(); print myFunc(); print myFunc();
      In newer Perls, you can also use
      use strict; use warnings; use feature qw/state/; sub myFunc { state $persistent; return $persistent++; } print myFunc(); print myFunc(); print myFunc();

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://943247]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-04-20 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls