Acme-Machi

 view release on metacpan or  search on metacpan

lib/Acme/Machi.pm  view on Meta::CPAN

      SRCH_Habit => 'BFS',
    }, $_[0];
  }

=head2 named

        Assign a new value to scalar-type instance variable, 'Name', in the object.
        Return: value of assignment.

=cut
  sub named {
    (ref $_[0]) ||  croak "Oops! Cannot use class method setting the object!";
    $_[0]{Name} = $_[1];
  }

=head2 name

        Return: person's name.

=cut
  sub name {
    $_[0]{Name};
  }


=head2 have_the_habit_of

        Assign a new searching habit to scalar-type instance variable, 'SRCH_Habit'.
        Only strings 'BFS' and 'DFS' are valid, setting the others will be ignored.
        Return: value of assignment.

=cut
  sub have_the_habit_of {
    (ref $_[0]) ||  croak "Oops! Cannot use class method setting the object!";
    $_[0]{SRCH_Habit} = $_[1] if($_[1] =~ m/([DB]FS)/);
  }

=head2 habit

        Return: person's searching habit.

=cut
  sub habit {
    $_[0]{SRCH_Habit};
  }


=head2 learning

        Append a list of words to array-type instance variable, 'Words', in the object.
        Return: how many words have she/he learnt.

=cut
  sub learning {
    (ref $_[0]) ||  croak "Oops! Cannot use class method setting the object!";
    unshift (@{$_[0]{Words}}, @_[1 .. $#_]);
  }
       
=head2 affectionate

        Randomly output one of predefined words to FILE_HANDLE, which default of is STDOUT.
        Return: 1 if no problems while calling this method.

=cut
  sub affectionate {
    (ref $_[0]) ||  croak "Oops! Cannot call affectionate() using class method!";
    my $words_list = $_[0]{Words};
    ($_[1] // *STDOUT)->print( $_[0]->name(),": ", $words_list->[int(rand($#$words_list))], "\n");
  }
  
       
=head2 search_file_from

        Using BFS or DFS to search the target from certain directory.
        Return: a two-element list: 
                  the first element is boolean value denoting whether the target was found or not.
                  the second element is the result string outputed from the core module, Data::Dumper.
                    You may get to know files distribution even better after printing the string.

=cut
  sub search_file_from {
    ref $_[0] ||  croak "Oops! Cannot ask non-human to search!";
    my ($target, $dir, $RESP) = @_[1,2,3];
    my $obj = File::Spec->catfile(getcwd, $target);
    my $s_dir = File::Spec->catfile(getcwd, $dir);
    my $push_front_back = ($_[0]->habit() eq 'DFS')? 
      sub {
        unshift(@{$_[0]}, $_[1]);
      }
      : sub {
        push(@{$_[0]}, $_[1]);
      };

    my $data = {};
    my @queue = ( [$s_dir, $data] );
    my ($elm, $np, $nd, $key, $found);
    ($obj eq $s_dir) && ($found = 1);
    return 1 if $RESP;
    while($elm = shift @queue){;
      ($np, $nd) = @$elm;
      $key = basename($np);
      $nd->{$key} = (-l $np or -f _)? undef : +{};
      if (ref $nd->{$key}) {;
        my $dh = IO::Dir->new("$np");
        my $npp;
        foreach ($dh->read()) {;
          $npp = File::Spec->catfile($np,$_);
          ($obj eq $npp) && ($found = 1);
          return 1 if $RESP;
          (m/\A\.{1,2}?\z/aa) || $push_front_back->(\@queue, [ $npp, $nd->{$key} ]);
        }
      }
    }
    print Data::Dumper->Dump([\$data],[qw% *data %]);
    $found;
  }
 

=head1 AUTHOR

Machi Amayadori, C<< <Eosin at Syaro.Cafe> >>



( run in 0.788 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )