Hi, On 06.12.2016 00:49, CARAYOL Jean-Pierre wrote: > Hi, > > > > I don't understand why the 2 following Perl6 programs give a different > result when running. > > If someone could give me an explanation, it will help me a lot. Many thanks. > > > > /(Note the 2 programs run on Linux and the Linux environment variable > $toto is set to "totoenv". ( export toto=totenv )/ > > > _Program1 :_ > > /cattest051.pl6/_ > _ > > #!/opt/rakudo-star-2016.10/bin/perl6 > use v6; > > grammar TEST { > rule TOP { :i \s* <varenv> } > token varenv {'$'<mot> } > token mot { <[a..z A..Z 0..9 =\-_!~'():@&+$,\']>+ } > } > > class ActionsTest { > method TOP ($/) { $/.make( ~%*ENV{$<varenv><mot>}); } > } > > my $actions = ActionsTest.new; > my $script = "\$toto" ; > my $scr = TEST.parse($script,:$actions); > say $scr; > say $scr.made; > ===>./test051.pl6 > 「$toto」 > varenv => 「$toto」 > mot => 「toto」 > *totoenv* > ---------------- > This programs runs fine and displayed the value for the toto environment > variable. > > > > _Program2:_ > > __cat test052.pl6 > > grammar TEST { > rule TOP { :i \s* <varenv> } > token varenv {'$'<mot> } > token mot { <[a..z A..Z 0..9 =\-_!~'():@&+$,\']>+ } > } > > class ActionsTest { > method varenv ($/) { $/.make( %*ENV{$<varenv><mot>}); } > method TOP ($/) { $/.make( ~$/); }
$/.make stores the result in $/.made, so the second line should be method TOP($/) { $/.make($<mot>.made) } HTH, Moritz > } > > my $actions = ActionsTest.new; > my $script = "\$toto" ; > my $scr = TEST.parse($script,:$actions); > say $scr; > say $scr.made; > > ===> > Use of Nil in string context > in method varenv at ./test052.pl6 line 14 > 「$toto」 > varenv => 「$toto」 > mot => 「toto」 > $toto > ---------------- > This program displays an error message and doesn't find the value of > toto environment variable. > > > The only difference between the 2 programs is instruction "make( > %*ENV{$<varenv><mot>}" is part of the TOP method in the program 1 and > part of the varenv method in program 2. > > I'm certainly missing something but I don't understand why program 2 > doesn't work. I'm very interested to get explanations for that. It will > help me to understand how to modify and AST using Perl6. > > > Thanks a lot for your help. > > Best regards, > > Jean-Pierre > > -- Moritz Lenz https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/