Need help about Build_MatchMany_Function !

S

sonet

I am tesing the code from
http://perl.apache.org/docs/general/perl_reference/perl_reference.html#Compiled_Regular_Expressions .
But it seem have some problem ,and i can not slove it by myself.
Sometime ,I got the result "Use of uninitialized value in concatenation (.)
or string at (eval 1) line 1, <HANDLE> line 101."


#you can got notspam.lst FROM
http://se2.program.com.tw/Portal_images/notspam.lst
#!/usr/local/perl
use warnings;
my $i = 0;
my $nsi = 0;
my @test;
open( HANDLE, 'notspam.lst' );
while (<HANDLE>)
{
$_ =~ s/\r|\n| //g;
if ( $_ ne '' && $_ !~ m/\#/ )
{
$test[ $nsi++ ] = $_;
}
}
close(HANDLE);
my $result = expr_build(@test);
open( HANDLE, 'notspam.lst' );
while (<HANDLE>)
{
$_ =~ s/\r|\n| //g;
if ( $_ ne '' && $_ !~ m/\#/ )
{
if ( !&$result("$_") )
{
print $_ . '=>no' . "\n";
sleep(5);
} else
{
print $_ . '=>yes' . "\n";
}
}
}

sub expr_build
{
my @R = @_;
my $expr = join '||', map { "\$_[0]=~/\$R[$_]/oi" } ( 0 .. $#R );
my $subref = eval "sub { $expr }";
return $subref;
}
 
D

Dr.Ruud

sonet schreef:


Some initial remarks:
#!/usr/local/perl

That should be the first line of you script.
More standard is #!/usr/bin/perl

Missing:

use strict;
use warnings;
my $i = 0;
my $nsi = 0;

The $nsi is not needed.
my @test;
open( HANDLE, 'notspam.lst' );

my $infile = 'notspam.lst';
open( HANDLE, '<', $infile ) or die "$infile: $!";
while (<HANDLE>)
{
$_ =~ s/\r|\n| //g;

ITYM:
s/\s+//g;

Regexen work on $_ if not specified otherwise.

if ( $_ ne '' && $_ !~ m/\#/ )
{
$test[ $nsi++ ] = $_;
}

ITYM /^#/ and not /#/.

Change to:
push @test, $_ unless /^$/ or /^#/;

See `perldoc -f push`.


}
close(HANDLE);


There is more to say about the rest of your code, but add the 'use
strict;' and apply the other changes, and see if you get any further
yourself.
 
T

Tad McClellan

Sometime ,I got the result "Use of uninitialized value in concatenation (.)
or string at (eval 1) line 1, <HANDLE> line 101."


Did you look to see what is different about line 101 in your data?

open( HANDLE, 'notspam.lst' );


You should always, yes *always*, check the return value from open():

open( HANDLE, 'notspam.lst' ) or die "could not open 'notspam.lst' $!";

This is good advice that you have been given before.

You should consider accepting good advice rather than ignoring it.

while (<HANDLE>)
{
$_ =~ s/\r|\n| //g;


tr/\r\n //d; # does the same thing

$test[ $nsi++ ] = $_;


push @test, $_; # no need for $nsi, let perl handle it

print $_ . '=>no' . "\n";


print "$_=>no\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top