You can do the same syntax in Perl if you do Autoload hack.
Create a small package to handle startup:
package Autoloader; use strict; use warnings; our $AUTOLOAD; sub AUTOLOAD { my $self = shift; my ($method) = (split(/::/, $AUTOLOAD))[-1]; die "Object does not contain method '$method'" if not ref $self->{$method} eq 'CODE'; goto &{$self->{$method}}; } 1;
Then your other package or main script will contain a routine that returns the object that Autoload processes when its method tries to call.
sub element { my $elem = shift; my $sub = { in => sub { return if not $_[0];
This gives you the opportunity to look like this:
doTask if element('something')->in(@array);
If you reorganize the closure and its arguments, you can switch the syntax in another way so that it looks a little closer to the autobox style:
doTask if search(@array)->contains('something');
to do this:
sub search { my @arr = @_; my $sub = { contains => sub { my $elem = shift or return; my %hash; @hash{@arr} = (); return (exists $hash{$elem}) ? 1 : (); } }; bless($sub, 'Autoloader'); }
delias Jan 27 '17 at 10:14 2017-01-27 10:14
source share