Sometimes I have a question about how something works in a tied variable. This one-liner lets me use "perl -de 0" to interactively test how tied variables behave.

For example, when I was curious about how keys and each interact with a tied hash, I did this:

> perl -de 0 # ... DB<1> sub AUTOLOAD { print STDERR "$AUTOLOAD( @_ ): "; eval <STDIN> +} DB<2> tie %h,'main' main::TIEHASH( main ): bless [] DB<3> x tied %h 0 main=ARRAY(0x1c81a88) empty array DB<4> print 0+keys %h main::FIRSTKEY( main=ARRAY(0x1c81a88) ): 'hello' main::NEXTKEY( main=ARRAY(0x1c81a88) hello ): 'goodbye' main::NEXTKEY( main=ARRAY(0x1c81a88) goodbye ): 2 DB<5> while( my($k,$v)= each %h ) { warn "$k=>$v\n"; keys %h if !$v +} main::FIRSTKEY( main=ARRAY(0x1c81a88) ): 'hello' main::FETCH( main=ARRAY(0x1c81a88) hello ): 'hi' hello=>hi main::NEXTKEY( main=ARRAY(0x1c81a88) hello ): 'goodbye' main::FETCH( main=ARRAY(0x1c81a88) goodbye ): 'bye' goodbye=>bye main::NEXTKEY( main=ARRAY(0x1c81a88) goodbye ): 'reset' main::FETCH( main=ARRAY(0x1c81a88) reset ): 0 reset=>0 main::FIRSTKEY( main=ARRAY(0x1c81a88) ): DB<6> q main::DESTROY( main=ARRAY(0x1c81a88) ): > exit
Note how keys in a void context causes the each iterator to start over at FIRSTKEY() [without having to bother the object] while keys in a scalar context has to ask the object for every key just so that it can count them.

sub AUTOLOAD { print STDERR "$AUTOLOAD( @_ ): "; eval <STDIN> }

In reply to Ad-hoc testing of [tie]d variables by tye

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.