You can always try the ref() command to figure out whether the variable you are dealing with is an array, a hash, or a scalar. Of course, this method works on a reference to the actual variable. Say like this:
my %hash = (foo => bar);
my @arr = (1);
my $var = \%hash;
my $var1 = \@arr;
my $var2 = 2;
print ref $var;
print ref $var1;
print ref $var2;
Will print:
HASH
ARRAY
* the last line is '' for 'not reference'.
I'm not sure if you really want to go any further from this and actually attempt to determin whether a variable is a string or an integer (or float). For that you could use regular expressions..
my $num = 15;
if ($num =~ /^[+-]?\d+$/) {
print "Dealing with an integer\n";
}
You could also play around with other forms of the match to distinct between float and integer values. However, in many cases when anyone has to go as far as to having to determine precise type of a variable, there's a big chance his/her code is wrong.
|
"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith
|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.