Perl variable type assessment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl variable type assessment
# 1  
Old 01-21-2009
Perl variable type assessment

Hello experts,
How we can find out,that what is type of a scalar variable? i.e a scalar var contain a number or a string.
Thanks in advance.
# 2  
Old 01-21-2009
As far as I know Perl uses dynamic typing, meaning the scalar always contains what Perl thinks is expected, eg a Regex will see a string, whereas int() or '*' will see a number if possible.
# 3  
Old 01-21-2009
Zaxon,

Its upto perl interpretor to decide whether variable type is floating ,integer or string.

For instance :
$i =5 ; (For perl interpreter it is a integer datatype)
$i =5.2 ; (For perl interpreter it is a floating datatype)
$i ="MY Perl" ; (For perl interpreter it is a String datatype)

So basically it is all about declaration and assigning value to scalar value will help you to understand the type of data type it is.

Cheers,
gehlnar
# 4  
Old 01-21-2009
pludi and gehlnar thanks, you now, I have a scalar that its value might be integer or a character, and I need to compare it with an if statement, I should be used != or ne ???
Regards

Last edited by Zaxon; 01-21-2009 at 06:22 AM..
# 5  
Old 01-21-2009
Zaxon,
Use != in case of numeric value comparision and use ne for string/character value comparision.

Cheers,
gehlnar
# 6  
Old 01-21-2009
yes, I know, but might from these two operand the first be an integer and the second be a character! may be.
Help me please. I wanna assessment the type of a scalar variable.

Last edited by Zaxon; 01-21-2009 at 07:12 AM..
# 7  
Old 01-21-2009
You can "enforce" a certain type by interacting with the desired type, eg. to force a string append and empty string ($var+="") or for a number multiply with 1 ($var*=1)
Note though that this is not always guaranteed to work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Strange value of the double type variable: -nan(0x8000000000000)

I am confused by the value of "currdisk->currangle" after adding operation. Initially the value of "currdisk->currangle" is 0.77500000000000013, but after adding operation, it's changed to "-nan(0x8000000000000)", Can anyone explain ? Thanks! The following is the occasion of gdb debugging. 3338 ... (8 Replies)
Discussion started by: 915086731
8 Replies

2. Shell Programming and Scripting

What is a type of this variable?

I have a question about the type of this variable checkU= sudo cat /etc/passwd: grep $uname: wc -l I write a script to check the existent of username that get from the keyboard. If there is no username,it will create the username else it show the 'Duplicate name" message. The problem is it... (4 Replies)
Discussion started by: thsecmaniac
4 Replies

3. Shell Programming and Scripting

what is the default return type of localtime() in perl?

Hi, I have given like this to get the time of the sub routine. my $start = localtime(); print "\n start time: $start \n"; Output start time: Fri Apr 29 01:01:31 2011 I want to know what is the format of the time. I am not able to follow is is HH:MM:SS or MM:HH:SS os... (2 Replies)
Discussion started by: vanitham
2 Replies

4. Programming

C++ type-casting a member variable in const methods

Is it permitted to type-cast a member variable passed in as a parameter in a member const method .. I am doing something like : in a return-type method () const { variable other = long(member variable) } this other assigned variable is not updating and I wonder if type-casting in such... (1 Reply)
Discussion started by: shriyer123
1 Replies

5. Shell Programming and Scripting

Perl data type checking

I am using perl 5.8.0. I need to check some values to see it they are floats. Our system does not have Data::Types so I can't use is_float. Is there something else that I can use? The only thing in Data is Dump.pm. I am not allowed to download anything to our system so I have to use what I have.... (3 Replies)
Discussion started by: ajgwin
3 Replies

6. Shell Programming and Scripting

use perl to get file type ?

Can anybody please tell me how can I determine whether a file is SYMBOLIC LINK, using the stat() function ? So far I have this: my @attrs = stat($fileName); my $mode = $attrs; What next ? (1 Reply)
Discussion started by: the_learner
1 Replies

7. Shell Programming and Scripting

getting type of variable

Hello: Is there any way to tell the type of a passed or entered variable, if it is a string or integer,...etc. Thanks a lot (2 Replies)
Discussion started by: aladdin
2 Replies

8. Shell Programming and Scripting

how to get type of variable in perl

hello all how can i get the type of variable in perl like typeof(var); in javascript for instance ? to know if the variable is int or string ? (2 Replies)
Discussion started by: umen
2 Replies

9. Shell Programming and Scripting

determine file type with perl

Hello i will like to know please how can i determine file type inside perl script not using the unix "file" program Thanks allot (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

Problem with variable type

Dear All, I am trying to write an script to calculate geometric centre of selected residues of a protein structure. Say I have a PDB file (a text containing the x, y, and z coordinates of atoms of a protein). I want to extract the X coordinates of the atoms belonging to the interested residues... (2 Replies)
Discussion started by: siavoush
2 Replies
Login or Register to Ask a Question