简体   繁体   English

触发器运算符/ readline-counter问题

[英]flip-flop-operator/readline-counter question

Reading this Perl: extract rows from 1 to n (Windows) I didn't understand the flip-flop-operator/readline-counter part. 阅读此Perl:从1到n提取行(在Windows中)我不了解flip-flopoperator / readline-counter部分。

perl -nE 'say $c if $c=1..3' my_file 

1 1个
2 2
3E0 3E0

Could someone explain me more detailed where this output comes from? 有人可以详细解释此输出的来源吗?

To quote perlop : 引用perlop

In scalar context, " .. " returns a boolean value. 在标量上下文中,“ .. ”返回布尔值。 The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed , awk , and various editors. 运算符是双稳态的,就像触发器一样,它模拟sedawk和各种编辑器的行范围(逗号)运算符。 Each " .. " operator maintains its own boolean state, even across calls to a subroutine that contains it. 每个“ .. ”运算符都保持自己的布尔状态,即使在包含该状态的子例程的调用之间也是如此。 It is false as long as its left operand is false. 只要其左操作数为false,它就是false。 Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. 一旦左操作数为true,则范围运算符将保持为true,直到右操作数为true,此后范围运算符将再次变为false。 It doesn't become false till the next time the range operator is evaluated. 直到下次评估范围运算符时,它才会变为假。 It can test the right operand and become false on the same evaluation it became true (as in awk ), but it still returns true once. 它可以测试正确的操作数,并在变为true的相同评估中变为false(如在awk中一样 ),但一次仍返回true。 If you don't want it to test the right operand until the next evaluation, as in sed , just use three dots (" ... ") instead of two. 如果您不希望它在下一次求值之前测试正确的操作数,如sed中所示 ,只需使用三个点(“ ... ”)而不是两个。 In all other regards, " ... " behaves just like " .. " does. 在所有其他方面,“ ... ”的行为与“ .. ”的行为相同。

The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. 当运算符处于“ false”状态时,不评估右操作数,而当运算符处于“ true”状态时,不评估左操作数。 The precedence is a little lower than || 优先级略低于|| and && . && The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. 返回的值是空字符串(如果为false)或序列号(从1开始)为true(真)。 The sequence number is reset for each range encountered. 将为遇到的每个范围重置序列号。 The final sequence number in a range has the string "E0" appended to it , which doesn't affect its numeric value, but gives you something to search for if you want to exclude the endpoint. 范围中的最后一个序列号附加有字符串“ E0” ,它不影响其数值,但是如果要排除端点,则可以进行搜索。 You can exclude the beginning point by waiting for the sequence number to be greater than 1. 您可以通过等待序列号大于1来排除起点。

If either operand of scalar " .. " is a constant expression, that operand is considered true if it is equal ( == ) to the current input line number (the $. variable). 如果标量“ .. ”的任何一个操作数是一个常量表达式,则该操作数如果等于( == )当前输入行号( $.变量),则认为该操作数为true。

(emphasis added) (强调)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM