`

Perl range operator on Scala Context

    博客分类:
  • perl
阅读更多

 

# file:
#   range_operator.pl
# description:
#   this file is the test file to show how to use the range operator in PERL
#
# conclusion:
#   the state transition of the .. operator is as such 
# 
#
#	 (0, 0)   false    <----  4  ------ (false)  (1, 1)
#	 	        |                        ^
#               |                        |
#(false -> true)1   Left    ..   right   3 (false -> true)
#               |                        |
#               V                        |
#    (1, 0)   (true)    -----   2 -----> true    (0, 1)
#
#
# left is not evaluate in (true) state
# right is not evaluate in (false) state
#
# state flip happen on next round.

use strict;

my $line_no = 0;

print "start", "\n";

my $i = 100;

# the combination of (0, 1) 
# and (1, 0) will evaluate as true
# 
# while (0 .. 1) {
	# print ++$i, "\n";
	# sleep 1;
# }

open file, "< range_operator.pl";


# as you can see from the below output, 
# the line 1 -- 6 return false on expression 1 . . /^$/
#  
my $i = 0;
$i = 0;
my $line_end = 0;

line:
while (<file>) {
	print ("\n") if !$line_end;
	print "iteration -- cut point ", ++$i, " | ";
	$line_end = 0;
	next line if (1 .. /^$/);
	$line_end = 1;
	print "iteration", $i, "\n";
}

open file, "< range_operator.pl";



print "done", "\n";
 

 

 

 

 

The explaination:

 

 

1.  while (0 .. 1) return true because  (0, 1) on the state chart is true; so you will see a line  printed every second.

 

2. The above output shall be as follow.

 

 

iteration -- cut point 1 |

...

iteration -- cut point 23 |

iteration -- cut point 24 | iteration24

...

 

 

as you can see, 

 

 

 

22: # state flip happen on next round.
23:
24: use strict;
25:
 

 

 

though at line 23, the expression /^$/ shall evalute as true, according to the state map, the range operator shall be false, and according to the logic at line 23, it shall be the following output.

 

 

 

iteration -- cut point 23 | iteration23

 

 

 

But why?

 

It is because the state transition will happen the next round. So that explains why the output changes on line 24 rather than line 23;

 

Another thing to notice is that, after the line 24, the output does not change no matter what the text changes to... The reason is because:

  • right is not evaluate in (false) state
  • the left is a constant value (1)

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics