An example of dying with a hash value in Perl XS

This page presents a simple and very minimalistic XS module which demonstrates how to "die" with a hash value rather than a string within a Perl XS extension. It relies on croak_sv. To run this, download the following four example files and put them in a directory or folder of their own, then follow the instructions at the base of the page.

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

static void acme_die_hash ()
    HV * hv;
    SV * sva;
    SV * svb;
    SV * hashref;
    sva = newSViv (1);
    svb = newSViv (2);
    hv = newHV ();
    hv_store (hv, "funky", strlen ("funky"), sva, 0);
    hv_store (hv, "click", strlen ("click"), svb, 0);
    hashref = newRV_inc ((SV *) hv);
    croak_sv (hashref);

MODULE=Acme::Die::Hash PACKAGE=Acme::Die::Hash


acme_die_hash ()
        acme_die_hash ();


package Acme::Die::Hash;
use warnings;
use strict;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw/acme_die_hash/;
our $VERSION = '0.01';
require XSLoader;
XSLoader::load ('Acme::Die::Hash', $VERSION);


use strict;
use warnings;
use ExtUtils::MakeMaker;

my $pm = '';
WriteMakefile (
    NAME => 'Acme::Die::Hash',
    VERSION_FROM => $pm,
    OBJECT => 'Hash.o',


use warnings;
use strict;
use Test::More;
use Acme::Die::Hash;
eval {
    acme_die_hash ();
ok ($@, "Died");
note ($@);
ok (ref $@ eq 'HASH', "Died with a hash");
ok ($@->{funky} == 1, "Got 1 for funky key");
done_testing ();


To run this example and see the test, use the following commands:

perl Makefile.PL
make test

The output should look something like the following:

$ perl Makefile.PL
Generating a Unix-style Makefile
Writing Makefile for Acme::Die::Hash
Writing MYMETA.yml and MYMETA.json
$ make
cp blib/lib/Acme/Die/
cp blib/lib/Acme/Die/
Running Mkbootstrap for Acme::Die::Hash ()
chmod 644 ""
"/usr/home/ben/software/install/bin/perl5.18.2" "/home/ben/software/install/lib/perl5/site_perl/5.18.2/ExtUtils/xsubpp"  -typemap "/home/ben/software/install/lib/perl5/5.18.2/ExtUtils/typemap"  Hash.xs > Hash.xsc && mv Hash.xsc Hash.c
cc -c    -DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -O    -DVERSION=\"0.01\"  -DXS_VERSION=\"0.01\" -DPIC -fPIC "-I/home/ben/software/install/lib/perl5/5.18.2/i386-freebsd/CORE"   Hash.c
rm -f blib/arch/auto/Acme/Die/Hash/
cc  -shared  -L/usr/local/lib -fstack-protector Hash.o  -o blib/arch/auto/Acme/Die/Hash/ 	    	 
chmod 755 blib/arch/auto/Acme/Die/Hash/
"/usr/home/ben/software/install/bin/perl5.18.2" -MExtUtils::Command::MM -e 'cp_nonempty' -- blib/arch/auto/Acme/Die/Hash/ 644
$ make test
Running Mkbootstrap for Acme::Die::Hash ()
chmod 644 ""
PERL_DL_NONLAZY=1 "/usr/home/ben/software/install/bin/perl5.18.2" "-Iblib/lib" "-Iblib/arch"
ok 1 - Died
# HASH(0x28806d60)
ok 2 - Died with a hash
ok 3 - Got 1 for funky key

Web links

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock ( or use the discussion group at Google Groups. / Privacy / Disclaimer