in reply to Validating ISBN numbers?

I think I'd recommend a "destructive test" on a copy of the string. Perhaps something like this:
sub assert_valid_isbn { local($_) = @_; s/^ISBN\s*// or die "'$_' does not begin with 'ISBN'"; y/- 0-9//cd and die "'$_[0]' contains invalid characters"; /-/ && / / and die "'$_[0]' contains both hyphens and spaces"; y/-/ /; # convert hyphens to spaces. my @g = split; @g == 4 or die "'$_[0]' is broken into the wrong number of groups" +; join('',@g) == 10 or die "'$_[0]' contains the wrong number of dig +its."; }
This doesn't return anything, but throws an exception for invalid values. You could use it like this:
for my $isbn ( @isbns ) { eval { assert_valid_isbn( $isbn ); process_this_isbn( $isbn ); }; $@ and warn $@; };

jdporter
...porque es dificil estar guapo y blanco.

Replies are listed 'Best First'.
(z) Re^2: CGI -Perl problem
by zigdon (Deacon) on Dec 17, 2002 at 18:01 UTC

    my @g = split;

    Careful there - you just allowed stuff like this invalid ISBM through - ISBN 0--06----096975-1. Perhaps you should use:

    my @g = split / /;

    btw, this is valid: ISBN 0-06-096975-X (note the 'X').

    -- Dan