On 9/4/22 04:23, Ralph Mellor wrote:
On Sun, Sep 4, 2022 at 5:07 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:

For the fun of it, I placed a "booboo;"

Interesting!

You might like to think of `BEGIN` as a signal to the "compiler":

"Please do more than just "compile" this code. Please also run it,
right now, before "compiling" any more code.".

----

Thus, when the "compiler" "compiles" the code below it will also "run"
enough of it to display "But this does".

     say "This doesn't appear";
     BEGIN {
       say "Nor this";
       booboo;
       BEGIN say "But this does";
     }}^%^& no matter what nonsense is here or below:

     BEGIN !& }}!!%^&

The "compiler" will then display "===SORRY!===" as it gets to the end
of the first outer `BEGIN` and realizes `booboo` hasn't been post declared
(so hasn't been declared at all).

At that point the "compiler" has already compiled `say "Nor this";` but it
does not run it. This is so despite that `say` having appeared in a `BEGIN`.
That's because the lack of a `booboo` declaration in that outer `BEGIN`
block is considered so important that the "compiler" immediately gives up
doing anything more whatsoever -- so no more compilation *or* running.

Once you understand it's just the "compiler" going sequentially through
the source code, doing what the code tells it to do, and recognizing that
code like `say 42` is telling the "compiler" to display `42` when that line
of code is supposed to run, and recognizing that `BEGIN say 42` tells
the "compiler" that the time when that code is supposed to run is ASAP,
it'll hopefully all seem as simple and nice as it in fact is.

----

I've "scare-quoted" "compiler", "compiles", and "run" in the above because:

* The Rakudo "compiler" is actually a combined compiler+runner

* In Raku, "compiles" includes "running" code that "runs" at "compile-time"

* "run" means executing code, but that can happen during "compilation"

(Perl has a similar scheme -- it has a similar `BEGIN` construct.)

(In theory, Raku code that's already been compiled ahead of time, and all
that's left is to run it, should significantly outperform Perl. In
practice, getting
to the point that this is generally true is a work in progress.)

--
raiph

Hi Raiph,

Awesomely well written!  What a treat!  Thank you!
Are you a technical writer?

The BEGIN gets me around the slow start up of Raku
programs.  I use it for a splash screen (zenity).

And if I do turn over a program to a customer that
start up speed is an issue, a splash screen will
get me around them thinking the flubbed the double
click and clicking multiple more times and, if
running Windows, crashing their computers.  Not
to mention taking FOREVER for seven copies of
the program to start up.

A good test for BENIN and error after that would be
to insert some Perl 5 code:

   BEGIN {
   }  /\\\/\/\\/\/\\

Aught to do it.

(That was an Perl 5 joke.  No one trying
figuring it out.  It is just nonsense.)

-T



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to