$name = "John"; $name = 1000;This is a convenient feature. But if I want to define type specifically then is there any way where I can define integer, float etc...
From perldata:
Scalars aren't necessarily one thing or another. There's no place to declare a scalar
variable to be of type "string", type "number", type "reference", or anything else.
Because of the automatic conversion of scalars, operations that return scalars don't need
to care (and in fact, cannot care) whether their caller is looking for a string, a num-
ber, or a reference. Perl is a contextually polymorphic language whose scalars can be
strings, numbers, or references (which includes objects). Although strings and numbers
are considered pretty much the same thing for nearly all purposes, references are
strongly-typed, uncastable pointers with builtin reference-counting and destructor invo-
cation.
This depends largely on what you are actually trying to accomplish. You could use Attribute::Types to simulate type-checking, but there's a good chance you don't actually need strong typing.
You could also create the behavior you want using overload and creating appropriate modules from which you can instantiate objects. You might also be looking for integer math. Or, if you're doing input validation, type casting, or something similar, you could use Data::Types.
So, what specifically are you aiming to accomplish?
perlmonks.org content © perlmonks.org and Anonymous Monk, izut, monkey_boy, pajout, radiantmatrix, sanPerl
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03