perl用双引号读取行的最佳方法是什么? 例如,
" This is ""an example"" of double double quotes"
我需要阅读此行并执行一些操作并将其保存回文件。 如果我尝试读取此文件,Getline将失败。 有没有更好的方法来读取此文件并执行行到线操作?
答案 0 :(得分:1)
Perl中没有 getline 。你可能在谈论IO->getline()
吗?
嗯...
" This is ""an example"" of double double quotes"
#! /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?