Text or Number

Joshua Goodall joshua at roughtrade.net
Sun Mar 28 05:13:07 CST 2004


On Sun, Mar 28, 2004 at 11:25:32AM +1000, Andrew Savige wrote:
> Notice that Joshua's prints:
> 
> 42: int=1 str= 
> 42: int=1 str= 
> 42: int=1 str=1
> 42: int=  str=1
> 
> while merlyn's trick prints:
> 
> 42: int=1 str= 
> 42: int=1 str= 
> 42: int=1 str= 
> 42: int=  str=1
> 
> Why the difference?

Both these are exploiting the internal representation; merlyn's
is using a side-effect, whilst mine directly interrogates the
guts.  Merlyn's result is one or the other; mine reveals the
"dualvar" property of Perl data.  Literally, both an integer
and a string representation are kept.

With appropriate use of SV flags (see sv.h and note that not
all are in B) you can determine various other properties of scalars,
e.g. blessedness, UTF8-encodedness.

However, I guess merlyn's has more chance of being retained in future
perls.

> Also, I'm curious to know which type of applications
> care about this (in the original question, I didn't understand exactly
> how this was causing trouble).

Scott is writing parallel port driver, and some API users confuse
ascii '1' (binary 0001111) with the actual value 00000001.  This can
result in more parallel lines being set than expected :).  By detecting
that the API user actually used a literal 1, you can implement a
DWIM handler internally e.g. using pack().

J


-- 
Joshua Goodall                           "as modern as tomorrow afternoon"
joshua at roughtrade.net                                       - FW109



More information about the Melbourne-pm mailing list