Skip to content

Commit

Permalink
- Added support for %s(...) and %S(...) to specifiy literal Perl …
Browse files Browse the repository at this point in the history
…module "symbols".

Example:
	require "File::Spec";
	say %s(File::Spec).catfile("a","b");	           # prints: "a/b"

	require "File::Spec::Functions";
	say %S(File::Spec::Functions).catfile("a", "b");   # =//=

Notes:
	%s(...) is used to specify object-oriented Perl modules.
	%S(...) is used to specify functional Perl modules.

More notes:
	%s(...) is equivalent with "...".to_caller
	%S(...) is equivalent with "...".to_fcaller
  • Loading branch information
trizen committed Jul 23, 2015
1 parent 3b61c89 commit f527e18
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lib/Sidef/Convert/Convert.pm
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ package Sidef::Convert::Convert {
Sidef::Types::Number::Number->new($_[0]->get_value);
}

*to_num = \&to_n;
*to_num = \&to_n;
*to_number = \&to_n;

sub to_float {
Expand Down Expand Up @@ -96,11 +96,11 @@ package Sidef::Convert::Convert {
}

sub to_caller {
Sidef::Module::OO->__NEW__(module => $_[0]->get_value);
Sidef::Module::OO->__NEW__($_[0]->get_value);
}

sub to_fcaller {
Sidef::Module::Func->__NEW__(module => $_[0]->get_value);
Sidef::Module::Func->__NEW__($_[0]->get_value);
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Module/Func.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package Sidef::Module::Func {
our $AUTOLOAD;

sub __NEW__ {
my (undef, %opt) = @_;
bless \%opt, __PACKAGE__;
my (undef, $module) = @_;
bless {module => $module}, __PACKAGE__;
}

sub DESTROY {
Expand Down
8 changes: 4 additions & 4 deletions lib/Sidef/Module/OO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package Sidef::Module::OO {
our $AUTOLOAD;

sub __NEW__ {
my (undef, %opt) = @_;
bless \%opt, __PACKAGE__;
my (undef, $module) = @_;
bless {module => $module}, __PACKAGE__;
}

sub DESTROY {
Expand All @@ -17,7 +17,7 @@ package Sidef::Module::OO {
my ($method) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);

if ($method eq '') {
return Sidef::Module::Func->__NEW__(module => $self->{module});
return Sidef::Module::Func->__NEW__($self->{module});
}

my @args = (
Expand Down Expand Up @@ -45,7 +45,7 @@ package Sidef::Module::OO {

my $result = $results[0];
if (ref($result) && eval { $result->can('can') }) {
return $self->__NEW__(module => ($result));
return $self->__NEW__($result);
}

Sidef::Perl::Perl->to_sidef($result);
Expand Down
22 changes: 14 additions & 8 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ package Sidef::Parser {
# Chars
| %c\b. (?{ [qw(0 to_chars Sidef::Types::Char::Chars)] })
| %C\b. (?{ [qw(1 to_chars Sidef::Types::Char::Chars)] })
# Symbols
| %s\b. (?{ [qw(0 __NEW__ Sidef::Module::OO)] })
| %S\b. (?{ [qw(0 __NEW__ Sidef::Module::Func)] })
)
}xs,
built_in_classes => {
Expand Down Expand Up @@ -775,20 +779,22 @@ package Sidef::Parser {

# Special case for array-like objects (bytes and chars)
my @array_like;
if ($method ne 'new') {
if ($method ne 'new' and $method ne '__NEW__') {
@array_like = ($package, $method);
$package = 'Sidef::Types::String::String';
$method = 'new';
}

my $obj = $double_quoted
? do {
state $str = Sidef::Types::String::String->new; # load the string module
Sidef::Types::String::String::apply_escapes($package->$method($string), $self);
}
: $package->$method($string =~ s{\\\\}{\\}gr);
my $obj = (
$double_quoted
? do {
state $str = Sidef::Types::String::String->new; # load the string module
Sidef::Types::String::String::apply_escapes($package->$method($string), $self);
}
: $package->$method($string =~ s{\\\\}{\\}gr)
);

# Special case for backticks (add method '`')
# Special case for backticks (add method 'exec')
if ($package eq 'Sidef::Types::Glob::Backtick') {
my $struct =
$double_quoted && ref($obj) eq 'HASH'
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -888,12 +888,12 @@ package Sidef::Types::String::String {

sub require {
my ($self) = @_;
Sidef::Module::OO->__NEW__(module => $self->_require);
Sidef::Module::OO->__NEW__($self->_require);
}

sub frequire {
my ($self) = @_;
Sidef::Module::Func->__NEW__(module => $self->_require);
Sidef::Module::Func->__NEW__($self->_require);
}

sub unescape {
Expand Down

0 comments on commit f527e18

Please sign in to comment.