Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a variable in a Perl 6 program before assigning to it

Tags:

raku

I want to assign literals to some of the variables at the end of the file with my program, but to use these variables earlier. The only method I've come up with to do it is the following:

my $text;

say $text;

BEGIN {    
    $text = "abc";
}

Is there a better / more idiomatic way?

like image 788
Eugene Barsky Avatar asked Dec 18 '22 00:12

Eugene Barsky


2 Answers

Just go functional.

Create subroutines instead:

say text();

sub text { "abc" }


UPDATE (Thanks raiph! Incorporating your feedback, including reference to using term:<>):

In the above code, I originally omitted the parentheses for the call to text, but it would be more maintainable to always include them to prevent the parser misunderstanding our intent. For example,

say text();          # "abc" 
say text() ~ text(); # "abcabc"
say text;            # "abc", interpreted as: say text()
say text ~ text;     # ERROR, interpreted as: say text(~text())

sub text { "abc" };

To avoid this, you could make text a term, which effectively makes the bareword text behave the same as text():

say text;        # "abc",    interpreted as: say text() 
say text ~ text; # "abcabc", interpreted as: say text() ~ text()

sub term:<text> { "abc" };

For compile-time optimizations and warnings, we can also add the pure trait to it (thanks Brad Gilbert!). is pure asserts that for a given input, the function "always produces the same output without any additional side effects":

say text;        # "abc",    interpreted as: say text() 
say text ~ text; # "abcabc", interpreted as: say text() ~ text()

sub term:<text> is pure { "abc" };
like image 125
Christopher Bottoms Avatar answered May 23 '23 19:05

Christopher Bottoms


Unlike Perl 5, in Perl 6 a BEGIN does not have to be a block. However, the lexical definition must be seen before it can be used, so the BEGIN block must be done before the say.

BEGIN my $text = "abc";
say $text;

Not sure whether this constitutes an answer to your question or not.

like image 25
Elizabeth Mattijsen Avatar answered May 23 '23 21:05

Elizabeth Mattijsen