Hi all

I've got a bit of trouble with perl's tie...

Run the following code width different options ( STORE, CLEAR,FETCH, ...) :
#!/opt/perl5/bin/perl -w use strict; package toy::hash; use strict; sub TIEHASH { my $class = shift; return bless { values => { @_ } }, $class; } sub STORE { my ( $self, $key, $value ) = @_; &::is_H_tied(); $self->{values}{$key} = $value; } sub FETCH { my ( $self, $key ) = @_; &::is_H_tied(); $self->{values}{$key}; } sub KEYS { my ( $self ) = @_; &::is_H_tied(); keys %{ $self->{values} }; } sub FIRSTKEY { my ( $self ) = @_; &::is_H_tied(); each %{ $self->{values} }; } sub NEXTKEY { my ( $self ) = @_; &::is_H_tied(); each %{ $self->{values} }; } sub CLEAR { my ( $self ) = @_; &::is_H_tied(); %{ $self->{values} } = (); } sub EXISTS { my ( $self, $key ) = @_; &::is_H_tied(); exists $self->{values}{$key}; } package main; our $H; sub is_H_tied { my $m = ( caller( 1 ) )[3]; print STDERR "method=$m, tied(\%\$H)=", tied( %$H ), "\n"; } my @k = ( 1..30 ); tie( my %h, 'toy::hash', map { $_, $_ * $_ } @k ); $H = \%h; die "\n" unless( @ARGV ); for( $ARGV[0] ) { /^CLEAR$/o && do { %h = (); tied( %h )->CLEAR(); last; }; /^STORE$/o && do { $h{pi} = 3.1415926; tied( %h )->STORE( qw( pi 3.1415926 ) ); last; }; /^FETCH$/o && do { my $x = $h{pi}; tied( %h )->FETCH( 'pi' ); last; }; /^DELETE$/o && do { delete $h{pi}; tied( %h )->DELETE( 'pi' ); last; }; /^KEYS$/o && do { keys %h; tied( %h )->KEYS(); last; }; /^EXISTS$/o && do { exists $h{pi}; tied( %h )->EXISTS( 'pi' ); last; }; }
In this script, I tie %h to toy::hash, and keep a reference to %h in $H. Then, while working with the object which holds the tie, I check whether %$H is tied or not.

It seems that %h = () and tied( %h )->CLEAR(); are not equivalent, for when the first is invoked, %$H is not tied to toy::hash anymore, while tied( %h )->CLEAR() does not break the tie of %$H.

I've ran another script with a TIEARRAY, and some methods break the tie, while some others don't.

So my question is :
Is this a bug or an undocumented feature of perl's tie ?

philou

Edit: s/pre/code/. larsen

update (broquaint): added <readmore> tag


In reply to Where's my tie ? by philou

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.