Dear Monks,
Why my code below seems to unable to benchmark a method taken from String::Approx?

#!/usr/bin/perl -w use strict; use Data::Dumper; use String::Approx qw\amatch\; use Benchmark 'cmpthese'; #----------Begin Benchmarking-------------- my $str = 'AAAAA'; my @arr = ('AAAAA','ATAAA','ATCGA','ATCAA'); my @my_match = mymatch($str,2,\@arr); print Dumper \@my_match; my @jk_match = amatch($str,['2'],@arr); print Dumper \@jk_match; #It fails here cmpthese (-5,{ me => "mymatch($str,2,\@arr)", jk => "amatch($str,['2'],@arr)",}); #------------------ Subroutine ------------- sub mymatch { my ($pattern, $d, $arr_2match) = @_; my @matches; foreach (@$arr_2match){ my $diff = hd($pattern,$_); #print "$_ --- Diff: $diff\n"; if ($diff <= $d){ push @matches, $_; } } return @matches; } sub hd { #String length is assumed to be equal my ($a,$b) = @_; my $len = length ($a); my $num_match=0; for (my $i=0; $i<$len; $i++) { # assume that the lists are of same length ++$num_match if substr($a, $i, 1) ne substr($b, $i, 1); } return $num_match; }
Such that it gives this result:
[snip others] Can't locate object method "ATCGA" via package "ATCAA" (perhaps you forgot to load "ATCAA"?) at (eval 7) line 1.
Is Benchmark only cater for Perl written subroutine/module? Is there a way to overcome it?
Regards,
Edward

In reply to Benchmarking String::Approx by monkfan

Title:
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, details, 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, summary, 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.