Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a definition of what is a string in perl, and what is it?

4 views
Skip to first unread message

hwilmer

unread,
May 16, 2019, 1:00:16 PM5/16/19
to begi...@perl.org

Hi,

since I'm trying to use "binary strings", the question comes up if there
is a definition of what is a string in perl, and what is it?

Uri Guttman

unread,
May 17, 2019, 10:15:04 AM5/17/19
to begi...@perl.org
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

Uri Guttman

unread,
May 21, 2019, 3:30:05 PM5/21/19
to hwilmer, Perl Beginners
On 5/21/19 8:10 AM, hwilmer wrote:

> I'm not overthinking anything.  I was trying to find out if what
> currently happens to work under given conditions does so by chance or
> luck, or if I can be reasonably sure that it was done right because it
> is how things work, and it will work under different conditions the
> same way.
i will repeat one last time. perl has byte strings which can hold
anything. period. there is no more explanation needed. it works for you
because that is how it works. it isn't luck or superstition. it just
works. perl just works like this.

>
> The results have lead to some improvements and have shown that, while
> somewhat inconclusive, there is no other way to do it in perl because
> the data type I would need does not exist in perl. That will have to
> suffice.
perl doesn't have data typing as such.  a scalar can hold an integer, a
float or a string. the string can be anything in a byte buffer.

i didn't eveb realize the last few emails were off list. please only
reply on list.

uri
0 new messages