当前位置:  开发笔记 > 编程语言 > 正文

如何跳转到Perl中的特定输入行?

如何解决《如何跳转到Perl中的特定输入行?》经验,为你挑选了1个好方法。

我想跳到包含"include"的第一行.

<> until /include/;

为什么这不起作用?



1> Robert Gambl..:

匹配运算符默认使用,$_但默认情况下<>运算符不会存储$_,除非它在while循环中使用,因此不会存储任何内容$_.

来自perldoc perlop:

   I/O Operators
   ...

   Ordinarily you must assign the returned value to a variable, but there
   is one situation where an automatic assignment happens.  If and only if
   the input symbol is the only thing inside the conditional of a "while"
   statement (even if disguised as a "for(;;)" loop), the value is auto?
   matically assigned to the global variable $_, destroying whatever was
   there previously.  (This may seem like an odd thing to you, but you’ll
   use the construct in almost every Perl script you write.)  The $_ vari?
   able is not implicitly localized.  You’ll have to put a "local $_;"
   before the loop if you want that to happen.

   The following lines are equivalent:

       while (defined($_ = )) { print; }
       while ($_ = ) { print; }
       while () { print; }
       for (;;) { print; }
       print while defined($_ = );
       print while ($_ = );
       print while ;

   This also behaves similarly, but avoids $_ :

       while (my $line = ) { print $line }

推荐阅读
赛亚兔备_393
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有