3.2.182 unpackunpack
This function does the reverse of
pack
: it
takes a string ( Here's a subroutine that does (some of) substr , only slower: sub substr { my($what, $where, $howmuch) = @_; if ($where < 0) { $where = -$where; return unpack "\@* X$where a$howmuch", $what; } else { return unpack "x$where a$howmuch", $what; } } and then there's: sub signed_ord { unpack "c", shift } Here's a complete uudecode program: #!/usr/bin/perl $_ = <> until ($mode,$file) = /^begin\s*(\d*)\s*(\S*)/; open(OUT,"> $file") if $file ne ""; while (<>) { last if /^end/; next if /[a-z]/; next unless int((((ord() - 32) & 077) + 2) / 3) == int(length() / 4); print OUT unpack "u", $_; } chmod oct $mode, $file;
In addition, you may prefix a field with
undef $/; $checksum = unpack ("%32C*", <>) % 32767; The following efficiently counts the number of set bits in a bit vector: $setbits = unpack "%32b*", $selectmask; Here's a simple MIME decoder: while (<>) { tr#A-Za-z0-9+/##cd; # remove non-base64 chars tr#A-Za-z0-9+/# -_#; # convert to uuencoded format $len = pack("c", 32 + 0.75*length); # compute length byte print unpack("u", $len . $_); # uudecode and print } |
|