Re: Using References to Formats, Examining Scalars With Devel::Peek



Hi,
Replying a bit late and slightly out-of-order, too.

attn.steven.kuo@xxxxxxxxx wrote:
I wrote:
To make format references useful at all, I suppose one would have to be
able to dereference them somehow. IS that possible, and if so how?
Well, just use another typeglob for dereferencing.

sub writeForm
{ # Write out the specified format.
local $~ = shift;
write;
} # sub

becomes:

use Scalar::Utiil ('reftype');

sub writeForm {
if (reftype $_[0] and reftype $_[0] eq 'FORMAT') {
local *FOO;
*FOO = $_[0];
local $~ = 'FOO';
write;
} else {
local $~ = shift;
write;
}
}

Hey, thanks for the code. Works fine for me. Somehow aliasing the name of
the format to be able to refer to it by another name didn't occur to me. But
as the built-in functions seem to support using a stringified scalar as a
format name, I doubt if using format refs have any benefits at all. It just
serves to make the syntax more gory, eh. No wonder they haven't been
documented propperly.

You can use Devel::Peek instead of Data::Dumper
if you want to look at the guts of a format reference.
Ah, nice. Seems most of the fields are the same as for the rest of the Perl
data types. The Peek module can be quite handy, I think. I initially
overlooked it, when going through all the built-in modules that semed
interesting, because the title talks about XS and I'm not ready to tackle it
yet.

Your note about the function got me playing around with other Perl variables
and browsing PErlguts to understand at a high level what's going on behind
the scenes.

In particular, I've been tracking when exactly scalars change their datatype
from integer to double and how frequently the string portions are updated.
Again I don't think even Perlguts covers the rules but I might be wrong, as
I didn't read it all through.

I've only experimented a little but it would seem to me that:

- Perl is pretty smart in keeping integer values integers. Even if you add
and subtract doubles you might still end up with an integer value, if the
result happens to fit exactly to an int.

- Using an operator or a built-in with a double and an int usually converts
the int to a double if you assign the result to a variable. THis is natural,
of course.

- There don't seme to be many instances in which variables which are now
doubles would be converted to ints. Even the int function doesn't do that,
if I'm interpreting these funny type abbreviations right.

- Perl appears to update all but the most recent types lazily. Sometimes the
string or double values can lag behind considerably. They do get updated
when current values for the types are needed such as in stringification or
calling functions taking doubles.

--
With kind regards Veli-Pekka Tätilä (vtatila@xxxxxxxxxxxxxxxxxxxx)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/


.



Relevant Pages

  • Re: Parsing strings to other datatypes without throwing exceptions
    ... int month = int.Parse); ... bool isDate; ... > I want to be able to check that a string is of a desired format ... > VB IsDatefunction doesn't work becuase it doesn't know what format ...
    (microsoft.public.dotnet.framework)
  • Re: another printf question!!
    ... > int factorial(int); ... It's best to use fflushafter a printf call, the format string ... Don't include whitespace characters at the end of a scanf format string. ...
    (comp.lang.c)
  • Re: range for int
    ... int main ... the o/p that i get: ... string "LONG_MIN". ... And you need to use the right format for the type you're printing. ...
    (comp.lang.c)
  • Re: String parsing question
    ... > My task is to convert items in the third format to the first format, ... > in the string, which may or may not have a trailing semicolon. ... While I could count semicolons easily with strchr, ... int myfunc(const char *idlist) ...
    (comp.lang.c)
  • Re: Convert int for string
    ... for string. ... Ho I can do it in Perl? ... Perl isn't a strong type language like C,so you don't have the need to ... would print 246 correctly,perl treat $x as an int type variable. ...
    (perl.beginners)