Data: Misc
Perl is binary clean, so this shouldn't be a problem. For example, this works fine (assuming
the files are found):
if (`cat /vmunix` =~ /gzip/) {
print "Your kernel is GNU-zip enabled!\n";
}
|
|
On less elegant (read: Byzantine) systems, however, you have to play tedious games with
"text" versus "binary" files. See perlfunc/"binmode"
or perlopentut. Most of
these ancient-thinking systems are curses out of Microsoft, who seem to be committed to putting
the backward into backward compatibility.
If you're concerned about 8-bit ASCII data, then see perllocale.
If you want to deal with multibyte characters, however, there are some gotchas. See the
section on Regular Expressions.
Assuming that you don't care about IEEE notations like "NaN" or
"Infinity", you probably just want to use a regular expression.
if (/\D/) { print "has nondigits\n" }
if (/^\d+$/) { print "is a whole number\n" }
if (/^-?\d+$/) { print "is an integer\n" }
if (/^[+-]?\d+$/) { print "is a +/- integer\n" }
if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal number\n" }
if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{ print "a C float\n" }
|
|
You can also use the Data::Types module on the CPAN, which
exports functions that validate data types using these and other regular expressions.
If you're on a POSIX system, Perl's supports the POSIX::strtod function. Its
semantics are somewhat cumbersome, so here's a getnum wrapper function for more
convenient access. This function takes a string and returns the number it found, or undef
for input that isn't a C float. The is_numeric function is a front end to getnum
if you just want to say, ``Is this a float?''
sub getnum {
use POSIX qw(strtod);
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$! = 0;
my($num, $unparsed) = strtod($str);
if (($str eq '') || ($unparsed != 0) || $!) {
return undef;
} else {
return $num;
}
}
sub is_numeric { defined getnum($_[0]) }
|
|
Or you could check out the String::Scanf module on the CPAN
instead. The POSIX module (part of the standard Perl distribution) provides the strtod
and strtol for converting strings to double and longs, respectively.
For some specific applications, you can use one of the DBM modules. See AnyDBM_File. More generically,
you should consult the FreezeThaw or Storable modules from CPAN. Starting from Perl 5.8 Storable
is part of the standard distribution. Here's one example using Storable's store and
retrieve functions:
use Storable;
store(\%hash, "filename");
# later on...
$href = retrieve("filename"); # by ref
%hash = %{ retrieve("filename") }; # direct to hash
|
|
The Data::Dumper module on CPAN (or the 5.005 release of Perl) is great for printing out data
structures. The Storable module, found on CPAN, provides a function called dclone
that recursively copies its argument.
use Storable qw(dclone);
$r2 = dclone($r1);
|
|
Where $r1 can be a reference to any kind of data structure you'd like. It will be deeply
copied. Because dclone takes and returns references, you'd have to add extra
punctuation if you had a hash of arrays that you wanted to copy.
%newhash = %{ dclone(\%oldhash) };
|
|
Use the UNIVERSAL class (see UNIVERSAL).
Get the Business::CreditCard module from CPAN.
The kgbpack.c code in the PGPLOT module on CPAN does just this. If you're doing a lot of
float or double processing, consider using the PDL module from CPAN instead--it makes
number-crunching easy.
|