RE: PerlHack - Adding new flag into a SV
Title: RE: PerlHack - Adding new flag into a SV From: Orton, Yves [EMAIL PROTECTED] I thought you are just automagicaly weaken()ing the referenced stored in the tied hash??? Something like package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } 'And that\'s it!'; Am I missing something? Itll segfault under many circumstances. yves Then you should report an error in Scalar::Util. Do you have an example script that segfaults? No but I can describe one that would. Imagine a binary tree. We store each node using weakrefs in the structure you describe. Now I search and find an internal node of the tree. The root of the tree falls out of scope, essentially stripping the tree of all of its relatives. Now the hash contains a zillion refs to non existant objects. Access one of those and POOF! Maybe you were thinking I meant a problem with your code. I meant a problem with the entire approach. IMO weak refs are not the greatest solution to the problems that they are usually used for. Often there are much better approaches that don't involve risking segfaults. Anyway, :-) Yves
Re: PerlHack - Adding new flag into a SV
On 27 Apr 2004, at 13:01, Orton, Yves wrote: [snip] No but I can describe one that would. Imagine a binary tree. We store each node using weakrefs in the structure you describe. Now I search and find an internal node of the tree. The root of the tree falls out of scope, essentially stripping the tree of all of its relatives. Now the hash contains a zillion refs to non existant objects. Access one of those and POOF! [snip] Erm. I don't think so. If the thing a weakref points to is collected you get a pointer to undef. To quote from the docs: REF will be turned into a weak reference. This means that it not hold a reference count on the object it references. Also the reference count on that object reaches zero, REF will be set undef. If you're getting something that can cause a segfault then something's wrong somewhere surely. Adrian
Re: PerlHack - Adding new flag into a SV
No, weakrefs cast backref magic onto their target to avoid this problem. When the reference count drops to zero the magic ensures that the weakref is set to undef. See Perl_sv_rvweaken() in sv.c for more. Sam. Orton, Yves wrote: From: Orton, Yves [EMAIL PROTECTED] I thought you are just automagicaly weaken()ing the referenced stored in the tied hash??? Something like package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } 'And that\'s it!'; Am I missing something? Itll segfault under many circumstances. yves Then you should report an error in Scalar::Util. Do you have an example script that segfaults? Maybe you were thinking I meant a problem with your code. I meant a problem with the entire approach. IMO weak refs are not the greatest solution to the problems that they are usually used for. Often there are much better approaches that don't involve risking segfaults. Anyway, :-) Yves -- Sam Vilain, sam /\T vilain |T net, PGP key ID: 0x05B52F13 (include my PGP key ID in personal replies to avoid spam filtering)
Re: PerlHack - Adding new flag into a SV
[EMAIL PROTECTED] (Graciliano M. P.) writes: I'm working on a module that need to add a new flag to a SV that is stored by it. Urgh. Don't do it! Actually I just need to find a way to mark that SV by this module, so, will be possible to identify this SVs from the others. Piers Cawley worked out a system for using magic to annotate an SV, for his Pixie persistence code. See http://groups.google.com/groups?threadm=843ct6j4c1.fsf%40despairon.bofh.org.uk -- Writing software is more fun than working.
Re: PerlHack - Adding new flag into a SV
From: Graciliano M. P. [EMAIL PROTECTED] *Why do that? Is for the module Hash::NoRef, where I need to store a value/object without actually make a reference to it. I previously tried to store a reference value and decrement its REFCNT, but this will create a lot of problems, including CORE dump of the interpreter, specially if we try to access this value after the destruction of it, since is impossible to know if a reference was destroied when the REFCNT is artificialy changed, and also we can access a recycled SV. As a new approach I will store the type and number of the SV, and mark that SV to guarantee that it wasn't destroied. After this I can access the SV getting it directly from the list of SVs in use. ? I thought you are just automagicaly weaken()ing the referenced stored in the tied hash??? Something like package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } 'And that\'s it!'; Am I missing something? Jenda = [EMAIL PROTECTED] === http://Jenda.Krynicky.cz = When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } package FakeObj; sub new { bless {}, 'FakeObj'; } sub DESTROY { print DESTROY called\n; } package main; my %objs; tie %objs, 'Hash::NoRef'; { my $obj = new FakeObj; $objs{first} = $obj; } print End of program\n;
RE: PerlHack - Adding new flag into a SV
Title: RE: PerlHack - Adding new flag into a SV I thought you are just automagicaly weaken()ing the referenced stored in the tied hash??? Something like package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } 'And that\'s it!'; Am I missing something? Itll segfault under many circumstances. yves
RE: PerlHack - Adding new flag into a SV
From: Orton, Yves [EMAIL PROTECTED] I thought you are just automagicaly weaken()ing the referenced stored in the tied hash??? Something like package Hash::NoRef; use Scalar::Util qw(weaken); require Tie::Hash; @ISA = (Tie::StdHash); sub STORE { $_[0]{$_[1]} = $_[2]; weaken($_[0]{$_[1]}) if (ref $_[2]); } 'And that\'s it!'; Am I missing something? Itll segfault under many circumstances. yves Then you should report an error in Scalar::Util. Do you have an example script that segfaults? Jenda = [EMAIL PROTECTED] === http://Jenda.Krynicky.cz = When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery