Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The @{} lets you do my $large = any(1..10) > 5;my @large = @{$large}; That is why I don't short circuit. The stringification is something i went back and forth on. It might fit better as any(1..10)->string; but I dunno.

BTW I added in the arthimitic. The nice thing about the dynamic way I overload is that it was only a few extra lines of code.

my $mix = all(1..5) + any(1,2); produces a result like my $mix = all(any(2,3),any(3,4), any(4,5),any(5,6), any(6,7));

Operator overloading realy is magic. ;) I also added a regex ->match method which alls things like if (any(@strings)->match(all($regex))

package List::Junctions; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(none any all); use strict; use vars qw/ $comparisons $compute/; use overload ( '@{}', sub { my $this = shift; return $this->{data}; }, '""', sub { my $this = shift; return join($" , @{$this->{data}}) if $this->{type} e +q 'all'; return $this->{data}->[rand @{$this->{data}}] if $thi +s->{type} eq 'any'; return ''; }, 'bool', sub {my $this = shift; return $this->{bool}; } ); my @bins = qw(binary 3way_comparison num_comparison str_comparison); foreach my $op (split " ", "@overload::ops{ @bins }") { $comparisons->{$op} = eval "sub { return shift() $op shift() }"; eval "use overload '$op' => sub {compare( '$op', \@_) };"; }; @bins = qw(with_assign); foreach my $op (split " ", "@overload::ops{ @bins }") { $compute->{$op} = eval "sub { return shift() $op shift() }"; eval "use overload '$op' => sub { compute( '$op', \@_) };"; }; $comparisons->{regex} = sub { return regex(@_) }; sub new { my $class = shift; my $type = shift || 'any'; return bless { type => $type , data => [@_], }, $class; } sub any { __PACKAGE__->new('any',@_); } sub all { __PACKAGE__->new('all',@_); } sub none { __PACKAGE__->new('none',@_); } sub true { $_[0]->{bool} = 1; $_[0]; } sub false { $_[0]->{bool} = 0; $_[0]; } sub match { compare("regex",@_); } sub compare { my ($how,$self,$compare, $reverse) = @_; my ($true,$false) = (all()->true(), all()->false()); foreach my $item (@{$self->{data}}) { my $test = $reverse ? $comparisons->{$how}->($compare,$item) : $comparisons->{$how}->($item,$compare); if ($test) { push @{$true->{data}} , $item; } else { push @{$false->{data}}, $item; } } return $true if (($self->{type} eq 'none') && scalar @{$true} == +0) or (($self->{type} eq 'all') && scalar @{$false} == +0) or (($self->{type} eq 'any') && scalar @{$true} != +0); return $false; } sub compute { my ($how,$self,$compare, $reverse) = @_; my $new = __PACKAGE__->new($self->{type}); foreach my $item (@{$self->{data}}) { my $new_item = $reverse ? $compute->{$how}->($compare,$item) : $compute->{$how}->($item,$compare); push @{$new->{data}}, $new_item; + } return $new; } sub regex { my ($item,$comparison) = @_; if (ref($item) eq __PACKAGE__) { return $item->match($comparison); } elsif ( ref($comparison) eq __PACKAGE__) { return $comparison->match($item,1); } else { return $item =~ $comparison; } } 1;

___________
Eric Hodges

In reply to Re^6: RFC: Junction.pm by eric256
in thread RFC: Junction.pm by fireartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-19 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found