Continues from previous week.
Feel free to submit a merge request or open a ticket if you found any issues with this post. We highly appreciate and welcome your feedback.
For a quick overview, go through the original tasks and recap of the weekly challenge.
Task #1
CPAN modules used: Data::Dumper
, Function::Parameters
, Inline
, JSON
, List::Util
, Modern::Perl
, Parse::RecDescent
, Test::Deep
, Test::More
, Text::Glob::Expand
, feature
, lib
, strict
, utf8
, warnings
For a good write-up of the problem, read the explanation of the problem at Rosetta Code.
Let’s start with the shortest and simplest solution. Look into the submission by Ulrich Rieke. The solution works as provided there is no whitespace within the curly braces. Note that curly braces was not passed to the Perl script as it have already parsed by the shell.
#!/usr/bin/perl ;
use strict ;
use warnings ;
#works only if there is no whitespace within the expansion bracket!
my $howoften = @ARGV - 2 ;
for my $i (1..$howoften) {
print "$ARGV[0] $ARGV[ $i ] $ARGV[-1]\n" ;
}
The next group of solution resort to using regex to parse and expand values within the curly braces. And it should wworks (quick glance of the code but we did not test each and every one) with spaces between items within the curly braces. Adam Russell, Dave Cross, Laurent Rosenfeld, Prajith P, Rage311, Lars Thegler, Jaldhar H. Vyas, Steven Wilson and Yet Ebreo.
Only two participants, Duane Powell and Lubos Kolouch decided to use Text::Glob::Expand
to solve this. Similarly, the solution by E. Choroba works in similar manner but using glob
function instead. Coming from E. Choroba, the test cases were always comprehensive. While optional, we do encourage participants to submit their test cases or output result together with their solution.
For more complex or unique approach, look into the solution by Colin Crain which supports nested braces, Andrezgz which can handle numerical and alphabet range, Duncan C. White which manages the brace expansion using a state machine, Kivanc Yazan which can work with blank input and nested braces, Roger Bell_West which should work correctly with nested braces and lastly Joelle Maslak which demonstrated that Parse:RecDescent
CPAN module was as good as Grammar in Raku.
Special mentioned of the submission by Burkhard Nickels. His submission, by far, was probably the most comprehensive submissions we’ve seen. Everything (solution for Task #1 and Task #2, blog, comments) have been combined together within one Perl script. There were were six variations of handing the brace expansions. Furthermore, some solutions were a combination of both Task #1 and Task #2.
Task #2
CPAN modules used: Benchmark
, FFI::Platypus
, FFI::Raw
, Function::Parameters
, Inline
, Memoize
, Modern::Perl
, Test::More
, constant
, feature
, lib
, strict
, warnings
The most common approach was to use the Inline
CPAN module which allows writing Perl subroutines in other programming languages, or for this case C. This was used by Duane Powell, Dave Jacoby, Dave Cross, Ruben Westerberg, Burkhard Nickels, Lars Thegler, Kivanc Yazan, Lubos Kolouch, Yet Ebreo, Duncan C. White, Andrezgz, Colin Crain, E. Choroba and Joelle Maslak. In short, almost everyone use this way as it’s the easiest and quickest way to integrate external programming languages into Perl.
For those using Perl binding approach through a low-level Foreign Function Interface (FFI), Rage311 picked the FFI::Platypus
CPAN module while Roger Bell West ended up with FFI::Raw
CPAN module.
Another approach was through Simplified Wrapper and Interface Generator (SWIG) which was observed in the solution by Adam Russell and Jaldhar H Vyas. Simplicity compare to XS was why both participant picked this approach.
Again, for different approaches used for this task, look no further other than the solutions by Burkhard Nickels.
SEE ALSO
(1) Bracen C with Perl 6 by Arne Sommer
(2) Perl Weekly Challenge 029 by Adam Russell
(3) Code “Reuse” and Perl Weekly Challenge #29 by Dave Jacoby
(4) Perl Weekly Challenge 29: Brace Expansion and Calling C Code by Laurent Rosenfeld
(5) Perl Weekly Challenge: Week 29 by Jaldhar H Vyas
(6) RogerBW’s Blog: Perl Weekly Challenge 29 by Roger Bell_West
(7) Perl Weekly Challenge W029 - Brace Expansion by Yet Ebreo
(8) Perl Weekly Challenge W029 - Calling C by Yet Ebreo
(9) Perl Weekly Challenge 029: Brace expansion and calling a C function by E Choroba