princepawn has asked for the wisdom of the Perl Monks concerning the following question:

For the following code:
my $test = shift; # we want a binary "tape" of 1s and 0s as the arg unless ( $test and $test =~ /^[01]+$/ ) { die "Your input was: $test\nGive a string (tape) of zeros and ones + only!\n"; } my @test_queue = split '', $test; # turn string into queue/array
We get the following behavior even though a single zero should pass the regexp test. A single one does pass, BTW.

Anyone know why? Gracias.

c:\Documents and Settings\1\My Documents\perl\dfa\simple>code.pl 0 code.pl 0 Your input was: 0 Give a string (tape) of zeros and ones only!

Replies are listed 'Best First'.
Re: regexp - character class containing zero does not find zero
by diotalevi (Canon) on Sep 27, 2002 at 05:43 UTC

    The value '0' evaluates to false in a boolean context so your and test fails. Why not use defined() or m/^./ to test for the presence of a $test value?

Re: regexp - character class containing zero does not find zero
by tadman (Prior) on Sep 27, 2002 at 06:05 UTC
    There's a few responses about why it's failing. Here's an idea on how to repair it:
    my ($test) = @ARGV; unless (defined($test) && test =~ /^[01]+$/) { die "..."; } my @test_queue = split('', $test);
Re: regexp - character class containing zero does not find zero
by Zaxo (Archbishop) on Sep 27, 2002 at 05:45 UTC
    unless ( $test and $test =~ /^[01]+$/ ) { ... never gets to the regex since $test is false.

    After Compline,
    Zaxo