Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Tie-ing hashes clobbers data

by demerphq (Chancellor)
on Apr 08, 2002 at 15:06 UTC ( [id://157454]=note: print w/replies, xml ) Need Help??


in reply to Tie-ing hashes clobbers data

Couple of points. A cyclic or self-referential structure will break your Tie. Second in the _write sub you are making a _copy_ of the hash. This may or may not be what you want to do.

But I think the heart of your problem is here:

print FH Data::Dumper->Dump([\%data], ['*data']);
and here
my $ret = do $file
This line sets the $ret value to be equal to the glob *data which was just overwritten by you. (ok, so you ignore the $ret and use the global var \%data, but that was just overwritten so... Same dif.) So its the copy semantics in the _write and _read subs that are causing you trouble. Change them to use scalar refs and not globs and the problem will go away. Oh and lose the shallow copy in _read its misleading and unnecessary.

UPDATE

A question: Why are you mixing proper method calls and procedural calls? _write() is a method, but you call it as a procedure. This means it wont be overidable in a subclass...

sub DESTROY { _write ($_[0]) } #not overidable!

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

Replies are listed 'Best First'.
Re: Re: Tie-ing hashes clobbers data
by Anonymous Monk on Apr 08, 2002 at 22:36 UTC
    _write() is a method, but you call it as a procedure. This means it wont be overidable in a subclass...

    ...and that's the point. A leading underscore indicates a private method.
      ...and that's the point. A leading underscore indicates a private method

      Er, no.

      The point is that its not a method. Its not a private method and its not a public method because it is not a method at all.

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

        Method, procedure, function. It's all the same thing. The thing that differes is how you call them. I'd say that
        sub Foo::new { bless {}, $_[0]; }
        is still a method even if I call it with Foo::new($class). Or what about this:
        my $bar = $foo->can('bar'); $bar->($foo); # Is there any difference between this &bar $foo->$bar; # and this &bar?
        There are times when you wish to specify which class's method to use. Here you can call your method as an ordinary subroutine call, being sure to pass the requisite first argument explicitly /.../ Unlike method calls, function calls don't consider inheritance. (perlobj)

        And this is exactly what we want... or at least what Dave05 intended. He even wrote that the subs are private. &_write takes an object as the first argument, so it's effectively the same thing as a method, hence you could say "private method", and private methods shouldn't be invoked as methods. Invoking private methods as methods will lead to Bad Things.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://157454]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found