perl - 一个简单的perl程序,用于从不同文件发送数据

标签 perl

我编写了一个perl程序,该程序定期将更新的数据从文件发送到远程服务器。但是现在我希望它从不同的文件中读取它并发送更新的数据,以便接收者应该知道如何从混合数据中分离出数据。我是否只需要添加某种分隔符?这类东西已经有标准了吗?

#############
#Change parameters
############
$PeerAddr='192.168.0.7';
$PeerPort='7070';
##############
# Import packages
##############
use Text::Diff;
use IO::Socket;
#############
# Define global variables
#############
$lineCount=0;
$loopCount=0;
our $stats2 = 0;
for($count = 0; $count <= 10000; $count++){
        my $data_dir="archive/otat/*dat";
        my $data_file= `ls -t $data_dir | head -1`;
        chomp($changed_data_file);
        print "old data_file is $changed_data_file \n";
        chomp($data_file);
        if($data_file ne $changed_data_file){
                $lineCount2=0;
                $changed_data_file=$data_file;
                print ("String:$data_file :$changed_data_file  are not equal\n");
                }
        while(defined($data_file)){
                print "$data_file \n";
                open (DAT,$data_file) || die("Could not open file! $!");
                @iofile = <DAT>;
                $lineCount = @iofile;
                splice(@diffLines);
                print "printing: $lineCount\n";
                print "printing 2: $lineCount2 \n";
                chomp $lineCount;
                chomp $lineCount2;
                if($lineCount != $lineCount2){
                        $j=0;
                        for($i=$lineCount2;$i <= $lineCount; $i++){
                        $diffLines[$j] = $iofile[$i];
                                $j++;
                }
                        $num=@diffLines;
                        print "count of diff lines:$num\n";
                        $lineCount2 = $lineCount;
                        $loopCount=0;
                }
                if($loopCount>2){
                        $loopCount=0;
                        print "Look for recent file \n";
                        last;
                }
                $loopCount++;
                sleep(5);
############################
                &socket_con(@diffLines);
        }

}
#### Methods/Functions
sub socket_con {
        if ($sock== 0){
                $sock = new IO::Socket::INET (
                                                        PeerAddr => $PeerAddr,
                                                        PeerPort => $PeerPort,
                                                        Proto => 'tcp'
                                                        );
                die "Could not create socket: $!\n" unless $sock;
        }
        print $sock @_;
#close($sock);
}

最佳答案

我已经使用JSON了很多,而且效果很好http://metacpan.org/pod/JSON您可以将数据存储在哈希中,进行序列化,将文本发送给客户端,然后将其转换为Perl哈希以方便使用。例如:

# on the server
use JSON;
...
# store changed lines in a hash
$diffLines->{$data_file}[$j]=$io_file[$i];
...
# Serialize the hash reference into a string which you then send to the client
$diffLinesSerialized = encode_json $diffLines;

# on the clinet
use JSON;
...
# convert received data from serialized string into hash
$diffLines = decode_json $diffLinesSerialized;
# $diffLines is now a has reference which can be accessed like normal
foreach my $data_file (keys %$diffLines) {
  foreach my $line (@{$diffLines->{$data_file}}) {
    ...
  }
}

综上所述,尽管我从编程角度来看并不非常喜欢XML,但它是这类事情的普遍标准。如果这只是一个专用的内部工具,不会扩展到更大的范围,则可能没有关系,但是如果您认为这可以变成更通用的服务,例如对于非perl客户,最好考虑一下XML作为一种选择。从面向服务的角度进行编程可以使将来的工作变得更容易。

关于perl - 一个简单的perl程序,用于从不同文件发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937906/

相关文章:

linux - 使用 Perl 如何清理没有文件的剩余目录?

perl - Perl 中警告传播的最佳实践

perl - perl打开文件的几种方式

perl - 我可以重新测试所有已安装的 CPAN 模块吗?

xml - 使用 perl 和 LibXML 从 XML 中提取节点并解析结果

perl - 限制数组的大小?

linux - 使用 perl 向调制解调器发送 AT 命令

perl -pe one liner : Replace all matched characters with same number of a different character. 非全局

regex - 如何从字符串中删除和 ID

perl - Perl的system()是否自动在Cygwin中转义字符?