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")

In reply to (tye)Re: Creative use of pack/unpack by tye
in thread Creative use of pack/unpack by Eureka_sg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.