Skip to content

Commit

Permalink
- Simplified the code inside the OO and Func module callers.
Browse files Browse the repository at this point in the history
In addition, it now allows passing variable references to Perl functions or methods.
  • Loading branch information
trizen committed Dec 11, 2015
1 parent ef52c90 commit 0b786ec
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 47 deletions.
44 changes: 3 additions & 41 deletions lib/Sidef/Module/Func.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package Sidef::Module::Func {

use 5.014;
our $AUTOLOAD;

sub __NEW__ {
Expand All @@ -12,43 +11,6 @@ package Sidef::Module::Func {
return;
}

sub __LOCATE__ {
my ($self, $name) = @_;

no strict 'refs';
my $mod_space = \%{$self->{module} . '::'};

if (exists $mod_space->{$name}) {
return $self->{module} . '::' . $name;
}

return;
}

sub _var {
my ($self, $name) = @_;

if (defined(my $type = $self->__LOCATE__($name))) {
no strict 'refs';
return ${$type};
}

warn qq{[WARN] Variable '$name' is not exported by module: "$self->{module}"!\n};
return;
}

sub _arr {
my ($self, $name) = @_;

if (defined(my $type = $self->__LOCATE__($name))) {
no strict 'refs';
return Sidef::Types::Array::Array->new(@{$type});
}

warn qq{[WARN] Array '$name' is not exported by module: "$self->{module}"!\n};
return;
}

sub AUTOLOAD {
my ($self, @arg) = @_;

Expand All @@ -59,8 +21,8 @@ package Sidef::Module::Func {
? (
map {
local $Sidef::Types::Number::Number::GET_PERL_VALUE = 1;
index(ref($_), 'Sidef::') == 0 ? $_->get_value
: ref($_) eq 'REF' ? ${$_}
index(ref($_), 'Sidef::') == 0
? $_->get_value
: $_
} @arg
)
Expand All @@ -69,7 +31,7 @@ package Sidef::Module::Func {

my @results = do {
local *UNIVERSAL::AUTOLOAD;
(\&{$self->{module} . '::' . $func})->(@args);
($self->{module} . '::' . $func)->(@args);
};

if (@results > 1) {
Expand Down
6 changes: 1 addition & 5 deletions lib/Sidef/Module/OO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ package Sidef::Module::OO {

sub AUTOLOAD {
my ($self, @arg) = @_;
my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);

if ($method eq '') {
return Sidef::Module::Func->__NEW__($self->{module});
}
my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);

my @args = (
@arg
Expand All @@ -27,7 +24,6 @@ package Sidef::Module::OO {
local $Sidef::Types::Number::Number::GET_PERL_VALUE = 1;
ref($_) eq __PACKAGE__ ? $_->{module}
: index(ref($_), 'Sidef::') == 0 ? $_->get_value
: ref($_) eq 'REF' ? ${$_}
: $_
} @arg
)
Expand Down
2 changes: 1 addition & 1 deletion scripts/RosettaCode/sha-1.sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
## http://rosettacode.org/wiki/SHA-1
#

var sha = require('Digest::SHA').();
var sha = frequire('Digest::SHA');
say sha.sha1_hex('Rosetta Code');

0 comments on commit 0b786ec

Please sign in to comment.