C++11 lambdas

Today I played around with some of the C++11 features and stumbled across a small pitfall one could easily fall in when using lambdas. Lambdas are actually pretty cool and it's about time they are available in C++. You now can basically define an anonymous function where you need one.

#include <iostream>

int main(int argc, char **argv)
{
  auto bar = []{ std::cout << "Moo!" << std::endl; };
  bar();
}

We define an anonymous function which prints Moo! to stdout and call it with bar();.

You can also capture variables and use them inside your lambda. Capturing can be done by value or by reference.

#include <iostream>

int main(int argc, char **argv)
{
  int foo = 23;
  auto bar = [&foo]{ foo = 42; };
  auto baz = [ foo]{ std::cout << foo << std::endl; };

  bar();
  baz();
}

foo is initially set to 23. We call bar() which did capture foo by reference and set foo to 42. Then baz() is called which did capture foo by value. By value means it has a copy of foo (which is const btw). So what do you think baz() will print to stdout? 23 or 42?

Since that's the pitfall I talked about: it prints 23. Because baz() captured the value of foo when the lambda was defined and at this time foo was still 23. We didn't call bar() before we defined baz().

April 23, 2013 | c++11

CPAN Smoker

Here is an overview of the setup I use to run several virtual CPAN smokers.

libvirt

I have 1 dedicated server which runs the VMs using libvirt and KVM. To manage the VMs (create new ones, start/stop, etc.) I use virt-manager and connect to the libvirtd through a ssh tunnel.

                    +--------------+
                    | virt-manager |
                    +--------------+
                           |qemu+ssh
                           v
/--------------------------------------------------------\
|                       libvirt                          |
+--------------------------------------------------------+
|                         KVM                            |
+--------------------------------------------------------+
|/---------\ /--------------\ /-----------\ /-----------\|
|| OpenBSD | | DragonFlyBSD | | Windows 7 | | Windows 8 ||
|\---------/ \--------------/ \-----------/ \-----------/|
\--------------------------------------------------------/

Because the VMs don't provide any services they are behind a NAT and not reachable from outside.

Smoker setup

metabase-relayd

The host runs a metabase-relayd. All CPAN test reports are send to this server and then relayed to cpantesters.org.

Install metabase-relayd:

% cpan metabase-relayd

Create the config file in ~/.metabase/relayd

address=192.168.122.1,127.0.0.1
idfile=/home/cpan/.metabase/id.json
port=8090
multiple=1
submissions=10
offline=0
url=http://metabase.cpantesters.org/api/v1/

If you don't have an idfile, you might want to create one:

% metabase-profile

Start the relay server:

% metabase-relayd

bootstrapping the smoker

First thing to do if you plan to run a smoker on a Windows machine is to install Perl. I use StrawberryPerl. All other OSs should have Perl already installed. But you'll need some additional Modules. Here is a list of the Modules I install before starting:

This list is mostly from wiki.cpantesters.org but might be a little outdated. Just try to install often used modules, so they won't be build every time a tested module depends on it.

Create the CPAN::Reporter config in ~/.cpanreporter/config.ini

edit_report=default:ask/no pass/na:no
email_from=Your Name <your@email.address>
send_report=default:ask/yes pass/na:yes
transport=Socket host 192.168.122.1 port 8090

This will send all test reports using Test::Reporter::Transport::Socket to the metabase-relayd we set up earlier. Now activate the sending of test reports:

% cpan
cpan> o conf init test_report
cpan> o conf commit
cpan> bye

Now everything is in place and if your metabase-relayd is up and running you can test your first module:

cpan> force test Acme::rafl::Everywhere

If all goes well you should see your test after a few minutes in the logtail and you can start smoking:

% perl -MCPAN::Reporter::Smoker -e start

I run this from within a screen session.

distroprefs

There are some modules which prompt for input, hang or crash on some platforms. You can disable the smoke testing of those modules or give them the right input using distroprefs. These are basically just yaml files which include rules for every module which needs special attention.

Search github for distroprefs and clone one of the repos to the disk of your smoker so you have a starting point. Then configure the prefs dir in cpan:

cpan> o conf init prefs_dir
cpan> o conf commit
April 14, 2013 | cpan, perl, kvm, libvirt

Update symlinks to always point to the newest file in folder

Say you have a directory as the following:

% tree .
.
├── foo.1
├── foo.10
├── foo.11
├── foo.2
├── foo.3
├── foo.4
├── foo.5
├── foo.6
├── foo.7
├── foo.8
├── foo.9
├── moo.1
├── moo.10
├── moo.11
├── moo.2
├── moo.3
├── moo.4
├── moo.5
├── moo.6
├── moo.7
├── moo.8
└── moo.9

… and you want to create 2 symlinks somewhere else so that they point to the files with the highest number for each file name. There will be two links foo, which points to foo.11, and moo, which points to moo.11. These symlinks should be updated by a cronjob.

Here is a small and probably dirty shell script, when run as a cronjob is doing exactly this.

#!/bin/sh

FILE_BASE=foo
SOURCE_FOLDER=/home/wose/tmp/source
TARGET_LINK=/home/wose/tmp/target/foo

HIGHEST_NUMBER=$(find "$SOURCE_FOLDER" -name "*$FILE_BASE*" | sed -r 's/.*\.([0-9]+$)/\1/' | sort -n | tail -n 1)

TARGET_FILE=$(find "$SOURCE_FOLDER" -name "$FILE_BASE.$HIGHEST_NUMBER")

if [ -f $TARGET_LINK ];
then
    rm $TARGET_LINK
fi

ln -s $TARGET_FILE $TARGET_LINK

For each symlink you want to update you would need one copy of this script.

March 24, 2013 | linux, shell, cron

The Hydra Bay

13hydra.png

The Pirate Bay hat ein neues Logo. Wer mit The Hydra Bay nichts anfangen kann, der darf sich hier die Geschichte dazu durchlesen.

Pittrich hat das ganze mal kopimisiert und vektorisiert, damit dem T-Shirt und Hoodie Druck nichts mehr im Wege steht. The Hydra Bay (SVG)

February 27, 2013 | tpb, svg, Arrr

Pluto und seine Monde

Wie ich gerade auf heise gelesen habe, hat Pluto sogar 5 Monde. Charon war mir bekannt, nun man lernt nie aus.

Fuer die Monde P4 und P5 wurden ueber eine Onlineumfrage neue Namen gesucht. William Shatner hat den Namen Vulcan vorgeschlagen und Leonard Nimoy unterstuetzte diesen Vorschlag mit einem grossartigen Tweet:

"Vulcan" is the logical choice. LLAP

February 26, 2013 | universe, star trek
about | blog | archives