Hello fellow Perl monks, I'm very interested in the Lisp programming language. As such, I've seen quite a few discussions in LISP related forums. One that comes up often is that modern languages (Python, Perl, etc...) are coming closer and closer to Lisp, employing functional programming tactics. Just to remind you:

"Functional programming is a style of programming that emphasizes the evaluation of expressions rather than the execution of commands."

I've came to think, what reminds of this style in Perl. Here are some examples that immediately popped up in my head:

Mapping

@arr = (1, 2, 3, 4, 5); map {print "** $_ **\n";} @arr;

Anonymous subs & treating subs as data

$ast_print = sub {print "** $_[0] **\n"}; process_and_print(25, $ast_print); sub process_and_print { my $val = $_[0]; my $print_func= $_[1]; #..... processing .... &$print_func($val); }

Eval: evaluating strings as Perl expressions

$op = "print \"hello\""; eval $op;

The latter reminds me of a nice example the Lisp people like to show. How do you write a Lisp interpreter ? Easy:

(loop (print (eval (read))))

You can do the same in Perl, and it isn't less easy (though a bit more obfuscated - the Perl way :)

while (<>) { eval; print $@; }

Note: all of the above are toy examples, chosen to demonstrate their specific purposes.

And I wanted to consult you, knowledgeable monks, what other examples do you have in mind ? Some very cool things may be done with the constructs shown above. eval is frequently used in obfuscations, but it is a very powerful tool - for code reading and executing code, etc.


In reply to Functional programming with Perl by spurperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.