howa wrote:
> On 7月5日, 上午4時56分, [EMAIL PROTECTED] (Rob Dixon) wrote:
>> A Perl scalar value (including array and hash elements) can have more than 
>> one
>> type simultaneously. Why do you think you need to know what type of value is
>> being held?
>>
>> Rob
> 
> hello,
> 
> can you show an example of  "more than one type simultaneously" ?
> 
> this is interesting...

Perl will convert a scalar value between integer, floating point and string
representations internally depending on what your program does with the value,
and more than one representation can be valid at the same time, for instance if
I write

  my $val = '0099';
  print $val+1;

then $val will have both a string value of '0099' and an integer value of 99.

The Scalar::Util module has a function dualvar() that lets you assign both the
string and numeric value of a scalar simultaneously. Try the program below.

HTH,

Rob


use strict;
use warnings;

use Scalar::Util qw/dualvar/;
use Devel::Peek;

my $twovals = dualvar 42, 'string';

print $twovals, "\n";
print $twovals+0, "\n";




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to