Three Sort Functions

Everyone (I’d have to assume) has written the two basic sort functions

  # sort strings case-insensitively
  sub insensitive {
    return uc $a cmp uc $b || $a cmp $b;
  }


and

  # sort values numerically
  sub numeric {
    return $a <=> $b;
  }

Here are three fun—and hopefully useful—sort functions.

This first one sorts strings with numeric suffixes (such as room numbers like “White 102”) first by the string part, then numerically by the suffix. The function assumes the data has been vetted; i.e. matches $rgx_strnum. Obviously you may need to tweak $rgx_strnum to meet your specific needs; you can also add calls to uc to sort the string part case-insensitively.

  our $rgx_strnum = qr/^([A-Za-z]+)\s*(\d+)$/;
  sub strnum {
    my ($aS, $aN) = $a =~ $rgx_strnum;
    my ($bS, $bN) = $b =~ $rgx_strnum;
    return $aS cmp $bS || $aN <=> $bN;
  }

This function sort strings ASCIIbetically except blank strings sort to the end—I am always surprised how often this comes up. As above, you can add calls to uc for a case-insensitive blanks-last sort.

Send in a Perl aref to C, get back a Perl array (and using the generated XS)

This is a tutorial as much as it is a request for guidance from experienced XS/C/perlguts folks, as TIMTOWTDI, and in this case, likely, a better way.

This will show you how to pass a Perl array reference (aref) into a C function, convert the aref into a C array, work on it, then push it back onto the stack so the C function returns it as a Perl array.

It'll also show that although we bite off of Inline::C, the XS code it generates can be used in your distribution, even without the end-user needing Inline installed.

First, straight to the code. Comments inline for what's happening (or, at least, what I think is happening... feedback welcomed):

SPI bus access, analog in/out on the Raspberry Pi powered by Perl

Well, all of the learning and testing I've done with C, XS, managing bits, reading and understanding hardware datatsheets etc in the last few months is really starting to pay off, with a lot of kudos going out to many Perlers for providing guidance and help with my questions, particularly with XS and C.

We now have reliable, working Perl code to output and receive input analog signals on the Raspberry Pi. This example uses an MCP41010 digital potentiometer for the analog out, and an ADC1015 analog to digital converter for analog in. I still have two different ADCs to write code for, two more models of digital pots, and later this week I should be receiving my DACs (digital to analog converter), my GPS receiver chip, and my MCP3004/8 ADCs.

The Perl Foundation Grant Committee is looking for new members

If you're looking for ways to be more involved and have more influence, here's your chance!

The Perl Foundation's Grant Committee is looking for new members to the Committee Member position and the Committee Secretary position.

You can find more information on TPF blog.

Saying of the day...

When your hammer is Perl, every other language looks like a nail!

Dancer2 0.204003 fixes missing dependencies, improves error handling

Dancer2 0.204003 is on its way to CPAN now, and provides the following changes:

  • The CPANTS testing service reported that some dependencies for Dancer2 were not specified in the distribution. This has been corrected, and we apologize for any issues this may have caused.
  • When a route exception occurred, Dancer2 would catch the error first, and would prevent any custom exception handling from trapping the exception. There were some ugly hacks for working around this, but this fix puts things right, and lets the exception hook fire first, and then will trap the error.
  • Several changes were made to Dancer2’s Template Toolkit integration, the most significant of which being the removal of the ANYCASE option.
  • Various documentation improvements.

A big thank you to those who contributed to this release and helped get it out the door.

The full changelog is as follows:

Bit string manipulation made easy with Bit::Manip

I've been writing a lot of software lately that deals with direct hardware access (specifically analog and digital hardware for the Raspberry Pi). This means that I've had to learn some C, as well as get proficient with bit manipulation and the bitwise operators.

As part of my learning, I thought I'd write a module to do this bit manipulation for me, hence Bit::Manip was born. (There's also a Bit::Manip::PP for those who can't/don't want to use XS. It should be indexed shortly).

Here's a scenario based example of how the software can be used.

You have a 16-bit configuration register for a piece of hardware that you want to configure and send in. Here's the bit configuration

|<--------- 16-bit config register ---------->|
|                             |               |
|---------------------------------------------|
|                             |               |
|                             |               |
|<------Byte 1: Control------>|<-Byte0: Data->|
|                             |               |
|-----------------------------|---------------|
| 15 | 14 13 | 12 11 | 10 9 8 | 7 6 5 4 3 2 1 |
  __   _____   _____   ______   _____________
  ^      ^       ^        ^          ^
  |      |       |        |          |
START    |       |      UNUSED      DATA
      CHANNEL    |
              PIN SELECT

...and the bit configuration:

Perl Aventure Series Part 2

A couple weeks ago we had our first meeting for the Perl Adventure Series. Despite a terrible ice storm we still had 3 new people turn out in addition to our regular cast of characters. I hope we see them (and more) back. 

During the meetup we set up our Dist::Zilla config file, and a new GitHub repository for the adventure series. And we got started designing the mission data structure. 

We had a great time discussing all the implications of this design. There was a heated argument about including code snippets inside the config file in order to keep the game engine generic. However, I’m going to argue to the group that we should be putting these code snippets out into individual modules (or perhaps roles) and then just name those plugins in the config file. I hate the idea of code in a config file. It makes my skin crawl. 

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl and offering the modern features you’ve come to expect in blog platforms, the site is hosted by Dave Cross and Aaron Crane, with a design donated by Six Apart, Ltd.