Search code examples
perlquotesgetline

Reading line from file with double double quotes


What is the best way in perl to read line with double double quotes? Example ,

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

I need to read this line and perform some manipulation and save it back onto a file . Getline fails if i try to read this file . Is there a better method to read this file and perform line to line manipulation ?


Solution

  • There's no getline in Perl. Are you possibly talking about IO->getline()?

    Hmmm...

    File test.txt:

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

    Perl program:

    #! /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>";
    }
    

    This prints:

    $ ./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" >
    

    I am having absolutely no trouble reading lines with multiple quotes using either the builtin Pler file methods, or using getline from IO::File.

    Can you be more specific in what you're experiencing? What are you attempting to do? What is your code?

    Are you talking about Perl or Python?