Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Reading and unpacking on one line

by tybalt89 (Prior)
on Nov 05, 2020 at 16:31 UTC ( #11123429=note: print w/replies, xml ) Need Help??

in reply to Reading and unpacking on one line

#!/usr/bin/perl use strict; # use warnings; open FILNAM, '<', \<<END or die $!; 1234567890 abcdefghijkl END my $val = unpack 'H32', do { local $/ = \16; <FILNAM> }; print "$val\n";

UPDATED: changed input to an in memory file to make program completely self contained.

Replies are listed 'Best First'.
Re^2: Reading and unpacking on one line
by Anonymous Monk on Nov 05, 2020 at 22:18 UTC

    Thanks to all for the answers. I understand what happened now. :)

    I will ask you this, since you slightly changed the method used to read the data (not that I need to know for this particular case, but just want to increase my understanding in general) - how do I specify reading 16 characters (perhaps UTF-8, for example) versus reading 16 bytes (as in pure binary data?

    Thank you again

      The behaviour depends on the filehandle. It's documented at the end of read

      > depending on the status of the filehandle, either (8-bit) bytes or characters are read.

      Similarly, sysread:

      > Note that if the filehandle has been marked as ":utf8", Unicode characters are read instead of bytes

      So, to read UTF-8, open the file handle with the :encoding(UTF-8) layer.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      I don't do UTF. Maybe someone else will answer this.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11123429]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-02-28 20:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found