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

All, This may seem trivial to you all, but is there a way to Base64 encode a variable OTHER than a Perl string?? The only examples I've found are with $string. I am trying to encode a file that I have read in as such:
open(DATA, $datafile) || die ("Could not open file"); my @rawdata=<DATA>; close(DATA);
Now I want to Base64 encode @rawdata. Can I do this without having to encode the file itself on the file system? You may ask why I want to do it this way. The reason for this, is because the data is a binary file and I am trying to send it via HTTP through Perl's LWP library. When I attempt to do this, Perl URL-encodes it which I DO NOT want, or at the very least, want to avoid in some manner. Maybe there is a way to simply tell Perl not to URL-encode it. I don't know. Thanks for your help in advance.

Replies are listed 'Best First'.
Re: How to Base64 Encode an array variable??
by ikegami (Patriarch) on Feb 01, 2010 at 15:03 UTC

    Perl URL-encodes it

    No it doesn't. LWP doesn't touch the content of your request or response.

    Now I want to Base64 encode @rawdata

    In general, you'd need to serialize your data in some way.

    In this case, you started with serialized data. Why do you split a binary file into "lines"?

    open(my $fh, '<', $datafile) or die ("Could not open file \"datafile\": $!\n"); binmode($fh); my $rawdata; { local $/; $rawdata = <$fh>; }

    Note that by base64-encoding your bytes, you're making your data 25% bigger for nothing.

      Note that by base64-encoding your bytes, you're making your data 25% bigger for nothing.

      It does allow you to turn binary data into printable text, for storage or transfer. I like to use it to include small images in scripts as data.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku
        I wasn't speaking generally, only about the case at hand. One does not print HTTP file transfers or include them in scripts.
Re: How to Base64 Encode an array variable??
by Anonymous Monk on Feb 01, 2010 at 15:00 UTC
Re: How to Base64 Encode an array variable??
by ambrus (Abbot) on Feb 02, 2010 at 09:55 UTC

    I don't get it. Why would you want to read the file to an array (one line in each element) instead of to a single string? Just slurp the whole thing to a single string and base64-encode it that way, then when you decode it, split it to lines with split /^/.

    If, however, you wanted to encode data other than a simple string, you could first serialize it with one of those modules like Storable. In the particular case of storing just an array of strings, you can get away with encoding it like $str = pack "(J/A)*", @data; and decoding with @data = unpack "(J/A)*", $str;

Re: How to Base64 Encode an array variable??
by ambrus (Abbot) on Feb 02, 2010 at 22:12 UTC

    Also, LWP has a way to use the multipart/form-data format in your POST requests, which will be as short as base64 encoding, that is, likely shorter than the URL-encoding used by the application/x-www-form-urlencoded format for binary data.