#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my %tests=( '6407587026'=>1, '97698bhj767565'=>0, '7867854jh6546789'=>0, '86565'=>1, '54587'=>1, 'ghggftr'=>0, '557765547'=>1, 'g569876876'=>0, '656778z6565'=>0, '6578867ggh7'=>0, '54tf5945'=>0, '67556567'=>1, '765476'=>1, '*Ä?=()&§9§""¹³¹²³½[}\¼'=>0, '454564'=>1, '547989'=>1, '709'=>1, '=)(/(&$§"%%/)(=/(/'=>0, ); my @zahlen=keys(%tests); my %functs=( 'split1'=>\&test_of_number_a, 'split2'=>\&test_of_number_b, 'regexp'=>\&test_of_number_c, ' int'=>\&test_of_number_d, 'unpack'=>\&test_of_number_e, ' tr'=>\&test_of_number_f, ); while( my ($name,$func)=each(%functs)) { my $ok=1; while(my ($num,$res)=each(%tests)) { $ok=0 if($func->($num)!=$res); } print "$name => ",$ok?'OK':'FAILED',"\n"; $functs{$name}=sub{ $func->($_) for(@zahlen); }; } cmpthese(40000,\%functs); ########################################################################################## sub test_of_number_a { for my $pos (0..length($_[0])-1) { $_=ord(substr($_[0],$pos,1)); return 0 if($_<48 || 57<$_); } return 1; } sub test_of_number_b { for(split('',''.shift())) { $_=ord($_); return 0 if($_<48 || 57<$_); } return 1; } sub test_of_number_c { return 1 if($_[0]=~/^\d+$/); return 0; } sub test_of_number_d { no warnings; return 1 if(int($_[0]) eq $_[0]); return 0; } sub test_of_number_e { ($_<48 || 57<$_)?return 0:undef() for(unpack('C*',$_[0])); return 1; } sub test_of_number_f { return 1 if($_[0]=~ y/0-9// == length($_[0])); return 0; }