Q&A

Q1: How do I define a variable so that it'll only contain a number?
A1: You can't. Perl doesn't have strong number types the way other languages do; the closest you can get is a scalar variable, and that can contain either a number or a string.

Most of the time, this will not matter; Perl will convert strings to numbers for you and vice versa.

Q2:But what if I end up with data in string form that isn't a number and I try to do something numeric with it?
A2: The default behavior is as I described in the section on “Converting Between Numbers and Strings.” Strings with no numeric content will become 0. Strings that start with numbers and then revert to characters will lose the characters.

If you have warnings turned on in your Perl script, then ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.