The type of variable (scalar, array, or hash), is typically determined by the sigil attached to the front of the variable name ($,@, and %, respectively). However, if you're looking for traditional data types like int, float, char, etc, you need to start using regular expresssions to determine that. Perl's type system is based around data structures, not data types.

As a side note, if you're not sure what data type a variable is, you probably have a deeper programming issue that you should probably look at. Typically it should be obvious what data type you're dealing with. For example, if you are using a C-style for loop, you probably have an integer.

for ( my $i = 0; $i < 10; $i++ ) { # do stuff }

However, the nifty thing about the above snippet is this: it doesn't matter what data type $i is. Yes, there are cases where this is important, but most of the time, Perl allows you to focus on the actual problem you're trying to solve (how can I import this banking data?) instead of focusing on the minutia (should this be a short or a long?). This is a big win with Perl, but it's a tough concept for many to get used to.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Re: Determine type of variable by Ovid
in thread Determine type of variable by Jrax

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.