diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-05-18 21:35:38 +0100 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-05-21 05:11:05 +0100 |
| commit | 2fa917c369447694dc76308560d0c5f65673ec59 (patch) | |
| tree | b099e3a77850c825f65367fd38015d9490d99a83 /content/md/gemfeed | |
| parent | d371050f2fb79b6d66a810e60ff9c0ed2381f2cc (diff) | |
add polyglot post
change html quotes
Diffstat (limited to 'content/md/gemfeed')
| -rw-r--r-- | content/md/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.md | 110 | ||||
| -rw-r--r-- | content/md/gemfeed/2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md | 2 | ||||
| -rw-r--r-- | content/md/gemfeed/index.md | 1 |
3 files changed, 113 insertions, 0 deletions
diff --git a/content/md/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.md b/content/md/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.md new file mode 100644 index 00000000..a422ef1e --- /dev/null +++ b/content/md/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.md @@ -0,0 +1,110 @@ +# The fibonacci.pl.c Polyglot + +> Written by Paul Buetow 2014-03-24 + +In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it + +[https://en.wikipedia.org/wiki/Polyglot_(computing)](https://en.wikipedia.org/wiki/Polyglot_(computing)) + +## The Fibonacci numbers + +For fun, I programmed my own Polyglot, which is both, valid Perl and C code. The interesting part about C is, that $ is a valid character to start variable names with: + +``` +#include <stdio.h> + +#define $arg function_argument +#define my int +#define sub int +#define BEGIN int main(void) + +my $arg; + +sub hello() { + printf("Hello, welcome to Perl-C!\n"); + printf("This program is both, valid C and Perl code!\n"); + printf("It calculates all fibonacci numbers from 0 to 9!\n\n"); + return 0; +} + +sub fibonacci() { + my $n = $arg; + + if ($n < 2) { + return $n; + } + + $arg = $n - 1; + my $fib1 = fibonacci(); + $arg = $n - 2; + my $fib2 = fibonacci(); + + return $fib1 + $fib2; +} + +BEGIN { + hello(); + my $i = 0; + + for ($i = 0; $i <= 10; ++$i) { + $arg = $i; + printf("fib(%d) = %d\n", $i, fibonacci()); + } + + return 0; +} +``` + +You can find the whole source code at GitHub: + +[https://github.com/snonux/perl-c-fibonacci](https://github.com/snonux/perl-c-fibonacci) + +### Let's run it with Perl: + +``` +❯ perl fibonacci.pl.c +Hello, welcome to Perl-C! +This program is both, valid C and Perl code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +``` + + +### Let's compile it as C and run the binary: + +``` +❯ gcc fibonacci.pl.c -o fibonacci +❯ ./fibonacci +Hello, welcome to Perl-C! +This program is both, valid C and Perl code! +It calculates all fibonacci numbers from 0 to 9! + +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +``` + +It's really fun to play with :-). + +E-Mail me your thoughts at comments@mx.buetow.org! + +[Go back to the main site](../) diff --git a/content/md/gemfeed/2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md b/content/md/gemfeed/2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md index ad2fdaba..c6ef908a 100644 --- a/content/md/gemfeed/2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md +++ b/content/md/gemfeed/2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md @@ -24,6 +24,8 @@ Over the last couple of years I wrote quite a few Puppet modules in order to manage my personal server infrastructure. One of them manages FreeBSD Jails and another one ZFS file systems. I thought I would give a brief overview in how it looks and feels. +[https://github.com/snonux/puppet-modules](https://github.com/snonux/puppet-modules) + ## ZFS The ZFS module is a pretty basic one. It does not manage ZFS pools yet as I am not creating them often enough which would justify implementing an automation. But let's see how we can create a ZFS file system (on an already given ZFS pool named ztank): diff --git a/content/md/gemfeed/index.md b/content/md/gemfeed/index.md index 624ef06d..b314429c 100644 --- a/content/md/gemfeed/index.md +++ b/content/md/gemfeed/index.md @@ -12,6 +12,7 @@ [2016-04-09 - Jails and ZFS with Puppet on FreeBSD](./2016-04-09-jails-and-zfs-on-freebsd-with-puppet.md) [2016-04-03 - Offsite backup with ZFS](./2016-04-03-offsite-backup-with-zfs.md) [2015-12-05 - Run Debian on your phone with Debroid](./2015-12-05-run-debian-on-your-phone-with-debroid.md) +[2014-03-24 - The fibonacci.pl.c Polyglot](./2014-03-24-the-fibonacci.pl.c-polyglot.md) [2011-05-07 - Perl Daemon (Service Framework)](./2011-05-07-perl-daemon-service-framework.md) [2010-05-09 - The Fype Programming Language](./2010-05-09-the-fype-programming-language.md) [2010-04-09 - Standard ML and Haskell](./2010-04-09-standard-ml-and-haskell.md) |
