Overloading in perl 
Author Message
 Overloading in perl

Hi,

 I am trying to overload Hash access in perl. Here's the code
i am using for it. Perl goes into infinite loop. Can somebody
let me know what is wrong.

Thanks a lot,
sreekanth

package ol;

use overload  '""' => \&stringify,
              '%{}'=> \&gethash1;

sub new {
    my $this = shift;;
    my $class = ref($this) || $this;

    bless $self, $class;
    return $self;

Quote:
}

sub gethash1 {
    my $self = $_[0];
    return %{$self};
Quote:
}

sub stringify {
    my $self = shift;
    return ref($self);

Quote:
}

1;

Driver program

use ol;
$a = new ol (1,a,2,b);
print "Access Hash ", $a->{1} ,"\n";



Mon, 08 Sep 2003 19:35:02 GMT  
 Overloading in perl
[A complimentary Cc of this posting was sent to Sreekanth


Quote:
> Hi,

>  I am trying to overload Hash access in perl. Here's the code
> i am using for it. Perl goes into infinite loop. Can somebody
> let me know what is wrong.
> use overload  '""' => \&stringify,
>               '%{}'=> \&gethash1;
> sub new {
...

>     bless $self, $class;
>     return $self;
> }
> sub gethash1 {
>     my $self = $_[0];
>     return %{$self};
> }

I see two bugs.  First of all, gethash1() should return a reference,
not a hash.  Second, you get an infinite loop.  [Because you access
%{} of $self inside a handler for %{}.]

You need an extra indirection to make $self into a scalar reference.


Ilya



Tue, 09 Sep 2003 01:48:38 GMT  
 Overloading in perl

Quote:

>  I am trying to overload Hash access in perl. Here's the code
> i am using for it. Perl goes into infinite loop. Can somebody
> let me know what is wrong.

You have made two totally independant mistakes.

Firstly the trivial one.  The method that implements the %{}
overloading must return a _reference_ to a hash.  The way to return a
reference to a hash is:

return \%some_hash;

Not:

return %some_hash;

Secondly the deeper mistake.  An object in Perl must be implemented as
a blessed thing of one of the Perl built-in datatypes: SCALAR, HASH,
ARRAY, GLOB or even if you are truely perverse CODE.

Overloading the dereferencing operators allows you to specify what
should happen if you try to access the object as if it were thing of a
given built-in type.

If you have an object that is a blessed hash and you overload %{} then
you will never be able to access the actual underlying hash because
every attempt you make to do so will be redirected through the
overload method (hense the infinite recursion you observed).

In practice this means that if you want to have an object that
overloads one or more of the dereferencing operators then the
underlying thing chosen to implement the object must be of a different
builtin type from the dereferencing operators.

See examples in the "overload" manpage.

If you want to have an object that overloads all 5 of the
dereferencing operators you are sick^H^H^H^H going to need to do some
fancy footwork with temporary unblessing or ignore the data stored in
the underlying thing.

Quote:
> package ol;

> use overload  '""' => \&stringify,
>               '%{}'=> \&gethash1;

> sub new {
>     my $this = shift;;
>     my $class = ref($this) || $this;

>     bless $self, $class;
>     return $self;
> }

Make that:

# Object of class ol is (reference to) a blessed scalar containing hashref

Quote:
> sub gethash1 {
>     my $self = $_[0];
>     return %{$self};

Make that

     return $$self;

Or if you prefer:

     return \%{${$self}};

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Tue, 09 Sep 2003 02:00:28 GMT  
 Overloading in perl

Quote:

> a blessed thing of one of the Perl built-in datatypes:
> ... or even if you are truely perverse CODE.

That's not perverse!  In fact, it's one of the nicest, cleanest
ways to implement objects in perl.  I do it that way a LOT.
Also, look at Abigail's OO::Closures module for an example.

--
John Porter

Useless use of time in void context.



Tue, 09 Sep 2003 02:17:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Overloading in perl

2. Overload of perl TK ???

3. HELP ME

4. overload method package and unload overloads

5. use overload or %OVERLOAD ???

6. Overloading perl object methods, Howto ?

7. Overloading in OO perl

8. PERL OO: Function overloading

9. Can standard Perl functions be overloaded ?

10. PROPOSAL: Overloaded subs in perl

11. Overloading perl object methods, Howto ?

12. Good do it yourself Pascal book

 

 
Powered by phpBB® Forum Software