Perl Cheat Sheet Page 11

ADVERTISEMENT

11. Conversion functions
y
chr EXPR
Returns the character represented by the decimal value
EXPR
.
y
gmtime EXPR
Converts a time as returned by the
function to a 9-element array
time
(0: $ sec, 1: $ min, 2: $ hour, 3: $ mday, 4: $ mon, 5: $ year, 6: $ wday, 7: $ yday,
8: $ isdst) with the time localized for the standard Greenwich time zone.
$ mon has the range 0..11 and $ wday has the range 0..6.
y
hex EXPR
Returns the decimal value of
interpreted as an hex string.
EXPR
y
localtime EXPR
Converts a time as returned by the
time
function to ctime(3) string. In array
context, returns a 9-element array (see
) with the time localized for
gmtime
the local time zone.
y
oct EXPR
Returns the decimal value of
EXPR
interpreted as an octal string. If
EXPR
starts off with 0x , interprets it as a hex string instead.
y
ord EXPR
Returns the ASCII value of the first character of
EXPR
.
vec EXPR, OFFSET, BITS
Treats string
EXPR
as a vector of unsigned integers of
BITS
bits each, and
yields the decimal value of the element at
OFFSET
.
BITS
must be a power
of 2 between 1 and 32. May be assigned to.
12. Structure conversion
pack TEMPLATE, LIST
Packs the values into a binary structure using
.
TEMPLATE
unpack TEMPLATE, EXPR
Unpacks the structure
into an array, using
.
EXPR
TEMPLATE
is a sequence of characters as follows:
TEMPLATE
a / A
ASCII string, null / space padded
b / B
Bit string in ascending / descending order
c / C
Native / unsigned char value
f / d
Single / double float in native format
h / H
Hex string, low / high nybble first.
i / I
Signed / unsigned integer value
l / L
Signed / unsigned long value
n / N
Short / long in network (big endian) byte order
s / S
Signed / unsigned short value
u / p
Uuencoded string / pointer to a string
A pointer to a structure (fixed-length string)
P
v / V
Short / long in VAX (little endian) byte order
w / x
BER compressed integer / null byte
X / @
Backup a byte / null fill until position
Each character may be followed by a decimal number which will be used as a
repeat count, ‘ * ’ specifies all remaining arguments.
If the format is preceded with %
N
,
unpack
returns an
N
-bit checksum instead.
Spaces may be included in the template for readability purposes.
11

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education