develooper Front page | perl.beginners | Postings from May 2019

Re: Is there a definition of what is a string in perl, and what isit?

Thread Previous
From:
Uri Guttman
Date:
May 17, 2019 13:58
Subject:
Re: Is there a definition of what is a string in perl, and what isit?
Message ID:
9aadd3ac-cf70-cf44-8e52-7f0eea560a5b@stemsystems.com
On 5/17/19 9:49 AM, hwilmer wrote:
>
> That means "binary data", like you would allocate some memory in C to 
> read a file into (like a jpeg image in my application) or perhaps use 
> a vector of a suitable data type in C++ for such data, or maybe an array.
>
> Despite being a useful simplification, strings are very complicated 
> things.  You might imagine a "binary string" as a number of 
> consecutive bytes, with "consecutive" meaning that one byte comes 
> after the other, and the order in which they come is relevant.  You 
> could describe a string like that, but since all kinds of encodings 
> nowadays come into play, I end up not knowing what I have when being 
> forced to use a "string" in perl because I don't know what else I 
> could use instead, particularly something that is not encumbered by 
> encodings.

one more time. perl strings are BYTE BUFFERS. you can store anything in 
them. they can hold blobs, images, text, etc. they are c buffers 
underneath. there is NO interpretation by perl itself on the string.
>
> It seems I can't even ask perl what encoding it assumes a "string" 
> has, which is really weird because I may really need to know that.
because perl doesn't 'encode' stuff. your code does.
>
>
> my $binary_string = `curl -s -k --max-filesize $MAX_DOWNLOAD_SIZE 
> "$url"`;
>
>
> $url refers to a jpeg image.  I need to store that image in a blob in 
> a database (using DBI).
that will put the image into the scalar. simple. it works. there are 
tons of image munging modules on cpan and they all use scalar variables 
to hold binary data. it just works.
>
> Curl prints binary data to STDOUT and not a string.  I can't have the 
> data encoded or otherwise altered because it would make the image 
> unretrievable.  The only way to get it in per is a what perl considers 
> as a string.
stdout does not mean text. it is just an output handle. it can handle 
any form of data. in a shell script you can pipe images to other 
programs just fine and that uses stdout.
>
>
> How do I prevent the binary data from being altered without even 
> knowing what a string is in perl?
if you don't alter it yourself, it will remain as it was read in. perl 
doesn't do anything without your asking it to do.

just do what you think will work, don't worry about encoding and it 
should be fine. you are not the first person to want binary data. you 
are overthinking this issue.

uri

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About