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 PROTOTYPES: DISABLE void acme_die_hash () CODE: 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); 1;
use strict; use warnings; use ExtUtils::MakeMaker; my $pm = 'Hash.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 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 build-template.pl blib/lib/Acme/Die/build-template.pl cp Hash.pm blib/lib/Acme/Die/Hash.pm Running Mkbootstrap for Acme::Die::Hash () chmod 644 "Hash.bs" "/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/Hash.so cc -shared -L/usr/local/lib -fstack-protector Hash.o -o blib/arch/auto/Acme/Die/Hash/Hash.so chmod 755 blib/arch/auto/Acme/Die/Hash/Hash.so "/usr/home/ben/software/install/bin/perl5.18.2" -MExtUtils::Command::MM -e 'cp_nonempty' -- Hash.bs blib/arch/auto/Acme/Die/Hash/Hash.bs 644 $ make test Running Mkbootstrap for Acme::Die::Hash () chmod 644 "Hash.bs" PERL_DL_NONLAZY=1 "/usr/home/ben/software/install/bin/perl5.18.2" "-Iblib/lib" "-Iblib/arch" test.pl ok 1 - Died # HASH(0x28806d60) ok 2 - Died with a hash ok 3 - Got 1 for funky key 1..3
Web links
-
Answer to "How can I output a die() message without the location
information?" at Stackoverflow.com
This page is an attempt to translate the above answer into XS. The above is a part of JSON::Parse.
Copyright © Ben Bullock 2009-2024. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com) or use the discussion group at Google Groups.
/
Privacy /
Disclaimer