Basic question
sanPerl
created: 2006-03-03 07:48:39
Dear Monks,
In Perl once a variable is defined, it can be used as integer and string as well, like.
$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...
Re: Basic question
created: 2006-03-03 07:59:37

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.

Igor 'izut' Sutton
your code, your rules.

Re: Basic question
created: 2006-03-03 08:19:51
Re: Basic question
created: 2006-03-03 08:22:06
java, c, c++ etc ... lol


This is not a Signature...
Re: Basic question
created: 2006-03-03 08:53:28
If you _really_ need to force some type, you can tie the variable and check stored scalar against regexp, or you can write own xs module with required behavior. See perltie, perlxs, perlguts.
Nothing very easy (excluding tied variables), do you really need it? Perhaps some regexp check on input can satisfy your criteria...
Re: Basic question
created: 2006-03-03 10:36:12

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?

<-radiant.matrix->
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re^2: Basic question
created: 2006-03-03 21:44:14
I needed this feature for input validation, which my tool would take from commandline arguments. I had plans to parse arguments through regex, but it seems I can use Attribute::Types and Data::Types for it
Thanks to everyone for the help.

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