BEGIN {}

1 view
Skip to first unread message

Autrijus Tang

unread,
Mar 25, 2005, 1:52:51 AM3/25/05
to perl6-c...@perl.org
So, as now Pugs generates PMC code that makes mandel.p6 run
faster than Perl 5 (http://use.perl.org/~autrijus/journal/23829),
I'm pondering this BEGIN{} mess that Pugs had not dealt with.

Consider this program, example.p6:

use v6;
my $var = BEGIN { say "I'm compiling, man"; time() }
say "Compiled at { time() - $var } seconds ago!";

Is this behaviour correct?

% pugscc --parrot example.p6
I'm compiling, man
% sleep 60
% parrot example.pmc
Compiled at 60 seconds ago!

If not, what is?

Thanks,
/Autrijus/

Rafael Garcia-Suarez

unread,
Mar 25, 2005, 3:32:13 AM3/25/05
to perl6-c...@perl.org
Autrijus Tang wrote in perl.perl6.compiler :

> Consider this program, example.p6:
>
> use v6;
> my $var = BEGIN { say "I'm compiling, man"; time() }
> say "Compiled at { time() - $var } seconds ago!";
>
> Is this behaviour correct?
>
> % pugscc --parrot example.p6
> I'm compiling, man
> % sleep 60
> % parrot example.pmc
> Compiled at 60 seconds ago!

It looks not correct to my perl 5 trained eyes.
BEGIN is designed to let things run as early as possible, mostly
for Perl programs to discover things about the environment they're
running in. That's a different environment than the environment they
were compiled in. For example :

my $use_debug_mode = BEGIN { %*ENV{DEBUGME} ?? 1 :: 0 };

On the other hand there are CHECK blocks too.

Nicholas Clark

unread,
Mar 25, 2005, 5:34:41 AM3/25/05
to perl6-c...@perl.org

The pugs behaviour is consistent with Perl 5's byte compiler/ByteLoader:

$ cat sleeper
my $var;
BEGIN { print "I'm compiling, man\n"; $var = time(); };
print "Compiled at @{[ time() - $var ]} seconds ago!\n";
__END__
$ ~/Reference/5.8.1/bin/perl5.8.1-32 ~/Reference/5.8.1/bin/perlcc5.8.1 -B sleeper
$ sleep 60
$ ~/Reference/5.8.1/bin/perl5.8.1-32 a.out
Compiled at 65 seconds ago!

If anyone wants to play with it, please note that the Perl 5 ByteLoader is
"experimental", not always reliable by 5.8.1, definitely not efficient, and
I think inadvertently broken from 5.8.3 or so onwards. Bug reports on it are
interesting, but fixing it isn't a priority. (Limited volunteer resources,
hard problem, and plenty of other things that can be done that are a better
use of time and enthusiasm)

Nicholas Clark

Markus Laire

unread,
Mar 25, 2005, 7:27:34 AM3/25/05
to perl6-c...@perl.org
Rafael Garcia-Suarez wrote:
>
> It looks not correct to my perl 5 trained eyes.
> BEGIN is designed to let things run as early as possible, mostly
> for Perl programs to discover things about the environment they're
> running in. That's a different environment than the environment they
> were compiled in. For example :
>
> my $use_debug_mode = BEGIN { %*ENV{DEBUGME} ?? 1 :: 0 };

According to S04 you'd use INIT block for that. INIT is run at run time,
ASAP. But BEGIN is run at compile time, ASAP.

My example code:
use v6;
my $compile_begin_time = BEGIN { time() }
my $compile_end_time = CHECK { time() }
my $run_begin_time = INIT { time() }
my %compile_time_environment = BEGIN { %*ENV }
my %run_time_environment = INIT { %*ENV }


(this list from http://dev.perl.org/perl6/synopsis/S04.html )

BEGIN {...}* at compile time, ASAP
CHECK {...}* at compile time, ALAP
INIT {...}* at run time, ASAP
END {...} at run time, ALAP
FIRST {...}* at first block entry time
ENTER {...}* at every block entry time
LEAVE {...} at every block exit time
KEEP {...} at every successful block exit
UNDO {...} at every unsuccessful block exit
NEXT {...} at loop continuation time
LAST {...} at loop termination time
PRE {...} assert precondition at every block entry
POST {...} assert postcondition at every block exit
CATCH {...} catch exceptions
CONTROL {...} catch control exceptions

--
Markus Laire
<Jam. 1:5-6>

Rafael Garcia-Suarez

unread,
Mar 25, 2005, 8:48:38 AM3/25/05
to perl6-c...@perl.org
Markus Laire wrote in perl.perl6.compiler :

>> my $use_debug_mode = BEGIN { %*ENV{DEBUGME} ?? 1 :: 0 };
>
> According to S04 you'd use INIT block for that. INIT is run at run time,
> ASAP. But BEGIN is run at compile time, ASAP.

Fair enough, makes sense. As long as BEGIN and CHECK blocks are run when
you evaluate code at run-time (which is not the case in perl 5 for CHECK
blocks currently)

Larry Wall

unread,
Mar 25, 2005, 11:11:17 AM3/25/05
to perl6-c...@perl.org
On Fri, Mar 25, 2005 at 02:52:51PM +0800, Autrijus Tang wrote:
: So, as now Pugs generates PMC code that makes mandel.p6 run

Yes, that's correct. The BEGIN resolves to a scalar time value at
compile time, so as far as the run-time system knows, you just said
something like

my $var = 123456.789;

Larry

Reply all
Reply to author
Forward
0 new messages