#!/usr/bin/perl -w
use strict;
my $password= "Not really";
hide( $password, "none of your business" );
print "password=($password), hide=(",hide($password),")\n";
$password= "No!";
print "password=($password), hide=(",hide($password),")\n";
# Prints:
#password=(Not really), hide=(none of your business)
#password=(No!), hide=(none of your business)
But the rest of the code (that uses pack and unpack)
is the spoiler so I'll obscure it:
sub hide {
my( $new )= 1<@_ ? pop : undef;
my $secret;
my( $p2, $rc, $f )=
unpack "LLL", unpack "P12", pack "L", \$_[0];
if( 5 == unpack "C", pack "V", $f ) {
my( $pv, $cur, $siz, $iv )=
unpack "L4", unpack "P16", pack "L", $p2;
$secret= unpack "P$iv", pack "L", $pv-$iv;
}
if( defined $new ) {
$_[0]= $new . $_[0];
substr( $_[0], 0, length($new) )= "";
}
return $secret;
}
| |
This allows you to store secret messages in scalars where no Perl code can find them (other than using pack/unpack or resorting to, for example, C code).
(updated)
-
tye
(but my friends call me "Tye") | [reply] [d/l] [select] |
As discussed in
this node
you can use it to implement fixed-width parsing of
data.
Also, I would imagine it would be good for making translation
between host and network byte order on little Endian
architectures.
| [reply] |
As I said in one of my previous nodes, if you do some sorting this article definitly shows a good use for pack...
"Only Bad Coders Badly Code In Perl" (OBC2IP)
| [reply] |