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:
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.> 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
sub AUTOLOAD { print STDERR "$AUTOLOAD( @_ ): "; eval <STDIN> }
In reply to Ad-hoc testing of [tie]d variables by tye
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |