Perl range operator on Scala Context

 

# 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)

 

在Flutter中,出现 “null check operator used on a null value” 报错通常是因为代码中使用了非空断言运算符 `!` ,但实际该对象为 `null`。以下是针对不同场景的解决方法: ### Android与Flutter混编导入GetX框架配置getPages参数出错 对于在Android与Flutter混编中导入GetX框架时,配置getPages参数出错导致的该报错,虽然配置的getPages为数组且可为空,但仍出现错误。需要仔细检查getPages数组的初始化和赋值过程,确保其中没有意外的 `null` 值。可以在赋值前进行空值检查,避免使用非空断言运算符 `!` 。例如: ```dart if (getPages != null) { // 进行相关操作 } ``` ### 通过sentry平台看到的ChangeNotifier.notifyListeners处报错 当出现 `_CastError Null check operator used on a null value change_notifier.dart in ChangeNotifier.notifyListeners at line 218` 报错时,需要检查 `ChangeNotifier` 的使用情况。可能是在调用 `notifyListeners` 方法时,相关的监听者列表或者状态对象为 `null`。可以在调用 `notifyListeners` 之前进行空值检查: ```dart if (this.hasListeners) { notifyListeners(); } ``` ### `_formKey.currentState` 为 `null` 报错 在使用 `_formKey.currentState!.validate()` 出现报错时,是因为 `_formKey.currentState` 可能为 `null`。需要确保在调用 `validate` 方法之前,`Form` 组件已经被正确构建并挂载到 `widget` 树中。可以在 `initState` 方法中添加一个延迟调用,确保 `Form` 已经构建完成: ```dart @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { if (_formKey.currentState != null) { _formKey.currentState!.validate(); } }); } ``` ### 使用 `Overlay` 传入 `context` 报错 当使用 `Overlay.of(context)` 传入 `context` 出现该报错时,是因为传入的 `context` 可能不在当前 `widget` 树中。需要确保传入 `Overlay.of(context)` 的 `context` 是在当前 `widget` 树中的。可以使用 `Builder` 组件来获取正确的 `context`: ```dart Builder( builder: (BuildContext context) { // 使用 context 调用 Overlay.of(context) return ElevatedButton( onPressed: () { Overlay.of(context)?.insert(...); }, child: Text('Open Overlay'), ); }, ); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值