Here is the most creative use of pack I've come up with:
#!/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")
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.