Rosetta Code/Run examples: Difference between revisions

→‎{{header|Perl 6}}: Make more language agnostic, various tweaks
m (→‎{{header|Perl 6}}: Better newline spacing)
(→‎{{header|Perl 6}}: Make more language agnostic, various tweaks)
Line 265:
This is a fairly comprehensive task code runner. It is set up to work for Perl 6 only at this point, but could be easily tweaked to work with other languages. There is so much variation to the task requirements and calling conventions that it would be problematic to make a general purpose, language agnostic code runner. (Heck, a single language one is hard enough.)
 
By default, this will download the Perl 6 section of any (every) task that has a Perl 6 example, extract the code blocks and attempt to run them. Many tasks require files or user interaction to proceed, others are not complete runnable code blocks (example code fragments), some tasks run forever. To try to deal with and compensate for this, this implementation can load a %resourcesresource hash that will: supply input files where necessary, skip unrunnable code fragments, limit long and/or infinite running blocks, supply user interaction code where possible, and skip blocks where user interaction is unavoidable.
 
The complete implementation is too large and cumbersome to post in it's entirety here, only the main task retrieval and execution code is included.
 
For the whole ball of wax see: [https://github.com/thundergnat/rc-run Thisthe rc-run github repository].
 
Run with no parameters to run every implemented task on Rosetta Code. Feed it a task name to only download / run that task.
Line 278:
use URI::Escape;
use JSON::Fast;
use Sort::Naturally;
use MONKEY-SEE-NO-EVAL;
 
Line 284 ⟶ 283:
my $url = 'http://rosettacode.org/mw';
 
# Language specific variables
my $lang = 'Perl_6'; # language
my $exe language = 'perl6Perl_6'; # executable to run perl6 in a shelllanguage
my $view header = 'xdg-openPerl 6'; #imager viewer, this will openheader defaulttext
my $exe = 'perl6'; # executable to run perl6 in a shell
my $ext = '.p6'; # file extension for perl6 code
 
# weird break in lang tag to placate terminally confused wiki formatter and syntax highlighter
my @blocks$tag = $perl6.comb(rx/<?after '<lang perl6>'> .*? <?before '</' 'lang>'> /); # language entry regex
 
my $view = 'xdg-open'; # image viewer, this will open default under Linux
my %resource = load-resources();
my $download = True;
Line 293 ⟶ 299:
 
run('clear');
note 'RetreivingRetrieving tasks';
 
if @*ARGS {
Line 304 ⟶ 310:
$url, 'pages',
:generator<categorymembers>,
:gcmtitle("Category:$langlanguage"),
:gcmlimit<350>,
:rawcontinue(),
Line 322 ⟶ 328:
my $page = $client.get("{ $url }/index.php?title={ uri-escape $title }&action=raw").content;
say "Whoops, can't find page: $url/$title :check spelling." and next if $page.elems == 0;
say "Getting code from: http://rosettacode.org/wiki/{ $title.subst(' ', '_', :g) }#Perl_6$language";
 
my $perl6entry = $page.comb(/'=={{header|Perl' 6$header '}}==' .+? [<?before \n'=='<-[={]>*'{{header'> || $] /).Str // whoops;
 
if $perl6entry ~~ /^^ 'See [[' (.+?) '/Perl_6' $language / {
$perl6entry = $client.get("{ $url }/index.php?title={ uri-escape $/[0].Str ~ '/Perl_6' ~ $language }&action=raw").content;
}
 
my $name = $title.subst(/<-[-0..9A..Za..z]>/, '_', :g);
 
my $dir = mkdir "./rc/tasks/$name";
 
spurt( "./rc/tasks/$name/$name.txt", $perl6entry );
 
my @blocks = $entry.comb: $tag;
# weird break in lang tag to placate terminally confused wiki formatter and syntax highlighter
 
my @blocks = $perl6.comb(/<?after '<lang perl6>'> .*? <?before '</' 'lang>'> /);
unless @blocks {
if %resource{"$name"}<skip> {
whoops;
say "Skipping $name: ", %resource{"$name"}<skip>, "\n";
}
}
 
for @blocks.kv -> $k, $v {
my $n = $k+@blocks >== 01 ?? $k'' !! ''$k;
spurt( "./rc/tasks/$name/$name$n.p6$ext", $v );
say "Skipping $name$n: ", %resource{"$name$n"}<skip>, "\n"
and next if %resource{"$name$n"}<skip>;
Line 365 ⟶ 377:
sub run-it ($dir, $code) {
my $current = $*CWD;
chdir "./rc/tasks/$dir/";
if %resource{$code}<file> -> $fn {
copy "./../../resources/{$fn}", "./{$fn}"
}
my @cmd = %resource{$code}<cmd> ?? |%resource{$code}<cmd> !! "$exe $code.p6$ext";
for @cmd -> $cmd {
say "\nCommand line: $cmd";
Line 388 ⟶ 400:
 
{{out}} with command line: '''perl6 RC-run.p6 "Determine if a string is numeric"'''
<pre>RetreivingRetrieving tasks
1 Determine if a string is numeric
Getting code from: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Perl_6
Line 420 ⟶ 432:
Done Determine_if_a_string_is_numeric
===============================================================================</pre>
Or, if the full %resourcesresource hash is loaded it will autmaticallyautomatically feed input parameters to tasks that require them:<br>
'''perl6 RC-run.p6 Lucky_and_even_lucky_numbers'''
<pre>RetreivingRetrieving tasks
1 Lucky_and_even_lucky_numbers
Getting code from: http://rosettacode.org/wiki/Lucky_and_even_lucky_numbers#Perl_6
10,339

edits