in reply to Autovivifying XS routine

You can do this fairly easily using overload:

#! perl -slw use strict; package Gimme; use Scalar::Util qw[ refaddr ]; my %hashes; my %arrays; use overload '%{}' => sub { return $hashes{ refaddr( $_[ 0 ] ) }; }, '@{}' => sub { return $arrays{ refaddr( $_[ 0 ] ) }; }, '""' => sub { return overload::StrVal( $_[ 0 ] ); }, '0+' => sub { return overload::StrVal( $_[ 0 ] ); }, ; sub new { my( $class, $dummy ) = shift; my $self = \$dummy; $hashes{ 0+$self } = {}; $arrays{ 0+$self } = []; return bless $self, $class; } package main; my $gimme = new Gimme; @{ $gimme } = 1 .. 10; print @{ $gimme }; %{ $gimme } = 'A' .. 'Z'; print %{ $gimme }; print $gimme->{ fred } = 'bill'; print $gimme->[ 123 ] = 456; __END__ C:\test>junk9 12345678910 STABOPWXKLYZEFQRMNCDIJGHUV bill 456

And I've seen overloading done using XS, though I've never done it myself, so in theory at least it should be possible to do it without having to chase opcode trees.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Autovivifying XS routine
by Anno (Deacon) on Jun 01, 2007 at 15:27 UTC
    Sorry, I mistyped an example (now corrected) that made it look like I wanted the behavior of a variable. That would indeed suggest an overload solution. I want it to be a function, with arguments in the actual case.

    ikegami has pointed out the Want module. That should show me the way.

    Anno

      Okay. I did see the function call, but assumed that the function would return an overloaded blessed reference (as new() in my example does), and then overloading would then come into play to determine the appropraite context.

      It will be interesting to see if Want can detect/supply the calling context more quickly than the overloading. The latter is notoriously slow, but when I played with Want many moons ago, it was also pretty slugish. You might be able to avoid some of that if you c&p the logic directly into your own XS code I suppose?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        It will be interesting to see if Want can detect/supply the calling context more quickly than the overloading. The latter is notoriously slow...

        The way I understand things, overload stuff is called from the code that interprets the de-referencing opcodes, individually in all five(?) cases. If it's slow, that's probably not because it has to determine the kind of reference needed, it should know. Not that it cared anyway.

        ...c&p the logic directly into your own XS code I suppose?

        That is the plan.

        Anno