Inline::Befunge - write Perl subs in Befunge
use Inline "Befunge"; print "9 + 16 = ", add(9, 16), "\n"; print "9 - 16 = ", subtract(9, 16),"\n"; __END__ __Befunge__ ;:add; +q ;:subtract; -q
Inline::Befunge module allows you to put Befunge source code directly "inline" in a Perl script or module.
This allows you to write cool stuff with all the power of Befunge!
Inline::Befunge will seem very similar to using a another Inline language, thanks to Inline's consistent look and feel.
This section will explain how to use
For more details on
The recommended way of using Inline is the following:
use Inline Befunge; ... __END__ __Befunge__ Befunge source code goes here.
But there are much more ways to use Inline. You'll find them in
As a befunge fan, you know that Befunge does not support named subroutines. So, I introduced a little hack (thank goes to Sean O'Rourke) in order for Inline to work: each subroutine definition should be prepended with a comment
;:subname; (notice the colon prepended).
You'll notice how smart it is, since it's enclosed in comments, and therefore the overall meaning of the code isn't changed.
You can define your subroutines in any of the four cardinal directions, and the subroutine velocity will be the velocity defined by the comment. That is, if you define a subroutine in a vertical comment from bottom to top, when called, the subroutine will start with a velocity of (0,-1).
You can add comments after the subname. Thus,
;:foo - a foo subroutine; defines a new subroutine
So, here's a valid example:
use Inline "Befunge"; hello(); __END__ __Befunge__ ;:hello - print a msg;<q_,#! #:<"Hello world!"a ; : a q - ;tcartsbus:; d d ; + ;not a valid func def; q
In this example, I defined three functions:
substract, and you can call them from within perl as if they were valid perl functions. The fourth comment
;not a valid func def; isn't a valid function definition since it lacks a colon
: just after the
In order to write useful functions, one should be able to pass arguments. This is possible, and one will find the arguments on the TOSS of the interpreter: this means the stack may not be empty at the beginning of the run. If you're a purist, then you may ignore this and call your functions without arguments, and the stack will be empty.
Strings are pushed as 0gnirts.
/!\ Remember, Befunge works with a stack: the first argument will be the first pushed, ie, the deeper in the stack.
For example, when calling an inlined Befunge function like this:
foo( 'bar', 7, 'baz' );, then the stack will be (bottom->top):
(0 114 97 98 7 0 122 97 98).
Furthermore, one can return some values from the befunge subroutines... how exciting!
To do so, the Befunge semantics have been a little adapted:
when an Instruction Pointer reaches a
q instruction, one cell will be popped from its stack and will be returned. This works like a scalar return.
when an Instruction Pointer reaches a
@ instruction, it will be killed (just as in traditional Befunge). But when the last IP reaches a
@, then the sub is exited and will return the whole stack of the IP that just died. This works like a list return.
/!\ Be careful, that you will return a list of integer values. You are to decide what to do with those values (especially, you are to convert back to characters with the
chr perl builtin function and join the chars if you're waiting for a string!).
Inline::Befunge supports a DEBUG option, to follow the IP(s) as they are running on the funge-space.
Use the following to activate it:
use Inline( Befunge => 'DATA', DEBUG => 1 ); ... __END__ __Befunge__ Enter you befunge code here.
Register as an Inline Language Support Module.
Check the params for the Befunge interpreter. Currently no options are supported.
Register the Befunge as a valid Inline extension.
This function actually fetches the Befunge code. It first splits it to find the functions.
Return a small report about the Befunge code.
I would like to thank:
Brian Ingerson, for writing the incredibly cool
Inline module, and giving the world's programmers enough rope to hang themselves many times over.
Chris Pressey, creator of Befunge, who gave a whole new dimension to both coding and obfuscating.
Sean O'Rourke <email@example.com> for his incredible cool idea on defining labels in Befunge code.
<jquelin at cpan.org>
Copyright (c) 2001-2007 Jerome Quelin, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.