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 13 2005 11:17AM messages near this date
Re: State of Design Documents | Anonymous macros?
Hi,

chromatic wrote:
>  On Mon, 2005-06-13 at 17:07 +0200, 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;
>  
>  Perhaps I'm being very naive, but why is this a problem?  Maybe it's
>  not the best way to do something, but I can see it being useful in
>  some circumstances.

sorry I was unclear, the problem is:

Consider you want to first compile a program to a .pbc on host1, and
then run it on host2:

  host1 $ echo hi >  some_file              # Make sure "some_file"
                                           # exists on host1.
  host1 $ cat >  program.p6
  my $fh = BEGIN { open "some_file" err... };
  say =$fh
  ^D
  host1 $ pugs -o program.pbc program.p6   # Hypothetical syntax
  # "some_file" is opened at compile-time, and an IO handle object
  # is returned. $fh contains an IO object now.
  # In the resulting program.pbc, there's no call to open() [1].
  host1 $ scp program.pbc host2:.          # Copy program.pbc to host2

  # "some_file" does not exist on host2:
  host2 $ ls -l some_file
  ls: some_file: No such file or directory
  host2 $ parrot program.pbc
  # XXX! Tries to readline() from an invalid filehandle!
  # Additionally, "some_file" doesn't exist on host2...

Do you see the problem? IO objects are only valid in the same process.
But the compiler process and the execution process are/may be
decoupled, or may even be on different computers.


--Ingo

[1] Similar as in this example:
      my $compiled_at = BEGIN { time };
      say "I was compiled at $compiled_at seconds after the epoch.";
    If you compile this to a .pbc, there won't be a call to time(),
    but the whole BEGIN block is substituted by the return value of
    time(). So, what the Perl 6 compiler will actually compile to
    PIR is:
      my $compiled_at = 172001560.613011;
      say "I was compiled at $compiled_at seconds after the epoch.";
    (Of course, a smart compiler can optimize this further.)

-- 
Linux, the choice of a GNU | To understand recursion, you must first
generation on a dual AMD   | understand recursion.
Athlon!                    | 

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