Perl Weekly Challenge 029

Part 1

Sample Run

$ perl "Perl {Daily,Weekly,Monthly,Yearly} Challenge"
Perl Daily Challenge
Perl Weekly Challenge
Perl Monthly Challenge
Perl Yearly Challenge

What I Did

There's probably a few ways to solve this that I didn't consider, however, I think in this case regular expressions are the way to go. everything before, inside, and after the braces are captured with the regex and then printed.

Part 2

Sample Run

$ perl -I. -Iblib/arch/auto/pwc 10000

What I Did

Whenever I need to interface Perl and code written in another language I greatly prefer SWIG over any other option. The Inline modules require a compiler to be present on the system running the code and in my experience many environment forbid compilers being installed on production systems for security reasons so I've never considered them a real option. XS is fine if you're only looking to integrate a C or C++ code base with Perl but then you'd end up having to use SWIG for anything else anyway. I previously wrote on interfacing C++ and Perl and what I did here is mostly the same, but for a much smaller problem. The challenge asked only to demonstrate calling a C function so here I decided to call the sqrt() function from math.h. The file pwc.i described the interface between Perl and the C functionality we which to wrap. In that file we are telling SWIG to generate a Perl module named which wraps sqrt() from math.h. When we run the command

$ swig -perl pwc.i

SWIG will generate several files, most importantly are and pwc_wrap.c. The Makefile.PL shown above will further automate the actual building of the C code. That is the following commands

$ perl Makefile.PL
$ make

will build the library in a blib directory. And that's it! As shown above be sure to include the SWIG generated library if it's in a location outside of your @INC. If you are writing something that you'd like to keep around then consider further running

$ make install

as you would expect to do for installing any module.

Comments for this post were locked by the author