Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Reading and unpacking on one line

by choroba (Cardinal)
on Nov 05, 2020 at 16:19 UTC ( [id://11123428]=note: print w/replies, xml ) Need Help??


in reply to Reading and unpacking on one line

From read:

> Returns the number of characters actually read, 0 at end of file, or undef if there was an error

Therefore, the second version is equivalent to

$val = unpack 'H32', 16;

because the read returned 16.

If you only specify the template, unpack uses the default variable $_ as the value to unpack.

You can use do to read and return the value you have just read:

$val = unpack 'H32', do { read FILNAM, $_, 16; $_ };

But you should check what read returned to handle possible errors.

Also, calling a file handle FILNAM, which rather seems to be an abbreviation of "file name", is misleading.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found