ref() is a good start. Scalar::Util provides a number of other options, such as looks_like_number() (which tells you if something is probably a number), blessed() (which returns the class it's blessed into, if any). As for int vs. float, that's easy - int($a) == $a means it's an int.
The important point here is that when you program in Perl, you shouldn't have to worry about int vs. float. Personally, I tend to not worry about anything except reference, object, and non-reference.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] [d/l] |
All this helps a lot!!!!
Thanks Luca
| [reply] |
| [reply] |
You can use ref for some of that:
if ( ref( $your_var ) =~ /HASH/ ) { ... }
or in more draconian fashion
if ( ref( $your_var ) eq 'HASH' ) {...}
It won't tell you whether a scalar is a string or a number.
Phil | [reply] [d/l] [select] |
Check ref(), it does what you want -- except differ between SCALAR types.
To differ between scalars, I do kludgy stuff (like add 0 and see if the result is not-zero and =~ /^\s*0\s*$/.) (-: Someone else will certainly answer better. :-)
| [reply] [d/l] |
The date is year-julian. One last question: If I would like to allow the following formats:
2005-111
2005111
2005
Somehow this doesn't work:
$a = $some_date ;
if ( $a =~ /\d{4}-?\d{3}?/ ) {
....
}
Any suggestions why this doesn't work ?
Thanks a lot Luca | [reply] [d/l] [select] |