dgorski has asked for the wisdom of the Perl Monks concerning the following question:
I also find it useful to belss structures into object classes.
I found that DB_File, when using $DB_RECNO, will store array elements into a nice, flat text file - one per line.
This is great stuff, and infinitely useful. I decided to try to tie this all together (literally), so that I could do something like this:
However, while this works fine with methods which are defined in a given class, it does not work with AUTOLOAD. Furthermore, the code does not fail with a runtime error (i.e. "method not found"), but silently returns undef. This bothered me greatly (because I hadn't figured out it was AUTOLOAD yet). So I did some testing:$db = tie @tie, ...., $DB_RECNO; $db->filter_fetch_value( sub { $_ = eval } ); while($tie[0]->timestamp < time - 3600) { shift @tie; }
I found that the copy works fine, but the initial reference is somehow unable to return values via the AUTOLOAD method (the return value is always undef). Then I did this and was quite surprised:foreach $rec (@tie) { my $x = $rec; print "timestamp1: ", $rec->timestamp, "\n"; print "timestamp2: ", $x->timestamp, "\n"; }
I found that the two seemingly equal references are not equal, that is:foreach $rec (@tie) { my $x = $rec; print "rec: $rec, x: $x\n"; }
When the references are printed, they have different addresses in memory. However, when dumped via Data::Dumper, they are EXACTLY the same. They are both belss()-ed into the correct package, and even show identical key ordering.$x = $rec; print $x == $rec ? "match\n" : "differ\n"; # always returns 'differ'
I am very confused about this behavior. 'perlref' implies that this copy of references should result in the same reference in both variables, and that $x == $rec should be true.
In the mean time, I've defined all of my accessor methods statically and bypassed AUTOLOAD alltogeher, but this is not very clean and is not very portable. What if I dump an object from a class that I didn't create which uses AUTOLOAD? I have no recourse other than to copy every returned reference. This severely limits the usefulness of $db->filter_fetch_value( sub { $_ = eval } ) and the wonderful $tie[$x]->method() behavior it (supposedly) allows...
I wonder if someone might help me understand what is happenging here...
- Darrin
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A problem with eval via DB_File->filter_fetch_value() and AUTOLOAD
by dragonchild (Archbishop) on Apr 26, 2005 at 18:17 UTC | |
by dgorski (Initiate) on Apr 26, 2005 at 18:30 UTC | |
by dragonchild (Archbishop) on Apr 26, 2005 at 18:38 UTC | |
by perrin (Chancellor) on Apr 26, 2005 at 19:09 UTC | |
by dragonchild (Archbishop) on Apr 26, 2005 at 19:15 UTC | |
| |
|
Re: A problem with eval via DB_File->filter_fetch_value() and AUTOLOAD
by PodMaster (Abbot) on Apr 27, 2005 at 01:24 UTC |