Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

File Slurping Problem in Win32 machine

by fletcher_the_dog (Friar)
on Sep 17, 2003 at 15:02 UTC ( [id://292136]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, I have a script that slurps in some files, does stuff to them, and writes them out again using a standard slurping idiom (yes I know it is generally better to read in a line at a time, that is not the point of the post):
sub EditFile{ my $file = shift; open INFILE,$file; my $text = do{local $/;<INFILE>}; close INFILE; # edit text open OUTFILE,">$file"; print OUTFILE $text; close OUTFILE; }
My problem is that some of these files have a "^Z" or "^A" character in the middle of them. When I slurp these files, the slurp stops at the "^Z" or the "^A", and I lose half the file. How do I get around this? Thanks!

Replies are listed 'Best First'.
Re: File Slurping Problem in Win32 machine
by Zaxo (Archbishop) on Sep 17, 2003 at 15:09 UTC
      Is binmode the same as using the pragma  use open IN=>":RAW";? I ask because I tried this pragma and it did not work.

        Yes, but it's spelled ':raw'. A third way (in recent perl) is to attach the discipline to the mode argument of open, open INPUT, '<:raw', '/path/to/whatever.dat' or die $!;

        After Compline,
        Zaxo

Re: File Slurping Problem in Win32 machine
by jmcnamara (Monsignor) on Sep 17, 2003 at 15:10 UTC

    binmode the filehandle:
    ... open INFILE, $file or die "Add warning here: $!"; binmode INFILE; ...

    --
    John.

Re: File Slurping Problem in Win32 machine
by tcf22 (Priest) on Sep 17, 2003 at 15:35 UTC
    You could always use read() instead of using <> to slurp in the whole file. I normally use that when dealing with files that contain binary data.
    sub EditFile{ my $file = shift; open INFILE,$file; binmode INFILE; my ($buffer, $text); while(read(INFILE, $text, 4096, length($text))){} #Alternative Read method #while(read(INFILE, $buffer, 4096)){ # $text .= $buffer; #} ## close INFILE; # edit text open OUTFILE,">$file"; binmode OUTFILE; print OUTFILE $text; close OUTFILE; }

    - Tom

Re: File Slurping Problem in Win32 machine
by jdtoronto (Prior) on Sep 17, 2003 at 15:12 UTC
    I don't often do that now, but, I think what you need to do is binmode the handle:

    binmode HANDLE;

    there is also a raw option. I understand that the behaviour of this changed in 5.6.1 so you should consult perlfunc in your current manual set.

    jdtoronto

Re: File Slurping Problem in Win32 machine
by tachyon (Chancellor) on Sep 18, 2003 at 04:42 UTC

    I know it is generally better to read in a line at a time

    Depends on you point of view. If memory is an issue it is better. If speed it the issue it is not. As is often the case you can spend Memory and gain speed. Often a lot of speed - Re: Performance Question

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found