Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
package Backtracking; $VERSION = 0.03; use strict; use Carp; sub import { while (my $arg = shift) { # process args if ($arg eq 'filter') { croak "'filter' option to Backtracking must be specified l +ast!\n" if @_; require Filter::Util::Call; import Filter::Util::Call; filter_add( sub { my ($status, $no_seen, $data); my $count = 0; while ($status = filter_read()) { return $status if $status < 0; if (/\bno\s+Backtracking\s+'filter'\s*;\s*$/) +{ $no_seen=1; last; } $data .= $_; $_ = ""; $count++; } $_ = $data; s/\bgoal([\r\n\s]+)(\w+)([\r\n\s]*\{)/Backtracking +::goal $1 '$2' => sub $3/g unless $status < 0; $_ .= "no Backtracking 'filter';\n" if $no_seen; $count; } ); } elsif ($arg eq 'goal') { my ($package) = caller; no strict 'refs'; *{$package."::goal"} = \&goal; } } } sub unimport { filter_del() if $_[0] eq 'filter'; } sub goal($&) { my ($name,$code) = @_; my ($package) = caller; no strict 'refs'; my $btarray = $package.'::BACKTRACK_'.$name; push @{$btarray},$code; if (@{$btarray} == 2) { eval " sub $package".'::'."$name { for (@".$btarray.") { my \$rv = \$_->(\@_); return \$rv if \$rv; } return; } 1; " or croak $@; } } 1; __END__ =pod =head1 NAME Backtracking =head1 DESCRIPTION A very simple backtracking mechanism. =head1 SYNOPSIS use Backtracking qw(goal filter); goal is_member { # filter syntax my ($x,$y,@list) = @_; return $x eq $y; }; goal is_member => sub { # goal syntax my ($x,undef,@list) = @_; return unless @list; return is_member($x,@list); }; for (qw(a b c d e f)) { if (is_member($_,qw(a b c d e))) { print "$_ is a member\n"; } else { print "$_ is not a member\n"; } } # That should print a is a member b is a member c is a member d is a member e is a member f is not a member =head1 KNOWN BUGS / CAVEATS =over 4 =item * All goals are run in SCALAR context. This might be changed in the futu +re. =item * There is no statement to express a 'cut'. =item * If you have the C<Filter> package installed, you can C<use Backtracking 'filter'> to turn something like: goal goal1 { ... }; into goal 'goal1' => sub { .... }; using the following regex: s/\bgoal([\r\n\s]+)(\w+)([\r\n\s]*\{)/Backtracking::goal $1 '$2' = +> sub $3/g This might mismatch sometimes. =back =head1 SEE ALSO L<Filter::Util::Call>, L<Filter::Simple> =head1 AUTHOR Joost Diepenmaat -

In reply to by Joost

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and monks are getting baked in the sun...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (1)
    As of 2018-05-23 01:44 GMT
    Find Nodes?
      Voting Booth?