By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

reading file into string

P: 36
Hi ,
i have file of 32kb , i want to read the whole file into string ,
i tried this by doing the below code, but i dint got the whole content of the file in the string , i guess the variable is not able to hold the all data, is there any way where i can achive the same?

Expand|Select|Wrap|Line Numbers
  1. open(FH, "$file");
  2.     my @output = <FH>;
  3.     close FH;
  4. foreach $logmsg (@output)
  5.     {
  6.         $sdpString = "$sdpString" ."$logmsg";
  7.  
  8.     }
  9.  
Aug 20 '08 #1
Share this Question
Share on Google+
3 Replies


KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. open(FH, $file) or die "$!";
  2. my $sdpString = do{local $/; <FH>;};
  3. close FH;
  4. print $sdpString;
  5.  
Aug 20 '08 #2

chaosprime
P: 5
I typically do like so:
Expand|Select|Wrap|Line Numbers
  1.       open(FH, $file) or die "$!";
  2.       my $sdpString = join('', <FH>);
  3.       close FH;
  4.       print $sdpString;
  5.  
I do want to say, though, that if it's in any way feasible for you to do whatever operation you're doing on one line of the file at a time, you ought to try doing it that way. The structure of Perl encourages you to do things one-line-at-a-time because it's a good idea.
Aug 20 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
I typically do like so:
Expand|Select|Wrap|Line Numbers
  1.       open(FH, $file) or die "$!";
  2.       my $sdpString = join('', <FH>);
  3.       close FH;
  4.       print $sdpString;
  5.  
I do want to say, though, that if it's in any way feasible for you to do whatever operation you're doing on one line of the file at a time, you ought to try doing it that way. The structure of Perl encourages you to do things one-line-at-a-time because it's a good idea.
Do it the way I show above, much more effcient than using join(), but if its a small file it won't matter much.
Aug 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.