FelixGeator has asked for the wisdom of the Perl Monks concerning the following question:

I have strange idea; Is possible write a Perl program which output a working Perl program which it also capable of output a working Perl program?

Many thanks good monks.

Replies are listed 'Best First'.
Re: Programs that write programs
by moritz (Cardinal) on Oct 28, 2011 at 10:20 UTC

    Sure. For example a "quine" is a program that produces its own source code as output. Running that output again as a Perl program of course yields the same output again.

    There are also nice multi-language quines, for example Perl code that output shell code, which then again produces the original Perl code etc. See for example http://www.nyx.net/~gthompso/quine.htm and http://johannesloetzsch.de/software/quines/.

    A less obscure example is a compiler: A compiler produces working code, and if it is written in the language it compiles, then it can compile itself, and the result can again compile itself etc. This is called "bootstrapping".

    Another example would be Acme::EyeDrops, which you can probably use on itself.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Programs that write programs
by roboticus (Chancellor) on Oct 28, 2011 at 13:05 UTC

    FelixGeator:

    It's certainly possible, though I've not written exactly that. One thing I do every once in a while is to write a program that will read a spreadsheet (that define simple requirements) and use it to generate a program. For example, on a previous job I was asked to create a parser for a horribly complicated ASCII report and convert it into a set of flat files suitable to import into a database.

    I told them "OK, if you give me an Excel spreadsheet describing exactly what you want in the format I specify, I'll give you a program that will do what you want." The spreadsheet was pretty simple: the first page described the input file & fields, the second page described the output file & fields, and the third page specified how to map and transform the input fields into the output fields. Perl wasn't installed on the destination machine, so my program wrote a C++ program that did the work. I also gave them the perl program so if they wanted simple changes, they could do it themselves.

    That program was very useful, as it saved me a great deal of trouble on that project (in fact, they used it for several other projects too, so it saved me a *lot* of headaches). For years afterwards, I would get requests to add another feature (transformation type) to the perl program. For example, they wanted to generate flat files to send to the mainframe, so they asked for a TO_EBCDIC transformation so they could output COMP-3 fields to the mainframe.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Programs that write programs
by Anonymous Monk on Oct 28, 2011 at 10:31 UTC
    I don't know your intention, but Perl offers programming facilities apart from those moritz talked about that are better suited to solving certain problems because they take shortcuts through the costly cycle of writing out source to a file, launching a new Perl instance and compiling the whole file.

    I'll mention code references (with which you can do amazing things), eval and Moose::Meta::{Class,Method}.

      No intention just strange dream last night!
        One take on this is the "meta-circular evaluator":
      • Structure And Interpretation Of Computer Programs
      • Abelson & Sussman's celebrated sort-of-Scheme-fest. Bill Hails has done something similar from a perl p.o.v:
      • http://www.billhails.net/Book/