Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to bypass File::Slurp when it is required by another module?

by vitoco (Friar)
on Sep 17, 2018 at 06:02 UTC ( #1222497=perlquestion: print w/replies, xml ) Need Help??

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

Hi! This seems to be quite repetitive but I still cannot figure out how to solve this...

I'm using Email::MIME::CreateHTML to build an email that contents an HTML message with a PNG image that I want to include in the email encoded as BASE64. Everything seems to work OK except for the fact that in the email, when displayed by the recipient's email client, the image is broken. I've decoded the BASE64 part of the multipart body and found that the encoded file had 1 byte less that the original image file. So I binary compared the original and decoded files and found that the missing byte was because read_file in File::Slurp always does a CR/LF into NL substitution with the asumption that it is a text file just because I'm running in a Windows machine ($buf =~ s/\015\012/\n/g if $is_win32 ;), and this binary image just happens to have that sequence of bytes in it's header.

Is there a way to replace the read_file method, without editing the modules or rewriting a whole resolver, or to force it to use binmode? Well, Slurp accepts some options like binmode, but this obvously needs CreateHTML to send that option to it, and I'm not sure if that works with small binary files.

Thanks!

Replies are listed 'Best First'.
Re: How to bypass File::Slurp when it is required by another module? (monkeypatch)
by beech (Parson) on Sep 17, 2018 at 06:21 UTC

    Hi,

    Where is the call to read_file exactly? That is what you should fix/bugreport to use binmode

    You can override in your program by "monkeypatch" (an emergency measure)

    sub File::Slurp::read_file { ... } or local *File::Slurp::read_file = sub { ... };

      Last time I looked the module was borked rt://83126.

Re: How to bypass File::Slurp when it is required by another module?
by Aldebaran (Deacon) on Sep 17, 2018 at 22:53 UTC

    One solution is to re-write with Path::Tiny. It took me some time, but I'm happy that File::Slurp gets used, required by nothing going forward.

    use Path::Tiny; # reading files $guts = $file->slurp; $guts = $file->slurp_utf8; @lines = $file->lines; @lines = $file->lines_utf8;

    They say not to re-invent the wheel, but I think you have to fix the flats. Uri Guttman might be the least pleasant person I've "met" in perl circles, and I wouldn't be surprised if he had interpersonal reasons for not maintaining the module that people know him for.

      Hi,

      slurp_raw for verbatim file :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1222497]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-06-06 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?