perl6

If I reassigned OUT in Perl 6, how can I change it back to stdout?

坚强是说给别人听的谎言 提交于 2019-12-06 02:26:46
问题 A very simple question, but I can't easily find an answer. I want all say in a block to go to a file. But then I want my output to return to STDOUT . How to do that? my $fh_foo = open "foo.txt", :w; $*OUT = $fh_foo; say "Hello, foo! Printing to foo.txt"; $*OUT = ????; say "This should be printed on the screen"; 回答1: The simple answer is to only change it lexically my $fh-foo = open "foo.txt", :w; { my $*OUT = $fh-foo; say "Hello, foo! Printing to foo.txt"; } say "This should be printed on the

Perl6: implicit and explicit import

╄→尐↘猪︶ㄣ 提交于 2019-12-06 01:37:28
问题 Is it possible to write a module in a way that when the module is use d with no explicit import all subroutines are imported and when it is use d with explicit import only theses explicit imported subroutines are available? #!/usr/bin/env perl6 use v6; use Bar::Foo; # all subroutines are imported sub-one(); sub-two(); sub-three(); #!/usr/bin/env perl6 use v6; use Bar::Foo :sub-one, :sub-two; sub-one(); sub-two(); # sub-three not imported 回答1: Give your subs both the special label :DEFAULT as

How can I slice a string like Python does in Perl 6?

人走茶凉 提交于 2019-12-05 19:36:06
In Python, I can splice string like this: solo = A quick brown fox jump over the lazy dog solo[3:5] I know substr and comb is enough, I want to know if it's possible, However. Should I use a role to do this? How to slice a string Strings (represented as class Str ) are seen as single values and not positional data structures in Perl 6, and thus can't be indexed/sliced with the built-in [ ] array indexing operator. As you already suggested, comb or substr is the way to go: my $solo = "A quick brown fox jumps over the lazy dog"; dd $solo.comb[3..4].join; # "ui" dd $solo.comb[3..^5].join; # "ui"

Not able to serve jupyter notebooks in binder

泪湿孤枕 提交于 2019-12-05 19:11:57
Binder project looks promising. It helps in executing notebooks in a github repository by building an executable container. I am trying to build an executable container in binder with the following Dockerfile that has Perl 6 and Python 3 kernels: FROM sumdoc/perl-6 ENV NB_USER jovyan ENV NB_UID 1000 ENV HOME /home/${NB_USER} RUN adduser --disabled-password \ --gecos "Default user" \ --uid ${NB_UID} \ ${NB_USER} RUN apt-get update \ && apt-get install -y build-essential \ git wget libzmq3-dev ca-certificates python3-pip \ && rm -rf /var/lib/apt/lists/* && pip3 install jupyter notebook --no

What's allowed in a Perl 6 identifier?

ぃ、小莉子 提交于 2019-12-05 17:03:53
问题 Synopsis 2 says: An identifier is composed of an alphabetic character followed by any sequence of alphanumeric characters. The definitions of alphabetic and numeric include appropriate Unicode characters. Underscore is always considered alphabetic. An identifier may also contain isolated apostrophes or hyphens provided the next character is alphabetic. Syntax in the Perl 6 docs says: Identifiers are a grammatical building block that occur in several places. An identifier is a primitive name,

Start REPL with definitions loaded from file

霸气de小男生 提交于 2019-12-05 16:41:34
Is there a way to start the Perl 6 REPL with definitions loaded from a file? I.e. let's say I have this in test.p6 : sub abc() { say 123; } I'd like to be able to start the perl6 REPL and load that file so that I can use abc interactively. I guess the easiest way would be to put your code in a .pm6 file, e.g. ./Foo.pm6 , mark the subroutines in question with is export and then start Rakudo Perl 6 like this: $ perl6 -I. -MFoo To exit type 'exit' or '^D' > abc 123 来源: https://stackoverflow.com/questions/45290418/start-repl-with-definitions-loaded-from-file

What happens when different thread schedulers are used in the same react block?

独自空忆成欢 提交于 2019-12-05 16:34:15
问题 This is a follow up question to is whenever signal() in react block order dependent? . The following code using the default scheduler $*SCHEDULER lets the user exit immediately by pressing CTRL-C in the following event loop: use v6; my %scheduler; my $use-default-scheduler = True; if $use-default-scheduler { %scheduler = scheduler => $*SCHEDULER; } react { whenever signal(SIGINT, |%scheduler) { say "Got signal"; exit; } whenever Supply.from-list($*IN.lines, |%scheduler) { say "Got line"; exit

Parsing binary structure with Perl6 Grammar

爷,独闯天下 提交于 2019-12-05 14:04:49
What is the best option to parse a binary structure with Perl6 . in Perl5 we have the pack/unpack methods on Perl6 they seems experimental is it possible to use Perl6 grammar to parse binary data let's say i have a file which have records on the following binary format : struct record { short int ut_type; char ut_line[UT_LINESIZE]; char ut_id[4]; char ut_user[UT_NAMESIZE]; char ut_host[UT_HOSTSIZE]; } is it possible to parse this file with Perl6 grammar ? What is the best option to parse a binary structure with Perl6? Especially given that you know about P5's pack/unpack, the new P5pack module

Passing regexes as arguments in Perl 6

别来无恙 提交于 2019-12-05 12:05:38
A continuation of this question , and probably an even more weird one. Can I e.g. concatenate two regexes using a sub ? (Of course, I understand, how to do it with a regex ) The following code is totally wrong, but I hope it can explain what I want to do: my Regex sub s12 ( $c, $v) { return / <{$c}> <{$v}> / } my regex consonant { <[a .. z] -[aeiou]> } my regex vowel { <[aeiou]> } my regex open_syllable { &s12( &consonant, &vowel ) } "bac" ~~ m:g/ <open_syllable> /; say $/; # should be 'ba' What you wrote is basically right, but you need to tweak the syntax a little. First, you should declare

How to define variable names dynamically in Perl 6?

倖福魔咒の 提交于 2019-12-05 11:58:14
I have a name which I'd like to give to a variable, in another string variable: my $name = '$a'; or simply my $name = 'a'; How to make the variable and to use it? I mean something like this (but it doesn't work): my $name = '$a'; my {$name} = 1; # Doesn't work say $a; # should be 1 A more general case. I have a list of variable names, say my @names = '$aa' ... '$cc'; How to declare and use the variable, whose name will be e.g. @names[2] ? According to documentation the lexical pad (symbol table) is immutable after compile time. Also (according to the same docs) it means EVAL cannot be used to