Created
January 19, 2012 16:59
-
-
Save jberger/1641172 to your computer and use it in GitHub Desktop.
TiePDL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
package TiePDL; | |
our @ISA = 'PDL'; | |
{ | |
no strict 'refs'; | |
*{'PDL::tie'} = sub { | |
my $self = shift; | |
bless $self, 'TiePDL'; | |
} | |
} | |
use overload | |
'%{}' => sub { | |
my $pdl = shift; | |
tie my %hash, 'TiePDL::Class', $pdl; | |
return \%hash; | |
}, | |
'&{}' => sub { | |
my $self = shift; | |
return sub { $self->slice(@_) }; | |
}; | |
package TiePDL::Class; | |
sub TIEHASH { | |
my ($class, $pdl) = @_; | |
my $self = { | |
pdl => $pdl, | |
}; | |
bless $self, $class; | |
return $self; | |
} | |
sub FETCH { | |
my ($self, $subscript) = @_; | |
return $self->{pdl}->slice($subscript); | |
} | |
sub STORE { | |
my ($self, $subscript, $value) = @_; | |
$self->FETCH($subscript) = $value; | |
return $value; | |
} | |
1; | |
__END__ | |
use example: | |
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use PDL; | |
use TiePDL; | |
my $pdl = xvals(7); | |
$pdl->tie; | |
print $pdl->{'1:3'}; | |
print $pdl->('1:3'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment