ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Re: BEGIN {...} and IO
by Ingo Blechschmidt other posts by this author
Jun 14 2005 10:57AM messages near this date
Re: Hyper-concat | Re: BEGIN {...} and IO
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!                    | 
Thread:
Ingo Blechschmidt
Nicholas Clark
Ingo Blechschmidt
Yuval Kogman

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved