in reply to usage of "my" keyword

Precision in language is crucial to writing your code. In this case, you're saying a present value is that value, post-incremented. Strictly speaking, that's true, but writing the code in this fashion is apt to trip the unwary -- author included:

#!/usr/bin/perl =w use strict; #1137973 my $number=8; for(my $i=0; $i<3; $i++) { print "present value of \$i is $i and the number is: $number \n"; print " now printing \$number++: " . $number++ . "\n"; # NOTE O +UTPUT! print "!! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10\ +n"; print " and after post-increment, \$i is STILL $i but number +is: $number\n\-----------\n"; } =head present value of $i is 0 and the number is: 8 now printing $number++: 8 # ie, unchanged! !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 0 but number is: 9 ----------- present value of $i is 1 and the number is: 9 now printing $number++: 9 !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 1 but number is: 10 ----------- present value of $i is 2 and the number is: 10 now printing $number++: 10 !! NOW, NOT EARLIER the post-increment occurs AFTER Ln 10 and after post-increment, $i is STILL 2 but number is: 11 ----------- =cut

You're cheese-paring the size of your script by a few bytes at the expense of clarity; better to post-increment in a standalone line of code -- for clarity.

UPDATE: ... or, as Athanasius pointed out in a message, a (standalone) pre-increment (++$number)could also be used effectively ... but that would REALLY require precise language... as in something like "the next number is: " or maybe "oops, off by one."

Replies are listed 'Best First'.
Re^2: usage of "my" keyword (quibble re clarity)
by ravi45722 (Pilgrim) on Aug 20, 2015 at 06:21 UTC

    I read all the manuals above suggested. But i have a small doubt

    #!/usr/bin/perl use strict; # my $row; . . for $row ($min..$max) { if($data == $required) { last; } } print "Data presented in:: $row \n";

    Here i am declaring the $row above the for loop. But the $row loosing its scope after the for loop. As per documents the scope of my variable does not lose.

    if i dont want to loose that variable scope & also i want to use strict then how can i edit it

      if i dont want to loose that variable scope & also i want to use strict then how can i edit it

      Its simple, use another variable that isn't $row

      my $what; for my $row ( $min .. $max ){ if( $data == $required ){ $what = $row; last; } } print "Data presented in:: $row \n";

        I am done in the same way. But i think MONKS will reduce the code without creating another module. Anyway thanks for your reply