Marty O'Brien > Language-GolfScript > Language::GolfScript



Annotate this POD

View/Report Bugs
Module Version: 0.04   Source  


Language::GolfScript - Perl implementation of GolfScript


Version 0.04


    # 1000 digits of PI, see
    use Language::GolfScript;

    # command-line. Say ...
    $ perl -MG
    # ... instead of ...
    $ ruby golfscript.rb
    # look! you already saved 10 keystrokes!

    # one-liner to compute pow(4,24)
    $ perl -MG -e '4 24?'



GolfScript is a stack oriented esoteric programming language aimed at solving problems (holes) in as few keystrokes as possible. It also aims to be simple and easy to write.

Short code is achieved by using single symbols to represent high level operations (such as map, join, array size, etc). Being stack based allows for functions and other manipulations to work without the need for explicit variables, however variables still exist and are useful for more complicated stack manipulations.

More details about GolfScript, including documentation, a tutorial, and examples are available at the home page

The Language::GolfScript module is a Perl implementation of the GolfScript spec, which was originally written in Ruby. It includes the G package, which simply wraps a source filter around Language::GolfScript and allows a GolfScript source file to be executed from the command line.

The current implementation is ~2100 lines and is still missing a few features. But I'm going to see if I can get it even lower! ;-)


Mainly the G package uses source filtering to automatically parse and execute the contents of a source file as GolfScript. But it makes use of the following functions in the Language::GolfScript module, which may also be called independently (to debug or to try several test cases, for example).

Execution methods


Initializes the GolfScript interpreter, executes the specified string of GolfScript code, and prints the resulting stack to standard output.

Language::GolfScript::test($source_string [, $input)

Initializes the stack to the specified input, executes the specified string of GolfScript code, and returns a string represnting the output of the GolfScript program.


Evaluates a string of GolfScript code with respect to the current stack. May change the stack depending on the code that was executed.



Input to the GolfScript program. When the module is first loaded, this variable is initialized from standard input and the stack is set to contain a single string containing this input.


If set to one, this module will write many details about the operations being performed to standard error.

If set to a value greater than one, this module will write even more details about operation of the GolfScript parser.

Any of these constructions

    use Language::GolfScript 'debug';
    use G 'debug';
    $ perl -MG=debug

will initialize Language::GolfScript::DEBUG to 1. Using the word verbose instead of debug will initialize Language::GolfScript::DEBUG to 2.


If set to non-zero, reports the character count to standard output after the script is evaluated.

This variable may be initialized to 1 by passing the word count to the module's import function, for example like

    use Language::GolfScript 'count'
    $ perl -MG=debug,count

The code length count function attempts to remove comments and whitespace that "looks" unnecessary. If any tokens were removed from the code for the counting function, the function will also display the "stripped" code. In this way, you may develop a script that includes comments and commented-out sections of code, and get a string of code where most of the unnecessary characters have already been removed.


If non-zero, terminates the program after the specified number of seconds. Helpful if your GolfScript code might enter an infinite loop.

This variable can be initialized by passing the word timeout to the module's import function.

    use Language::GolfScript timeout => 60;
    $ perl -MG=timeout=10




While much effort has been devoted to getting the Perl implementation of GolfScript to conform to the specification and to be consistent with the Ruby implementation, there are a few differences.

Perl implementation is not complete

Every effort has been made to get the specific examples on the GolfScript Builtins page to work. But the implementation may have neglected some infrequently used operations that are valid GolfScript syntax (say, the ) operating on a block). Feel free to bring these edge cases to my attention with CPAN's Request Tracker (see "SUPPORT AND DOCUMENTATION", below).

Perl GolfScript is slow

Supposedly, Ruby is slow and the Ruby GolfScript implementation is even slower. Well, Perl GolfScript is slower still, for a variety of reasons.

If biginteger operations are particularly painful on your script, consider installing libgmp ( on your system along with the Math::BigInt::GMP package.

Interpolated string parsing

Ruby's string parsing function allows arbitrary Ruby expressions to be included in strings with the #{ruby expression} syntax. This functionality is also exported to Ruby GolfScript:

    "The time is now #{}"

In Perl's GolfScript, the best we can easily do is to evaluate this syntax as a Perl expression with Perl's eval function. So an equivalent Perl GolfScript code snippet would be

    "The time is now #{scalar localtime}"
Error messages

Illegal operations in GolfScript generate a stack trace. The Ruby and Perl GolfScript stack traces will necessarily look different for the same error.


After installing, you can find documentation for this module with the perldoc command.

    perldoc Language::GolfScript

You can also look for information at:

    RT, CPAN's request tracker

    AnnoCPAN, Annotated CPAN documentation

    CPAN Ratings

    Search CPAN


Specification and original Ruby implementation by Darren Smith.

Perl implementation by Marty O'Brien, <>.


Copyright (c) 2010, Marty O'Brien

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.0 or, at your option, any later version of Perl 5.

syntax highlighting: