
package Text::Wigwam::Stash::Base::Magic;

# This is the base Magic class which holds pseudo-methods common to all
# data types, as well as managing the DEFAULT behaviors.

use strict;
use warnings;
no warnings q/uninitialized/;

our $VERSION = '0.01';

use Scalar::Util qw( reftype blessed refaddr );

sub AUTOLOAD {
    return if our $AUTOLOAD =~ /::DESTROY$/;
    my( $class, $meth ) = $AUTOLOAD =~ /(.*)::(.*)$/;
    my (undef, $val, $key, $ref) = @_;
    return $class->can('DEFAULT')
           ? $class->DEFAULT($val, $key, $ref)
           : $val
    ;
}

sub Defined   { 1 }
sub Ref       { ref $_[1] }
sub Reftype   { reftype $_[1] }
sub Reference { $_[1] }

#    This refinfo breaks down when overload is used on a blessed reference.
#    sub Refinfo { ref $_[1] ? "$_[1]" : '' }
sub Refinfo   {
    return '' unless ref $_[1];
    my $addr = refaddr $_[1];
    $addr =~ s/([0-9]+)/sprintf("%#x", $1)/ge;
    blessed $_[1] ?  join( "" => ref $_[1], '=', reftype $_[1], '(', $addr, ')' )
                  :  join( "" => ref $_[1], '=', '(', $addr, ')' );
}
sub Refaddr   { refaddr $_[1] }
sub Refclass  { blessed $_[1] ? 'BLESSED' : reftype $_[1] || 'STRING' }
sub Refname   { reftype $_[1] || 'STRING' }
sub Blessed   { blessed $_[1] }
sub Undefine  { ${$_[3]} = undef if $_[3]; return }
sub Size      { 1 }
sub Quiet     { '' }

1;

