Home > Archive > PERL Beginners > October 2005 > help slurping a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
help slurping a file
|
|
| Renee Halbrook 2005-10-28, 7:56 am |
| Hi,
I'm not sure if this is the correct group to post this question to. If there
is a better forum for this kind of question, please let me know.
I am trying to slurp a file that was written on Mac OSX, using a standard
text editor.
I am developing on a windows machine, and running my scripts on a Linux box.
I get the same problem on both machines.
If I set the local($\ = "\r")
I can get a line by line read of the file.
However, if I set the
local($\=undef)
or
local($\="")
The script will only read in the first line of the file, and then quit.
I got a read of the byte code in java, and the endline character is indeed
"\r".
My question is, why can't I slurp in the entire file?
Below is a sample of my code:
#!/usr/local/bin/perl
use warnings;
my $file = "pathnameofmyfile";
open( MYFILE, "$file" ) or die "Can't open $file for reading: $!\n";
readfile();
close(MYFILE);
sub readfile(){
local($/) = "\r";
while ( my $line = <MYFILE> ){
print "line is $line\n";
}
}
Renee Halbrook
Bioinformatics Programmer
The Carnegie Institution of Washington
Department of Plant Biology
260 Panama Street
Stanford, CA 94305
| |
| John W. Krahn 2005-10-28, 7:56 am |
| Renee Halbrook wrote:
> Hi,
Hello,
> I'm not sure if this is the correct group to post this question to. If there
> is a better forum for this kind of question, please let me know.
>
> I am trying to slurp a file that was written on Mac OSX, using a standard
> text editor.
> I am developing on a windows machine, and running my scripts on a Linux box.
> I get the same problem on both machines.
>
> If I set the local($\ = "\r")
> I can get a line by line read of the file.
> However, if I set the
> local($\=undef)
> or
> local($\="")
$\ is the *OUTPUT RECORD SEPARATOR* so it will have no effect on how files are
read in.
> The script will only read in the first line of the file, and then quit.
> I got a read of the byte code in java, and the endline character is indeed
> "\r".
> My question is, why can't I slurp in the entire file?
perldoc -q "How can I read in an entire file all at once"
John
--
use Perl;
program
fulfillment
| |
| JupiterHost.Net 2005-10-28, 6:56 pm |
| Hello,
> My question is, why can't I slurp in the entire file?
Because you're fiddling with things that ought not be fiddled with ;p
(IE $\)
instead:
use File::Slurp;
my @lines = read_file($file);
or you can use Perl 6's slurp() via a Perl6 module (perl6::Slurp maybe
???) see cpan for details.
| |
| Xavier Noria 2005-10-28, 6:56 pm |
| On Oct 28, 2005, at 16:16, JupiterHost.Net wrote:
> Hello,
>
>
>
> Because you're fiddling with things that ought not be fiddled
> with ;p (IE $\)
>
> instead:
>
> use File::Slurp;
>
> my @lines = read_file($file);
Wrong, that code assumes $file has the runtime platform conventions,
which is not the case.
You'll end up with the entire file as a single string in @lines, not
the expected array of lines. The reason is that \015 is the line
separator in the file. Hence, seen as a text file in a Linux box it
has only one line.
To make it work with File::Slurp that way we need to do precisely
what you said we ought not to: fiddle with $/ before the call to
read_file().
-- fxn
| |
| JupiterHost.Net 2005-10-28, 6:56 pm |
|
Xavier Noria wrote:
>
>
> Wrong, that code assumes $file has the runtime platform conventions,
> which is not the case.
OK sorry, sheesh... missed the part about it being a screwy file.
Can you change the crewy line ending to a normal one then use it?
If not don't yell, I'm just trying to help...
|
|
|
|
|