This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html
Set by lizmat on 8 June 2022.
deoac my $foo = 1; BEGIN { say $foo; } 03:52
#OUTPUT: (Any)
Why?
kjp Because the "$foo = 1" isn't executed until run time, and the code in the BEGIN block is run at compile time. 03:59
deoac Yet, `say $food` throws an error, not (Any). So BEGIN knows about $foo. 04:59
And `sub foo {say 'Hello'};  BEGIN { foo; };`  prints `Hello` 05:01
so why isn't the assignment recognized?
Nemokosch I think the reasoning by kjp was legit 06:41
m: constant $foo = 1; BEGIN { say $foo; } # constant is evaluated at compile time 06:43
Just because something is visually one line, it doesn't necessarily mean it is executed all at once, confer Javascript `var`s... 06:45
m: BEGIN { say $foo; }; my $foo = 1;
by the way, your subroutine example doesn't work for me 06:47
m: sub foo {say 'Hello'}; BEGIN { foo; };
lizmat deoc: only the assignment is runtime, the declaration of $foo is also compile time
Nemokosch I can imagine this depends on the way of compilation; it definitely doesn't work for me with `raku -e`
lizmat that's why the $foo is known inside the BEGIN 06:48
Nemokosch I think it's good to clarify that BEGIN is not something you often need. Ironically, it's not a beginner thing 06:51
m: my \foo = 1; BEGIN { say foo; } 07:03
lizmat m: my \foo = 1; BEGIN { say foo; } 07:06
camelia (Mu)
lizmat the reason that shows Mu is that when you declare a variable, it gets declared with a Scalar container already in it 07:07
Nemokosch I was curious about terms because they are bound at declaration 07:10