简体   繁体   English

File :: Tail :: select是什么意思?

[英]File::Tail::select mean?

This is great it is work, but has one part I not understand : 很好,很工作,但是有一部分我不明白:

print $_->{"input"}." (".localtime(time).") ".$_-> read;

What does it print? 它打印什么? If I modify it to: 如果我将其修改为:

print "$_";

There are some error. 有一些错误。 Why? 为什么?

#!/usr/local/bin/perl

use File::Tail;
chdir( "/var/log/snort");
foreach my $fol(glob "*.*.*.*")
{
        print "Opening $fol\n";
        chdir("/var/log/snort/$fol");
        foreach my $subfile(glob "*:*")
        {
                print "opening $subfile\n";
                push(@files,File::Tail->new(name=>"$subfile",debug=>$debug));
        }
        while (1)
        {
                ($nfound,$timeleft,@pending)= File::Tail::select(undef,undef,undef,$timeout,@files);
                unless ($nfound)
                {
                   # timeout - do something else here, if you need to
                }
                else
                {
                        foreach (@pending)
                        {
                                print $_->{"input"}." (".localtime(time).") ".$_-> read;
                        }
                }
        }
}

example result: 示例结果:

TCP:34628-80 (Wed Mar 30 01:49:57 2011) 03/30-01:49:50.607858 119.40.116.196:80 -> 192.168.242.133:34628
TCP:34628-80 (Wed Mar 30 01:49:57 2011) TCP TTL:128 TOS:0x0 ID:34869 IpLen:20 DgmLen:40
TCP:34629-80 (Wed Mar 30 01:49:57 2011) 03/30-01:49:51.309716 119.40.116.196:80 -> 192.168.242.133:34629
UDP:41415-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.220999 192.168.242.2:53 -> 192.168.242.133:41415
UDP:44705-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.427011 192.168.242.2:53 -> 192.168.242.133:44705
UDP:50539-53 (Wed Mar 30 01:49:57 2011) 03/30-01:49:47.213455 192.168.242.2:53 -> 192.168.242.133:50539
TCP:34628-80 (Wed Mar 30 01:49:57 2011) ***AP**F Seq: 0x2F3E700A  Ack: 0x2359814F  Win: 0xFAF0  TcpLen: 20
TCP:34629-80 (Wed Mar 30 01:49:57 2011) TCP TTL:128 TOS:0x0 ID:34871 IpLen:20 DgmLen:40
UDP:41415-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34859 IpLen:20 DgmLen:65
UDP:44705-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34861 IpLen:20 DgmLen:153
UDP:50539-53 (Wed Mar 30 01:49:57 2011) UDP TTL:128 TOS:0x0 ID:34857 IpLen:20 DgmLen:179
TCP:34628-80 (Wed Mar 30 01:49:57 2011) =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
TCP:34629-80 (Wed Mar 30 01:49:57 2011) ***AP**F Seq: 0x9D70418  Ack: 0x248089DB  Win: 0xFAF0  TcpLen: 20
UDP:41415-53 (Wed Mar 30 01:49:57 2011) Len: 37
UDP:44705-53 (Wed Mar 30 01:49:57 2011) Len: 125
UDP:50539-53 (Wed Mar 30 01:49:57 2011) Len: 151
TCP:34628-80 (Wed Mar 30 01:49:57 2011) 
TCP:34629-80 (Wed Mar 30 01:49:57 2011) =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

File::Tail::select returns the number of filehandles found, some manner of timeout, and a list of File::Tail objects. File::Tail::select返回找到的文件句柄数,某种方式的超时以及File::Tail对象的列表。 These are in @pending in your code. 这些在您的代码中@pending中。 When you go to print things out, you loop through each member in @pending without explicitly creating a new variable, so each element of @pending gets put into the special $_ variable automatically. 当您打印输出内容时,您将循环遍历@pending每个成员而无需显式创建新变量,因此@pending每个元素都会自动放入特殊的$_变量中。

You first print $_->{"input"} , which is some manner of "input" (the docs are unclear as to what this is), the current time in your timezone, and the string read from the file ( $_->read ). 首先打印$_->{"input"} ,这是某种“输入”方式(文档尚不清楚这是什么),时区中的当前时间以及从文件中读取的字符串( $_->read )。

If you just try to print out $_ you're trying to print out an object, which won't give you expected results (unless said object overloads stringification, which File::Tail doesn't). 如果仅尝试打印$_ ,则尝试打印对象,这不会给您预期的结果(除非所说的对象重载了字符串化,而File::Tail则没有)。

I suggest reading through the File::Tail documentation , along with perldoc perlvar . 我建议仔细阅读File::Tail文档以及perldoc perlvar

Edit : I looked through the File::Tail source and the $_->{"input"} is egregious abuse of object-oriented Perl. 编辑 :我看了File::Tail$_->{"input"}是对面向对象Perl的严重滥用。 This is actually the object's internal representation of the name parameter to the object creator. 这实际上是对象创建者name参数的内部表示。 You should properly access it by changing $_->{"input"} to $_->name . 您应该通过将$_->{"input"}更改$_->name来正确访问它。 I note that it's not your fault that your code is written this way, as the File::Tail documentation uses exactly this syntax, but it's incorrect and should be changed. 我注意到以这种方式编写代码不是您的错,因为File::Tail文档正是使用这种语法,但是它是不正确的,应该更改。

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

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