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

Is perl capable of checking whether a scalar is a number or a string?
#!/usr/bin/perl -w use warnings; sub test { my ($gil, $char) = @_; print ("you have $gil for $char."); } &test (925, "Mithos");
I want to achieve something along the lines of:
if ($gil != num){die "Gil should be a number.";} if ($char != string){die "Your characters name should consist of lette +rs only.";}
Does perl have a function for this or.. ?

Replies are listed 'Best First'.
Re: Checking for Number or String
by ikegami (Patriarch) on Nov 15, 2009 at 23:09 UTC
    Scalar::Util's looks_like_number tells the difference between what's a number and what's not (according to Perl's definition).
    use Scalar::Util qw( looks_like_number ); die("Invalid amount of Gil \"$gil\"\n") if !looks_like_number($gil); die("Invalid character name \"$char\". " ."It must only contain letters [a-z])\n") if $char !~ /^[a-zA-Z]+\z/;
      Thanks a lot, Scalar::Util will come in very handy!
Re: Checking for Number or String
by biohisham (Priest) on Nov 15, 2009 at 23:10 UTC