使用双引号从文件中读取行

时间:2014-07-25 13:50:44

标签: perl quotes getline

perl用双引号读取行的最佳方法是什么? 例如,

            " This is ""an example"" of double double quotes" 

我需要阅读此行并执行一些操作并将其保存回文件。 如果我尝试读取此文件,Getline将失败。 有没有更好的方法来读取此文件并执行行到线操作?

1 个答案:

答案 0 :(得分:1)

Perl中没有 getline 。你可能在谈论IO->getline()吗?

嗯...

文件test.txt:

" This is ""an example"" of double double quotes" 

Perl程序:

#! /usr/bin/env perl
use warnings;
use strict;
use feature qw(say);
use autodie;

say "Using builtin Perl file operations.";
open my $fh, "<", "test.txt";
while ( my $line = <$fh> ) {
    chomp $line;
    say "The line is <$line>";
}
close $fh;

say "Using IO::File in order to use 'getline'.";
use IO::File;
my $io = IO::File->new;
$io->open("test.txt");
while ( my $line = $io->getline ) {
    chomp $line;
    say "The line is <$line>";
}

打印:

$ ./test.pl
Using builtin Perl file operations.
The line is <" This is ""an example"" of double double quotes" >
Using IO::File in order to use 'getline'.
The line is <" This is ""an example"" of double double quotes" >

使用内置Pler文件方法或使用getline中的IO::File读取包含多个引号的行完全没有问题。

您可以更具体地了解您的体验吗?你想做什么?你的代码是什么?

你在谈论Perl还是Python?