Ingo Blechschmidt wrote:
> # No problem:
> my $data = BEGIN {
> my $fh = open "some_file" err...;
> =$fh;
> };
>
> # Problem;
> my $fh = BEGIN { open "some_file" err... };
> # Compile-time filehandle leaked into runtime!
> say =$fh;
[...]
> * There's a boolean property may_leak_into_runtime every object has,
> with a default of true.
> BEGIN and CHECK then check if the object they're about to return has
> .may_leak_into_runtime set to false -- if that's the case, die:
>
> class MyClass does may_leak_into_runtime(false) {
> method get_some_value () {...}
> }
>
> my $foo = BEGIN { MyClass.new }.get_some_value; # really means
> my $foo = BEGIN {
> my $result = MyClass.new;
> $result.may_leak_into_runtime ?? $result :: die "...";
> }.get_some_value;
>
> Pro: Great flexibility, easy to use
unfortunately, even though I really like that solution, I found a
scenario where it'll fail:
my $foo = BEGIN {
my $fh = open "some_file";
# $fh.may_leak_into_runtime is 0, so if we return $fh
# the compiler would throw an exception.
# But, instead, we create a closure:
my $code = { =$fh };
# $code.may_leak_into_runtime is 1, as Code is not an IO.
# So, this won't fail at compile-time.
};
say $foo();
# Compiler isn't able to catch this error at compile-time.
Maybe we should just hardcode the filehandles-leaking-into-runtime case
in the compiler? And, if the compiler can't detect the problem at
compile-time, just throw a runtime exception?
my $fh = BEGIN { open "some_file" };
=$fh; # "Can't readline() on unopened filehandle leaked
# from compile-time into runtime, try to..."
Opinions?
--Ingo
--
Linux, the choice of a GNU | Wissen ist Wissen, wo man es findet.
generation on a dual AMD |
Athlon! |