Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: variable declaration
by GrandFather (Saint) 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 NetWallah (Canon) 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 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();
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.