I understand what's happening. See
<https://rt.perl.org/Ticket/Display.html?id=120928>. I'm not convinced
it's a bug.

Explaining again here just in case I was too vague in the bug report.

1. Variables lead double lives. There's the "ghost world" static
version of a variable, that existed since the beginning of time and is
the final fallback during variable lookup (instead of the runtime
dying). Then, there's one "live" copy of the variable for every time a
code block is entered. In the case of the mainline and %h, there's one
such copy.

2. By the time the macro is called, the program hasn't started yet.
Hence only the "ghost world" %h exists. It is to that one the
assignment is made. (As evidenced by the &diag macro in the bug
report.)

3. Then the program starts. The "live" %h is created. It is inspected
and found empty, because no-one has ever assigned to it.

I agree that the behavior is surprising. Unfortunately, it's fairly
consistent too. Instead of taking sides, I'm going to think deeply
about it, and maybe blog about it too.

It does remind me of a similar problem with roles. Not sure we have a
reference for that problem anywhere (so maybe that deserves a blog
post too), and I'm too un-coffee'd to write it up, but it's
essentially the same difficulty, solved through a hack. In the short
term, maybe the %h problem needs to be solved through a hack, too. In
the long term, we should round up all these similar problems and find
some common way to solve them all.

// Carl

On Sun, Dec 29, 2013 at 2:25 AM, Richard Hainsworth
<rnhainswo...@gmail.com> wrote:
> I was trying out macros and run into an anomaly. Not sure what is happening.
>
> I tried a macro snippet as a standalone script, then tested exactly the same
> in REPL. It worked in REPL, as in the hash variable was changed. But the
> hash variable stayed the same in the stand alone. What am I missing?
>
> Here is a paste from my terminal.
>
> $ cat macro-test.p6
> my %o;
> macro attr ( $nm, $val ) { quasi { %o{ {{{$nm}}} } = {{{$val}}}  } }
> %o.say;
> attr 'fst', 42;
> %o.say;
> $
> $ perl6
>> my %o
> ().hash
>> macro attr ( $nm, $val ) { quasi { %o{ {{{$nm}}} } = {{{$val}}}  } }
> Nil
>> %o.say
> ().hash
>> attr 'fst', 42
> 42
>> %o.say
> ("fst" => 42).hash
>> ^C
> $ perl6 macro-test.p6
> ().hash
> ().hash
> $
> $ perl6 -v
> This is perl6 version 2013.11 built on parrot 5.9.0 revision 0
>
>

Reply via email to