Dr.Ruud wrote:
> William schreef:
>> [*attribution dammit*] Ruud:
> 
>>> There is no real way to test if a value inside a variable has a
>>> numeric "personality". Variables can have multiple "personalities",
>>> each with there own binary value.
>>>
>>> So maybe the problem just comes from the leading zeroes that your
>>> sprintf format added?
>> Not exactly from the leading zeros,
>>
>> use Data::Dumper;
>> my $numStr = sprintf("%4d", 1234);
>> print (Dumper $numStr), "\n";
>>
>> my $numStr = sprintf("%05d", 1234);
>> print (Dumper $numStr), "\n";
>>
>> my $num = sprintf("%05d", 1234);
>> $num += 0;
>> print (Dumper $num), "\n";
>>
>>
>>   use Language::Prolog::Types::overload;
>>
>>   use Language::Prolog::Types qw(:ctors);
>>
>>   print prolog_functor('foo', $numStr), "\n";
>>   print prolog_functor('foo', $num), "\n";
>>
>>
>> $VAR1 = '1234';
>> $VAR1 = '01234';
>> $VAR1 = 1234;
>> foo(01234)
>> foo(1234)
>>
>>
>> As you can see the last of $VAR1 does not have single quote, so it's
>> really in number data type.
> 
> It is not "in number data type", it is merely available as numeric. (as
> I have said before)
> 
> 
>> I think so far, adding zero solve the problem.
> 
> That is indeed one of the ways to add a numeric personality to a
> variable.
> Testing with a numeric operator also "works", see below:
> 
> $ perl -Mstrict -Mwarnings -MData::Dumper -wle'
>    my $s = "1234";
>    print Dumper($s);
>    print 1 if $s == 0;
>    print Dumper($s);
>    print 2 if $s eq "1234";
>    print Dumper($s);
> '
> $VAR1 = '1234';
> 
> $VAR1 = 1234;
> 
> 2
> $VAR1 = 1234;
> 
> That Data::Dumper prefers to print the numeric face of the variable, if
> available, is a matter of choice inside the code of Data::Dumper.
> Maybe the other modules that you use, have Data::Dumper embedded?

That is not true. Each scalar value has a bitmask saying which of its data
fields are useable. Rewriting William's code to use Devel::Peek shows this 
clearly:

use strict;
use warnings;

use Devel::Peek qw/Dump/;

my $numstr = sprintf("%4d", 1234);
Dump $numstr;

$numstr = sprintf("%05d", 1234);
Dump $numstr;

my $num = sprintf("%05d", 1234);
$num += 0;
Dump $num;

**OUTPUT**

SV = PV(0x365ec) at 0x3652c
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,POK,pPOK)
  PV = 0x182ea94 "1234"\0
  CUR = 4
  LEN = 8
SV = PV(0x365ec) at 0x3652c
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,POK,pPOK)
  PV = 0x182ea94 "01234"\0
  CUR = 5
  LEN = 8
SV = PVIV(0x37604) at 0x357c4
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,IOK,pIOK)
  IV = 1234
  PV = 0x183ba74 "01234"\0
  CUR = 5
  LEN = 8

So in the first two cases you can see that the POK flag (denoting a string
value) is set, but in the third case, after adding zero, the IOK flag becomes
set (indicating an integer) while the POK flag is cleared, meaning that the the
string value is no longer valid, even though there is still space allocated for
it and its value is unchanged.

HTH,

Rob

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


Reply via email to