Q:

perl STDIN to subroutine

I was writing a code to just find any non-alpha numeric characters in the input line. I tried the following code:
sub Verify
{
    my $string = ($_[0]);
    print "$string\n";
    if ($string =~ /[^a-zA-Z0-9]/)
    {
        print "The string contains non-alphanumeric characters\n";
    }
    else
    {
        print "No invalid characters";
    }
}

$message = <STDIN>;
Verify ($message);

I tried with different inputs but the output is always "The string contains non-alphanumeric characters". What is wrong in this code?

A:

When reading from STDIN, you have also the \n character at the end or your variable. So you have to remove it before match the regexpr. This is easy with the help of chomp function:
chomp $string;

For debugging purposes, change the print statement to print "[$string]\n"; to see this effect.

