Bart Lateur wrote:

> On Wed, 13 Sep 2000 22:49:14 -0700, Glenn Linderman wrote:
>
> >So who needs a special format type?  Just need a string.  It becomes a
> >format when you use it as such...  So just
> >
> >  my $frm = <<'.'
> >@<<<<<<<<<<<<<:  @<<<<<<<<
> >$name, $ssn
> >.
>
> I, for one, can see two problems with this. And I'm not an expert.

OK, these are good discussion items.  And I'm not an expert either, but I
don't see either of these problems, yet.

> Both are related to the variables.
>
>  1) With a string, there can be no compile-time check on the variables.
> If one isn't found, you can, at best, get a runtime error.

What compile time checks are you expecting on the variables?  For example:

test.pl:
format STDOUT =
@<<<<
$foo
.

write;

C:\>test.pl
Name "main::foo" used only once: possible typo at C:\my\perl\test.pl line
3.
Use of uninitialized value in formline at C:\my\perl\test.pl line 3.

So I guess the first message is due to noticing the reference to $foo at
compile time, all right, but other than counting usage, what sort of checks
are done, and would they really be missed?  The other message is a run-time
check.

>  2) What about lexical variables? Suppose that $name is a lexcial
> variable. If this format string is interpreted in another module,
> there's no way that that module can get a grasp on the lexical $name.

OK, this is an issue.  Clearly if compilation were deferred to first use,
then lexical variables would get different interpretations, either by not
being found, or the name could mean something different in the scope in
which it was compiled.

I suspect that these are relatively obscure cases, and that the multitude
of formats are created, compiled, and used in a scope in which all the
variables referenced by the formats are consistently available.

And if a format is used to reference a lexical variable that has gone out
of scope, the current behavior is somewhat useless...

test.pl:
{
my $foo;

format STDOUT =
@<<<<
$foo
.

$foo = 3;
}
$foo = 1;
write;

C:\>test.pl
Name "main::foo" used only once: possible typo at C:\my\perl\test.pl line
11.
Use of uninitialized value in formline at C:\my\perl\test.pl line 6.

Even though the lexical $foo was initialized in its scope, that same scope
in which the format was defined, because that scope was exited, it has no
value.

I suppose you could run into problems with something like:

my $foo = 3;
my $format = <<'.';
@<<<<
$foo
.
{
  my $foo = 1;
  write $format;
}

If the compilation of $format were deferred to the time the write first
uses it, a different $foo is in scope.  This would definitely be a semantic
change from today's formats.  However, there are two questions to be
addressed:

1) do perl6 formats need to have exactly the same scoping rules as perl5
formats in this regard?

2) is it a problem in practice?

None of the format code I've written would work differently do to such a
semantic change in the time of compilation, but I'm just one novice.  I
tend to think that code that uses formats is really pretty simple code...
if it were more complex, it would probably use sprintf rather than
format... as simple code, it tends not to have many scopes and reused
variable names.

If it is, p52p6 could handle this situation by renaming the inner scope's
$foo to avoid the conflict.

> --
>         Bart.

--
Glenn
=====
There  are two kinds of people, those
who finish  what they start,  and  so
on...                 -- Robert Byrne



_____NetZero Free Internet Access and Email______
   http://www.netzero.net/download/index.html

Reply via email to