I can do this in Python:
>>> type(1)
<class 'int'>
What’s the Perl equivalent?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Perl does not make a hard distinction between strings and numbers the way that Python or Ruby do. In Perl, the use of operators determines how the variable will be interpreted.
In Python, when you write
1 + 2, Python checks the type of each argument, sees that they are numbers, and then performs the addition (3). If you write'1' + '2', it sees that both are strings, and performs concatenation ('12'). And if you write1 + '2'you get a type error.In Perl, when you write
1 + 2, the+operator imposes numeric context on it’s arguments. Each of the arguments is converted to a number (with a subsequent warning if the argument could not be converted cleanly) and then the addition is performed (3). If you write'1' + '2', the arguments are still converted to numbers with a result of3.If you wanted concatenation, you would use the
.operator:1 . 2which will result in'12'even though both of the arguments were numbers.So since Perl’s operators force a type interpretation, the variable itself does not need to (and doesn’t) contain a type. If you really need to determine which is which, you can use
Scalar::Util‘slooks_like_numberfunction, but generally coding like that in Perl is indicative of a design problem.Perl’s scalar variables can contain one of 4 things:
undef)The
refkeyword is used to determine if a scalar contains a reference, and what type that reference is.The full list can be found at perldoc -f ref
When a reference is
blessed into a package, that reference becomes an object. In that case,refwill return the name of the package the object is blessed into.