Text-Bidi

 view release on metacpan or  search on metacpan

lib/Text/Bidi/Array.pm  view on Meta::CPAN

# Created: Wed 11 Sep 2013 09:47:56 AM IDT
# Last Changed: Mon 23 Sep 2013 09:20:16 AM IDT

use 5.10.0;
use warnings;
use integer;
use strict;

package Text::Bidi::Array;
# ABSTRACT: Base class for dual life arrays
$Text::Bidi::Array::VERSION = '2.18';

use Carp;

use Tie::Array;
use base qw(Tie::Array);


use overload 
    '${}' => 'as_scalar', '@{}' => 'as_array', '""' => 'data', fallback => 1;



sub new {
    my $class = shift;
    my $self = tie(my @magic, $class, @_);
    $self->{'magic'} = \@magic;
    $self
}


sub TIEARRAY {
    my $class = shift;
    my $data = shift || 0;
    my $self = { @_ };
    bless $self => $class;
    $self->_init($data)
}

sub _init {
    my ($self, $data) = @_;
    if ( ref($data) ) {
        my @data = eval { @$data };
        croak $@ if $@;
        $data = $self->pack(@data);
    }
    $self->{'data'} = $data;
    return $self
}


sub data { $_[0]->{'data'} }

sub as_scalar { \$_[0]->{'data'} }

sub as_array { $_[0]->{'magic'} }

sub CLEAR {
    $_[0]->{'data'} = 0
}

1;

__END__

=pod

=head1 NAME

Text::Bidi::Array - Base class for dual life arrays

=head1 VERSION

version 2.18

=head1 SYNOPSIS

    use Text::Bidi::Array::Byte;
    my $a = new Text::Bidi::Array::Byte "abc";
    say $a->[1]; # says 98
    say $$a; # says abc
    say "$a"; # also says abc

=head1 DESCRIPTION

This is an abstract base class for objects that function as ``dual-life'' 
arrays: When viewed as an array reference, it functions as a usual array of 
numbers. When used as a scalar reference, it returns the same array as a 
packed string. The packing specification depends on the sub-class. These 
classes are used in L<Text::Bidi> to conveniently pass arrays to the 
underlying fribidi library, but could be of independent interest.

=head1 OVERLOADED OPERATORS

An object of this type can be dereferenced either as a scalar or as an array.  
In the first case, it returns the packed (string) representation of the 



( run in 0.665 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )