perl - why can't shift the second value? -
i feel confused little perl script. have passed 2 values routin, however, first 1 works in subroutin.
#!usr/bin/perl sub even_number_printer_gen { ( $input1, $input2 ) = @_; #my $input1=shift; #my $input2=shift; print "after shifting, input1 $input1 , input2 $input2\n"; if ( $input1 % 2 ) { $input1++ } if ( $input2 % 2 ) { $input2++ } $rs = sub { #subroutin 1, everytime add 2 in $input1 print "$input1 "; $input1 += 2; }; $rs2 = sub { #subroutin 2, everytime add 3 in $input2 print "$input2 "; $ipnut2 += 3; }; @rs3 = ( $rs, $rs2 ); return @rs3; #return 2 subroutins array } @iterator = &even_number_printer_gen( 31, 20 ); ( $i = 0; $i < 10; $i++ ) { &{ $iterator[0] }; #refer subroutin 1 &{ $iterator[1] }; #refer subroutin 2 print "\n"; } print "done!\n";
the output
after shifting, input1 31 , input2 20 32 20 34 20 36 20 38 20 40 20 42 20 44 20 46 20 48 20 50 20 done!
why second value doesn't change?
you wrote:
$ipnut2 += 3;
ought be:
$input2 += 3;
on computer
f.jardon@xxxxx 11:49:04 ~/tmp $ cat fixedperl.pl #!/usr/bin/perl sub even_number_printer_gen { ( $input1, $input2 ) = @_; #my $input1=shift; #my $input2=shift; print "after shifting, input1 $input1 , input2 $input2\n"; if ( $input1 % 2 ) { $input1++ } if ( $input2 % 2 ) { $input2++ } $rs = sub { #subroutin 1, everytime add 2 in $input1 print "$input1 "; $input1 += 2; }; $rs2 = sub { #subroutin 2, everytime add 3 in $input2 print "$input2 "; $input2 += 3; ## <<<<====================== here fix }; @rs3 = ( $rs, $rs2 ); return @rs3; #return 2 subroutins array } @iterator = &even_number_printer_gen( 31, 20 ); ( $i = 0; $i < 10; $i++ ) { &{ $iterator[0] }; #refer subroutin 1 &{ $iterator[1] }; #refer subroutin 2 print "\n"; } print "done!\n"; f.jardon@xxxxx 11:49:06 ~/tmp $ ./fixedperl.pl after shifting, input1 31 , input2 20 32 20 34 23 36 26 38 29 40 32 42 35 44 38 46 41 48 44 50 47 done!
Comments
Post a Comment