in reply to usage of "my" keyword

You have use strict; so you must declare variables or the error you see will be thrown. If you've copied/paste this without understanding I suggest you read and understand strict. An alternative would be to declare $i within the for loop.

"How to merge these three lines"

One way to write this:

use strict; use warnings; my $number=8; for( my $i=0; $i<20; $i++ ){ print "present value of the number is: $number\n"; $number++; }

If you are new to perl the following links will likely be of interest:

Replies are listed 'Best First'.
Re^2: usage of "my" keyword
by ravi45722 (Pilgrim) on Aug 10, 2015 at 09:28 UTC
    Here if i use my keyword for number its scope is also available in other functions. I want to restrict the scope of number. Thanks for reply
      No, if you declare $i inside the for loop the way marto has shown, the scope of $i will be only the for loop itself, not outside that block. Consider this example:
      $ perl -E 'for( my $i=0; $i<10; $i++ ){say "Inside loop: $i";} say "Ou +tside loop: $i";' Inside loop: 0 Inside loop: 1 Inside loop: 2 Inside loop: 3 Inside loop: 4 Inside loop: 5 Inside loop: 6 Inside loop: 7 Inside loop: 8 Inside loop: 9 Outside loop:
      You can see that the value of $i is not defined outside the loop. And if I add the warnings, I get:
      ... Inside loop: 9 Use of uninitialized value $i in concatenation (.) or string at -e lin +e 1. Outside loop:
      I want to restrict the scope of number.

      It's not clear to me, but it seems possible you're actually asking "I want to restrict the scope of the $number scalar." If that's the case, then one way is to establish an independent scope  { ... } for the variable:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $number = 42; print '$number before independent scope == ', $number; ;; { my $number = 8; print '$number in independent scope == ', $number++ for 1 .. 5; } ;; print '$number after independent scope == ', $number; " $number before independent scope == 42 $number in independent scope == 8 $number in independent scope == 9 $number in independent scope == 10 $number in independent scope == 11 $number in independent scope == 12 $number after independent scope == 42

      Update: Discussions of scoping can be found throughout perlsyn and among the Tutorials in the Variables and Scoping section.


      Give a man a fish:  <%-(-(-(-<

      Here if i use my keyword for number its scope is also available in other functions. I want to restrict the scope of number.

      What other functions???