Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Declaring a variable's type

by sanPerl (Friar)
on Mar 03, 2006 at 12:48 UTC ( [id://534212]=perlquestion: print w/replies, xml ) Need Help??

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

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...

2006-03-04 Retitled by planetscape, as per Monastery guidelines
Original title: 'Basic question'

Replies are listed 'Best First'.
Re: Declaring a variable's type
by izut (Chaplain) on Mar 03, 2006 at 12:59 UTC

    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: Declaring a variable's type
by Anonymous Monk on Mar 03, 2006 at 13:19 UTC
Re: Declaring a variable's type
by pajout (Curate) on Mar 03, 2006 at 13:53 UTC
    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: Declaring a variable's type
by radiantmatrix (Parson) on Mar 03, 2006 at 15:36 UTC

    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
      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.
Re: Declaring a variable's type
by monkey_boy (Priest) on Mar 03, 2006 at 13:22 UTC
    java, c, c++ etc ... lol


    This is not a Signature...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://534212]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-26 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found