Keep Calm, Roll on.

In my last post I left off with asking myself a whole lot of design questions after I decided to centralize most if not all of my 'Game/Business Rules' into 'Roles' under the logical namespaces. In the end I can just live with what I came up with even if there is some duplication of code I don't think I am in anti-pattern land yet.

kkrac.jpg

Well now I think I will get back on track from where I left on with this post and that is filtering out 'Player Classes' once I select a 'Race'. I had the basics done but now there are a few more little things to add in and we will start with a cool one racial adjustment.

Some races get an adjustment to their ability scores and these adjustments are used when selecting a 'Class' So an 'Elf' with '8' dexterity roll get a +1 so they will be able to become a thief.

ZMQ::FFI and FFI::Raw

A few months ago I released ZMQ::FFI, zeromq bindings for Perl that use FFI::Raw instead of XS. There's been some interest, so I thought I would write a short post to announce the module beyond the zeromq mailing list.

In another post I'll discuss FFI::Raw itself, what it currently provides, as well as some advanced use cases that came up during the development of ZMQ::FFI. If reverse depends on metacpan is anything to go by, it seems many people aren't aware of this module. In fact, at the time of this writing ZMQ::FFI is the only module using it:

ffi-raw-reverse-depends.png That's a shame, as it's a terrific module which finally makes FFI in Perl feasible. It effectively eliminates the need for XS in many cases, and it's sufficiently baked to be considered "production ready."

Tech Tip: Opening a file for read/write without clobbering it.

The Tip

perlopentut gives several options for opening a file for read and write, and I opted to use '+>' for Maniac Downloader, but as it turned out, it caused the existing file to be clobbered (= its length to be set to zero and all of its contents deleted).

After asking on Freenode's #perl channel, we reached this solution for opening a filehandle so that: 1) it will be opened for read/write. 2) it can open both an existing file or create a new file that does not exist. 3) If the file exists, its contents will be retained, and it won't be clobbered. and 4) it will overwrite existing contents, and not append everything at the end of the file.

The code is as follows:

A repl site for a lot of languages

repl.it

Source code available on GitHub. Who will add Perl support to it?

Rule Role's or Role's Rule?

In my last post I used a role called 'MaxLevel' to define a small set of game rules that define both the 'Player Classes' and 'Maximum Player Level' a race can obtain. That got me thinking that perhaps I could improve my 'Race' class a little by getting rid of the 'ability_mins' attribute and moving it into a role.

player-character-deaths-d-amp-d-dungeons-dragons-character-d-demotivational-poster-1242486691.jpg

After a little more head scratching what I decided on was to reorganize my namespace a little so all the 'Game Rules' or 'Business Rules' if you like will be in logical places. So all rules for a 'Race' are in its folder and all 'Races' have the same set of rules but with slightly different code.

So step one I converted my 'ability_mins' into a role like this.

Testing Random Dice Rolls

I've decided to rewrite my Perl testing course from scratch and hit upon an interesting problem that is outside of the scope of the course, but is a perfect fit for this site: testing dice rolls. I have the the following function as part of a student lesson in the course:

Regular Expression Library

Regular Expression Library :

[From my blog.]

Lexical closures with END blocks warn, but Just Work

I was just impressed by the awesomeness of Perl. More testing revealed to me that "use 5.018" does not invoke "use warnings", which dampened my enthusiasm a little. But Perl is still pretty awesome. Here's the situation.

I write lots of Prima GUI applications. In this one (a presentation if you can believe that), I wanted a timer so I could see how long my talk was going. I had just run through part of my lecture and meant to check my time before closing (the clock is discretely placed in a separate window), but forgot to check before quitting the talk. Frustrated, I decided that I wanted my program to print the talk duration when it was finished.

My normal approach would have been to declare a file-scope lexical that gets initialized at startup, and refer to that in my END block. But instead, I decided to embed the END block within the initialization function itself.

A Class Without Class

Well in this entry I am going go to the next step and this time enter a 'Race' into the instantiation arguments of my Creator and it should then give me a list of 'Player Classes' that are available to the entered Ability rolls and Race selection.

Of course I will still have to the same checks for 'Grot' and 'Race' as I do not want someone to sneak something in by simply adding a 'Race' to the mix.

0227_LARP-592x323.jpg

So like my LARP party above lets start with a new code in my 'Creator' class's 'BEGIN';

Tie::File don't while(<>)

Consider the common case where you want to take a text file and walk it doing various operations such as stripping whitespace, looking for duplicates, sorting or other common operations. I was recently asked to work on a program where part of the process is 'cleaning' uploaded files before doing other things with it. This process was over 40 lines of subs opening old and new filehandles, doing the standard while(<>) {} and then renaming new file to old file. If you find yourself wanting to do something you would normally do on an array, but on a file and often in place, consider Tie::File before while()ing away at it. The documentation has many examples and there are several examples on the web including at Perl Monks. In this case I was able to whittle this down to this:

TPF Grant Progress Report: January 2014

This past May, The Perl Foundation awarded a grant to fund development of a couple features in Pinto. Pinto is a robust tool for curating a private repository of CPAN modules, so you can build your application with the right modules every time. This is my fourth progress report on that work.

I've done quite a lot of work on Pinto the last few weeks. And today, I pushed a development release to CPAN that has a lot of really cool features and improvements. However, none of this work is tied to the TPF grant.

But now the backlog is cleared up a bit, so I hope to finally finish off the grant work. Look for another update next month.

PS: I'll have a lot more to say about the new Pinto features when I make a production release in a week or so. For a sneak peek, look at the change log.

PHP puzzle

So I've decided to make a little PHP puzzle.

The following program contains 3 test cases that you need to make pass. To do that, you should replace XXX in the assignments to $a, $b and $c by the right magic values.

A Race to the Finish

Well in my last post I made use of the visitor pattern so sort out 'Grot' characters at an early stage. My next challenge is to set the race of my 'Character' from one of the ones below.

RPG_Races_proportions_by_Artigas.jpg

Well not all of these just the original 7 A D&D races. I could try my visitor pattern again but part of what I want to build into this system is the ability to eventually expand into the odd miss-mash of races above. So I could go back and attempt to do this with parametrized roles like I tried to do with Grot character as in this post but what I really want is to load in my 'Ability' rolls and then have it tell me what 'Race' I could be rather then attempting to load all those roles.

I guess I could use a simple given when in sub on my 'Creator' class but then I would have to recode things if I added a new 'Race' into the mix.

Want to hire me, or Dave Cross or Michael Schwern?

So far our company has been growing quickly and I'm quite pleased with that. I'm doubly pleased to announce that Dave Cross and Michael Schwern have joined us as trainers and consultants. Read the announcement here.

perlsphere.net: Static since last Friday

Anyone know what the problem is?

perlsphere.net

I hate Dist::Zilla and missing Makefile.PL

It is f*** annoying to clone the repository with only dist.ini and missing Makefile.PL or Build.PL.

It is more annoying to run dzil build and see

Required plugin bundle [@YETANOTHER] isn't installed.

I don’t want to install 300 more packages only because I need this only one small module which I cloned from git repository. I can’t install 300 more packages for Dzil because I do it on Android where there is no Dzill because I need to fix some modules which I can’t fix because of missing Dzil. Damn it.

Dist::Zilla: real Catch-22

Fortunately usually I can prepare tiny stub with Makefile.PL:


# Very simple stub because of damn Dist::Zilla
use ExtUtils::MakeMaker;
WriteMakefile( 'PL_FILES' => {}, 'VERSION' => '0.01', 'EXE_FILES' => [] );

Stupid? If it’s stupid and it works, it’s not stupid. ExtUtils::MakeMaker beats Dist::Zilla.

No Grot is Good Grot

On one of my earlier posts I mentioned a Grot character, in other words a set of ability rolls that can not result in a playable character.

dice_and_men.jpg


After a few dead ends and a little frustration I think I can now use the visitor pattern to check and see if I have a Grot before I go forward.

So All I really need to do is add into each of my 'Ability' classes the rule that applies for it. Using good old strength again the rule is a roll under 6 means you can only be a MU (Magic User) as well if you are going to me a MU you must have intelligence over 8. So in each 'Ability' class I have an 'is_grot' which tests for its grot condition. So in each of my Abblitiy classes I add something like this

The Simplest Exporter?

I know this package is on the CPAN, but I can't find it. I'm revising some training material for my Perl testing course and want a simpler exporter primarily so that it can fit on my slides easier. What I currently have is:

package Some::Package;
use Ovid::Exporter qw(sub1 sub2);

sub sub1 { ... }
sub sub2 { ... }

And you would use it as you would expect:

use Some::Package qw(sub1 sub2);
# or
use Some::Package ':all';

The code looks like this:

German Perl Workshop 2014 - Payment now possible - T-70 days

We have enabled the Act payment functionality for GPW2014. You can now pay for your tickets and receive your invoices. As a matter of fact we have now finalized our ticket prices.

If you want to take advantage of the early bird rebate you have to hurry. This phase ends at the end of January 2014.

We would also like to thank STRATO AG for sponsoring us.

Since our talk schedule is full we are now closing submissions. Lightning talks can still be submitted via email or during the workshop.

DE: http://www.perl-community.de/bat/poard/message/172983

Tool tip: looking at a module's source

I've got a script for looking at locally installed modules, used thusly:

    % pmvi Net::FTP

I use this a lot, since I often find myself wanting to check exactly what a module is doing, either to learn from it, because the doc isn't clear, or I have reason not to trust the doc, or curiosity, or ...

Here's the script:

    #!/bin/sh
    /usr/bin/view `mpath $*`

view is a symlink to vim, and mpath comes with my Module::Path module.

What's your tool tip?

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.