adaykin31 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to convert each line in a text file as a string, and then save off the line to the file after I modify the string. How would I go about converting a line in a text file to a string?
  • Comment on Treating file lines in a text file as a string

Replies are listed 'Best First'.
Re: Treating file lines in a text file as a string
by Joost (Canon) on Apr 25, 2008 at 22:49 UTC

      I'll be damned. This reminds me of a story.

      In about 2002 when I'd been hacking Perl quite a bit for 3 years I started to feel like I wanted to know how far I'd come. I was self-taught and pretty uneasy about my skills even though I'd accomplished some large projects, even an international one, at a large company. I looked around online till I found some Perl self-tests. There were three tests: beginner, moderate, expert. Feeling sheepish, I thought I'd better do the beginner first to get a good score and then move on. I expected to ace it.

      I got like a 55% or something. I was so mad I blazed through the expert exam. Got 100% to my amazement. Being self-taught on real projects I knew references and objects and that sort of jazz pretty well, but I couldn't even answer some of the basics because they'd never come up.

      In 9 years of Perl hacking, I did not know till now that there was a readline function in Perl or that there was anything else underlying the diamond operator.

        readline are <> are two names for the same op (readline).

        >perl -MO=Concise -e"<FH>" 5 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 4 <1> readline[t2] vK/1 ->5 3 <#> gv[*FH] s ->4 -e syntax OK >perl -MO=Concise -e"readline(*FH)" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <1> readline[t2] vK/1 ->6 4 <1> rv2gv sKR/1 ->5 3 <#> gv[*FH] s ->4 -e syntax OK

        Similarly, ``, qx`` and readpipe are three names for the same op (backtick).

        and and && are the same op, and or and || are the same op.

        Finally, if and unless are implemented using the and and or ops respectively.

Re: Treating file lines in a text file as a string
by CountZero (Bishop) on Apr 25, 2008 at 23:09 UTC
    use strict; my @lines; # read the whole file into an array { # we use a block here so the lexical filehandle variable goes automa +tically out of scope and gets closed open my $filehandle, '<', 'path/to/my/file'; @lines = <$filehandle>; } # do something with the lines in @lines #now save the array to disk { # we use a block here so the lexical filehandle variable goes automa +tically out of scope and gets closed open my $filehandle, '>', 'path/to/my/file'; print $filehandle @lines; }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Thanks, I was able to get my program working the way I wanted it to!