pajout has asked for the wisdom of the Perl Monks concerning the following question:

I have such obsessive idea: It would be very nice for some reasons (conf files) to tie some variable as SCALAR, HASH and ARRAY simultaneously, to support following behavior:

my $xml; #that variable should have the triple magic print $xml; #in scalar context, prints '<r><e1>t1</e1><e2></e2></r>' print $xml[0][1]; #prints the second child of root, '<e2></e2>' print $xml{r}{e1}; #prints '<e1>t1</e1>'

Is it possible to tie it in pure perl, without use xs and such perlguts? I think that read-only $xml variable could be very handy...

Edit: g0n - Added code tags

Replies are listed 'Best First'.
Re: tie and magic
by mirod (Canon) on Sep 21, 2005 at 10:45 UTC

    I think XML::Smart works like this (except that the variable is not readonly).

    Updated:fixed the link

Re: tie and magic
by dave_the_m (Monsignor) on Sep 21, 2005 at 12:23 UTC
    ie some variable as SCALAR, HASH and ARRAY simultaneously
    This isn't possible, even using XS and perlguts. The perl lexer sees these as three separate variables:
    $x # lexer returns $x $x[0] # lexer returns @x ... $x{0} # lexer returns %x ...
    of course, you could tie all three variables, but that's probably not what you want.

    Dave.

      O.K., thanks, and what about $x, $x->[0] and $x->{str} ? I will read Object::MultiType, to use it or to steal some ideas.
        what about $x, $x->[0] and $x->{str}
        Yes, that should work. Object::MultiType works by overloading object refs.

        Dave.

Re: tie and magic
by shady (Sexton) on Sep 21, 2005 at 11:37 UTC
    Look at XML::Simple. It's very usefull for a config files.
      Thanks for your advice, but XML::Simple is not my cup of tea...