The Magic Of Tie

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite & 1 Group

    The Magic Of Tie - Presentation Transcript

    1. The Magic of tie brian d foy brian@stonehenge.com Stonehenge Consulting Ser vices May 26, 2006 Dallas-Ft. Worth Perl Mongers
    2. Fun with variables Tied variables are really hidden objects The objects have a hidden interface to make them act like normal variables Behind that interface, we do we what
    3. Normal variables $value = 5; $other_value = $value; @array = qw( a b c ); $array[3] = ‘d’; $last = $#array shift @array; %hash = map { $_, 1 } @array; $hash{a} = 2; delete $hash{b}; my @keys = keys %hash;
    4. Do what we want The tie interface let’s us decide what to do in each case We can store values any way we like tie my $scalar, ‘Tie::Foo’, @args; tie my @array, ‘Tie::Bar’, @args; tie my %hash, ‘Tie::Quux’, @args; tie my $fh, ‘Tie::Baz’, @args;
    5. Do what the user knows Scalars, arrays, hashes Common, “Learning Perl” level syntax Hide all of the complexity
    6. DBM::Deep Persistent hashes use DBM::Deep; my $db = DBM::Deep->new( \"foo.db\" ); $db->{key} = 'value'; print $db->{key}; # true multi-level support $db->{my_complex} = [ 'hello', { perl => 'rules' }, 42, 99 ];
    7. Tie:: modules Tie::Scalar Tie::Array Tie::Hash Tie::Handle Tie::File Tie::Cycle Tie::Toggle Tie::FlipFlop Tie::Handle::CSV Tie::SortHash IO::Scalar IO::String
    8. Two variables The normal looking variable and it’s secret life as an object my $object = tie my $scalar, ‘Tie::ClassName’, @args;
    9. We do this... Perl does this... $scalar $object->FETCH $scalar = 5 $object->STORE(5)
    10. Scalars my $object = tie my $scalar, Tie::Foo- ‘Tie::Foo’, @args; >TIESCALAR( @args ) $n = $scalar; $n = $object->FETCH; $scalar = 5; $object->STORE(5); tied($scalar)->foo(5); $object->foo(5); and a few others: DESTROY, UNTIE
    11. Tie::Cycle use Tie::Cycle; tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF 000000 FFFF00 ) ]; print $cycle; # FFFFFF print $cycle; # 000000 print $cycle; # FFFF00 print $cycle; # FFFFFF back to the beginning (tied $cycle)->reset; # back to the beginning
    12. IO::Scalar use 5.005; use IO::Scalar; $data = \"My message:\\n\"; ### Open a handle on a string $SH = IO::Scalar->new( \\$data ); $SH->print(\"Hello\"); $SH->print(\", world!\\nBye now!\\n\"); print \"The string is now: \", $data, \"\\n\"; == OR == open my $fh, “>”, \\$variable;
    13. Tie::Scalar Base class for tied scalars Override for the parts you need
    14. Arrays my $object = tie my @array, Tie::Foo- ‘Tie::Foo’, @args; >TIEARRAY( @args ) $n = $array[$i]; $n = $object->FETCH($i); $array[$i] = 5; $object->STORE($i, 5); $n = @array $object->FETCHSIZE; $#array = $n; $object->STORESIZE($n+1); $#array += $n; $object->EXTEND($n); @array = (); $object->CLEAR; and a few others: DELETE, EXISTS, PUSH, POP, SHIFT, UNSHIFT, SPLICE, DESTROY, UNTIE
    15. Tie::Array Base class for tied arrays Override the parts you need
    16. Hashes my $obj = tie my %hash, Tie::Foo- ‘Tie::Foo’, @args; >TIEHASH( @args ) $n = $hash{ $key }; $n = $obj->FETCH($key); $hash{ $key } = 5; $obj->STORE($key, 5); delete $hash{ $key }; $obj->DELETE( $key ) exists $hash{ $key }; $obj->EXISTS( $key ) %hash = (); $obj->CLEAR @keys = ( my @keys = keys %hash; $obj->FIRSTKEY, $obj->NEXTKEY ... ) and a few others: SCALAR, UNTIE, DESTROY
    17. Tie::Hash Base class for tied hashes Override the parts you need
    18. Filehandles my $object = tie my $fh, Tie::Foo- ‘Tie::Foo’, @args; >TIEHANDLE( @args ) print $fh @stuff; $object->PRINT(@stuff); my $line = <$fh> $object->READLINE; close $fh; $object->CLOSE; and a few others: WRITE, PRINTF, READ, DESTROY, UNTIE
    19. Tie::Handle Base class for tied handles Override the parts you need

    + _brian d _foy_brian d _foy, 2 years ago

    custom

    2422 views, 1 favs, 0 embeds more stats

    May 2006 presentation to the Dallas/Ft. Worth Perl more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2422
      • 2422 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 0
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Tags

    Groups / Events