Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Treating the file contents of a text file as a string

by adaykin31 (Novice)
on May 22, 2008 at 07:18 UTC ( [id://687882]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I have two files saved into two variables. I want to treat the contents in each as one big string, how would I do this?
  • Comment on Treating the file contents of a text file as a string

Replies are listed 'Best First'.
Re: Treating the file contents of a text file as a string
by poolpi (Hermit) on May 22, 2008 at 08:26 UTC
Re: Treating the file contents of a text file as a string
by stiller (Friar) on May 22, 2008 at 07:43 UTC
    When you write that you have two files saved into two variables, I read that as meaning that you have read the files content into these variables, is that right? If so, then they are "big" strings, and you treat them as any other string. Or do you mean that you have the names of two files in two variables? A small bit of code would have given a better understanding of what you tried to do, and a spot on answer right away.
Re: Treating the file contents of a text file as a string
by Erez (Priest) on May 22, 2008 at 07:51 UTC

    local $/ = undef; #slurp entire file my $string; open (my $FH, '<', $file1); $string = <$FH>; close $FH; open (my $FH, '<', $file2); $string .= <$FH>; #notice the concatenation close $FH;
    UPDATE: changed the $/ assignment to local.

    Stop saying 'script'. Stop saying 'line-noise'.
    We have nothing to lose but our metaphors.

      It would be better to minimise the change to the global $/ (record separator) variable, as this may cause action at a distance in some other part of the code (or a module). This is done by reducing the scope to the shortest possible block.

      You are also repeating yourself. This is not good.

      my $string = slurp($file1) . slurp($file2); sub slurp { my $file = shift; open $fh, <, $file or do { warn "Failed to open $file for input: $!\n"; return ''; } local $/ = undef; return <$fh>; # $fh goes out of scope, file closed automatically }

      • another intruder with the mooring in the heart of the Perl

      undef $/; #slurp entire file
      That's a very bad idea. You must *NOT* modify special globals like that. Use localised versions and a minimal scope...
Re: Treating the file contents of a text file as a string
by mwah (Hermit) on May 22, 2008 at 08:16 UTC

    From analyzing your question I'd assume that you try to handle the content of each file as a string for itself (no concatenation).

    About one month ago you asked almost the same question w/different wording.

    Please do give us some more words of "problem description" next time ;-)

    (My interpretation of your question follows:)

    my ($filename1, $filename2) = qw( adaykin1.txt adaykin2.txt ); my @string; for my $fn ($filename1, $filename2) { # iterate over given file +names open my $fh, '<', $fn or die "$fn - $!"; # open each file local $/; push @string, <$fh> # read it into one string } # print $string[0]; # first file as one big string print $string[1]; # second file as one big string ...

    Regards

    mwa

      Well it wasn't the same question. Read the last one again and you'll see. What I meant in this question was to read every single character in a file and save every single character in the file into one string. And yes the File::Slurp was exactly what I was looking for this time. Code is working perfectly now, thanks!
Re: Treating the file contents of a text file as a string
by walto (Pilgrim) on May 22, 2008 at 13:02 UTC
    You can also use File::Slurp
    use strict; use warnings; use File::Slurp; my $bigfile = read_file ('file1') . read_file ('file2'); print "$bigfile\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://687882]
Approved by Erez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found