Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw


( #480=superdoc: print w/replies, xml ) Need Help??

If you've discovered something amazing about Perl that you just need to share with everyone, this is the right place.

This section is also used for non-question discussions about Perl, and for any discussions that are not specifically programming related. For example, if you want to share or discuss opinions on hacker culture, the job market, or Perl 6 development, this is the place. (Note, however, that discussions about the PerlMonks web site belong in PerlMonks Discussion.)

Meditations is sometimes used as a sounding-board — a place to post initial drafts of perl tutorials, code modules, book reviews, articles, quizzes, etc. — so that the author can benefit from the collective insight of the monks before publishing the finished item to its proper place (be it Tutorials, Cool Uses for Perl, Reviews, or whatever). If you do this, it is generally considered appropriate to prefix your node title with "RFC:" (for "request for comments").

User Meditations
Anniversary: five years a monk
No replies — Read more | Post response
by Laurent_R
on Sep 09, 2017 at 11:06
    Dear fellow brethren,

    I made profession and registered to this monastery just five years ago today. And since then, I connected to this forum almost every single day. And it has been a pleasure every time.

    Even though I asked only a few questions over these five years, but struggled much more to try to answer others' questions, I learned really a lot from reading posts from many fellow monks in this community. Over this five-year period, this site has made me knowledgeable about my favorite programming language more than anything else I have done in the meantime.

    Many thanks to you all, dear sisters and brothers. And long live Perl.


RFC: LSP for Perl
1 direct reply — Read more / Contribute
by RonW
on Sep 06, 2017 at 20:30

    When I went to to see if the latest major release was ready (it is, I'm late, as usual), I discovered something called "Language Server Protocol". Eclipse Oxygen supports it. Several other editiors/IDEs supposedly already support it.

    The idea is that instead of each editor/IDE needing a plug-in for each additional language some one wants to support, each editor/IDE gets a LSP client plug-in and each language gets a LSP server.

    I did some searching, but didn't find project creating a LSP server for Perl. Still, I think it would be a way to help get better support for Perl into the editors and IDEs that people want to use.

    So, sisters and brothers, what do you think? would you be willing to contribute (and how)?

    Thank you for your thoughts.

Chinese to Hex and Hex to Chinese
1 direct reply — Read more / Contribute
by thanos1983
on Sep 05, 2017 at 04:47

    Hello fellow Monks,

    I am trying to convert an Chinese string to hexadecimal (final form) and vise versa, but I wanted also to see the in between steps.

    I tried successfully to convert the string to utf-8 and then I was thinking of converting it into ascii characters and then to hex with the help of String::HexConvert. If that would work I would revert the process to the original form.

    I manage to accomplish my task for utf-8 and UCS-2 encoding. I though of sharing the script, since I was not able to find further information online.

    Similar question (How do I convert a sequence of hexes (D0 D6) to Chinese characters (中)?). I used the following modules (Text::Unidecode) and also (String::HexConvert).

    Sample of code:

    use utf8;
    use strict;
    use warnings;
    use feature 'say';
    use Text::Unidecode;
    use Encode qw(decode encode);
    use String::HexConvert ':all';
    binmode( STDOUT, ':utf8' );
    my $Chinese = '北亰'; # Chinese characters for Bei Jing (U+5317 U+4EB0)
    say 'UTF-8';
    my $utf8 = encode( 'UTF-8', $Chinese );
    my $ascii2hexUTF8 = ascii_to_hex($utf8);
    say join(' ', split(/(..)/, $ascii2hexUTF8));
    my $hex2ascciiUTF8 = hex_to_ascii($ascii2hexUTF8);
    my $strUTF8 = decode( 'UTF-8', $hex2ascciiUTF8);
    say $strUTF8;
    say unidecode( $strUTF8 );
    say '';
    say 'UCS-2';
    my $ucs2 = encode("UCS-2BE", $Chinese);
    my $ascii2hexUCS2 = ascii_to_hex($ucs2);
    say join(' ', split(/(..)/, $ascii2hexUCS2));
    my $hex2ascciiUCS2 = hex_to_ascii($ascii2hexUCS2);
    my $strUCS2 = decode("UCS-2BE", $hex2ascciiUCS2);
    say $strUCS2;
    say unidecode( $strUCS2 );
    $ perl
     e5  8c  97  e4  ba  b0
    Bei Jing
     53  17  4e  b0
    Bei Jing

    Hope this helps someone else as it did to me, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Scope in use
5 direct replies — Read more / Contribute
by choroba
on Sep 04, 2017 at 04:35
    A colleague has asked me recently why the following ends in Global symbol "$lib" requires explicit package name:
    #! /usr/bin/perl use warnings; use strict; use lib my $lib = '.'; print $lib;

    My answer was that the code is equivalent to

    BEGIN { require lib; 'lib'->import(my $lib = '.'); }

    But I was kind of unhappy about it. Is it a feature that use creates a scope around its parameters, or was it just easier to implement it like that? Have you noticed the behaviour and what do you think about it?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
A Few Impressions from Amsterdam
4 direct replies — Read more / Contribute
by haukex
on Aug 13, 2017 at 11:03

    I was originally going to post something else for my 1000th node, but I'll save that for later, since I think this is fitting: As many of you probably know, The Perl Conference in Amsterdam ended on Friday. It was my first Perl event, and a great experience! I was very happy to finally meet some of you in person, choroba, LanX, Corion, Tux, Laurent_R, and rurban, and I'm really sorry I didn't get to say bye properly since I had to leave a bit early. If I met some of you but haven't yet matched you to a username, I apologize and drop me a /msg.

    I enjoyed the talks by some of the greats like TimToady, TheDamian, Ovid, and brian_d_foy, some of which are already available as videos on Facebook (Update: and YouTube, thanks Corion), and I hear the full videos should be online within a month or so. For my favorite funny moment of the conference: First, watch Damian Conway's talk "Three Little Words", definitely worth it on its own (as is checking out the impressive PPR), and then go to the Lighting Talks from Aug 10th and skip ahead to around 1h39m15s in the video... :-)

    Update: Added direct links to the YouTube videos.

    Update 2: Some pictures can be found at Also updated links to YouTube.

How has Perl affected you?
9 direct replies — Read more / Contribute
by stevieb
on Aug 12, 2017 at 16:58

    Slow weekend afternoon, taking a break from packing up my life and doing a huge shift towards an entirely new adventure.

    I ran into Perl through my first non-contract job. I picked up an ISP that was barely more than a start-up, and with a book off of a shelf, I fell in love (Perl 4/early 5 days).

    Over the years, (but with 20/20 hindsight it started not long after I had to pick up Perl) I have come to appreciate the Perl community as a group who are loyal, dedicated and serious, all the while being able to take criticism quite well.

    I savored the day I became a Friar; it allowed me to take part in some decision making on this site, which imho is the de-facto place to find Perl experts.

    Since the first day I picked up that "Learn Perl in 21 Days" book, I've gone on to do a lot of interesting things, meet a lot of interesting people, learned more than what I've been able to share, and help a lot of people in this language (and thanks to it, other ones as well).

    I'm coming up on my 8th birthday here, so while taking a breather from the physical duties of life, thought I'd once again share my appreciation for Perlmonks, and ask you, newbie or not, why you are invested in Perl, what it has done for you, and whether it has changed anything regarding how you approach other tasks/problems in your day-to-day.


High Performance Game of Life
5 direct replies — Read more / Contribute
by eyepopslikeamosquito
on Aug 11, 2017 at 08:49

    The universe of Conway's Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

    1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    2. Any live cell with two or three live neighbour lives on to the next generation.
    3. Any live cell with more than three live neighbours dies, as if by overcrowding.
    4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed - births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

    -- Conway's Game of Life (wikipedia)

    I've recently been forced to become acquainted with Conway's Game of Life after being asked to assess various implementations of it submitted by job applicants. Rather than a chore, I actually found it very interesting. Not the GUI stuff, just the guts of the code. How to implement it cleanly, concisely, efficiently.

    I wrote a couple of solutions myself, in both Perl and C++11, which I present below. Though still a game of life novice, I've spent quite a bit of time on these solutions already - including some profiling and performance optimization of my C++ solution. My Perl solution is essentially a translation of the one I wrote first in C++.

    Having spent time analysing this problem, I'm interested to see how others go about it.

    The Challenge

    If you want to test yourself, and have a few hours to spare, you might like to have a go (without reading my solutions below) at implementing the above algorithm as a simple Organism class with methods:

    • insert_cells() - insert a list of live cells (x and y coordinates) into the Organism. This is used to set the starting state of the Organism.
    • count() - return the number of live cells in the Organism. This is used for verification by unit tests.
    • get_live_cells() - return the list of all cells currently alive in the Organism. This is also used for verification.
    • tick() - create the next generation of the Organism by applying the four rules above. This is where most of the hard work and performance challenges are.
    For this challenge, you don't need to be truly infinite, but you must handle cells with x and y co-ordinates in the -2 GB to 2 GB range (i.e. 32-bit signed integer x and y co-ordinates). In the interests of keeping the challenge smallish, you don't need to consider graphics, customizations, variations, just implement the four basic rules above.


    I also have a special interest in high performance computing, so hope to learn more about performance and scalability by the feedback I get from this node. I've already sought performance advice related to this challenge in Fastest way to lookup a point in a set.

    Update: A much faster, and much more complex, implementation was done later in a follow up node: More Betterer Game of Life

RFC: pragma pragmatic
3 direct replies — Read more / Contribute
by shmem
on Aug 09, 2017 at 03:55

    We all (that's a rough rounded estimate) know about the most important pragma modules - strict and warnings - and maybe also how to turn them on or off, or how to turn on/off a subset of the facilities provided by them. In comparison, few of us know how a pragma really works. I don't either - to know *really* well, I'd have to wade the perl source code, which I won't, for now.

    This I know from the docs, if I haven't misread them: the variable $^H is used for hints to the compiler, e.g. how to behave in respect to refs, subs and vars. The hash %^H is used for hints from the compiler to the interpreter, i.e. perl's runtime. The current state of %^H at compile time is stored in the optree (another obscure thing most perl programmers don't care about, but that's what's getting run), so its state can be retrieved at runtime via (caller($level))[10].

    We could use that to make the scope of imported functions from some arbitrary package purely lexical, even if that package isn't pragma aware. Here's a shot at this:

    Package pragmatic:

    Usage example:

    package Foo; use 5.10.0; require Exporter; @Foo::ISA = qw(Exporter); our @EXPORT = qw(foo); sub foo { say "Foo::foo at line ".(caller)[2] } 1;
    #!/usr/bin/perl use 5.10.0; sub foo { say "main::foo at line ".(caller)[2] } use pragmatic Foo; foo; # line 5 no pragmatic Foo; foo; # line 7 use pragmatic Foo; foo; # line 9 no pragmatic; foo; # line 11 use pragmatic Foo; foo; # line 13 __END__ Foo::foo at line 5 main::foo at line 7 Foo::foo at line 9 main::foo at line 11 Foo::foo at line 13

    A module imported via pragmatic isn't imported into the caller's namespace, but into the package pragmatic::import. Each symbol from that package is inspected, and after inspection, deleted. If the CODE slot is defined, its content is stored in the hash %symbols for this caller and this package. If a symbol of that name is found in the caller, its CODE slot content is saved to the hash %masked. The caller is given a reference to the undefined subroutine of that name in package pragmatic. At runtime, this triggers AUTOLOAD, where the appropriate subroutine is called, depending on whether the pragma is in effect or not.

    This works for exported subroutines only. If a package exports other glob types, they are passed into the caller's namespace, and they aren't managed by pragmatic. I wish there was a way to govern those too.

    To be done yet:

    • a forget(PACKAGE) method which finishes the pragmatic management and stores the stashed subroutines permanently in the caller's namespace
    • ... (fill the blank)

    What do you think? Bad idea? useful? Big delirium? Comments welcome.

    edit: fixed some bugs

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Which internal DSL are there in Perl? (Domain Specific Languages - Part 1)
7 direct replies — Read more / Contribute
by LanX
on Aug 02, 2017 at 22:08
    Taking some definitions from Wikipedia and Martin Fowlers Book: (emphasizes added)

    DSLs are small languages, focused on a particular aspect of a software system. You can't build a whole program with a DSL, but you often use multiple DSLs in a system mainly written in a general purpose language.

    DSLs come in two main forms: external and internal

    An external DSL is a language that's parsed independently of the host general purpose language: good examples include regular expressions and CSS

    Internal DSLs are a particular form of API in a host general purpose language, often referred to as a fluent interface.

    They are represented within the syntax of a general-purpose language. Itís a stylized use of that language for a domain-specific purpose.

    People find DSLs valuable because a well designed DSL can be much easier to program with than a traditional library.

    This improves programmer productivity , which is always valuable. In particular it may also improve communication with domain experts , which is an important tool for tackling one of the hardest problems in software development.

    Now there are some more frequently used examples in Perl which come to mind like SQL and Template languages like in Template::Toolkit2

    [% FOREACH file=files %] [% IF ( matches ='dd/mm/(\d+)') ) %] <a href='#'>[% matches.0 %]</a> [% END %] [% END %]

    Both qualify as are for sure as DSLs.

    They are sub-languages which can be used by "domain experts" without any knowledge of Perl.

    And they are external, because neither SQL nor TT commands are implemented as Perl functions or follow Perl syntax.

    • SQL appears as embedded string inside Perl code is only parsed after leaving Perl thru DBI
    • And TT may be parsed with Perl but is not parsed as Perl.
    So far for external DSLs , but what are good examples for internal DSLs in Perl?

    Well designed traditional modules and classes are sometimes close to represent a sub-language but they still come with a lot of boilerplate.

    For instance an expert in the domain of TCL/TK may (probably?) not care much if the "host" language is Perl or Python or Ruby as long as he can call the commonly known methods. (see for comparison of snippets in various languages).

    But the necessity to constantly repeat an $obj-> or PACK::AGE:: on the left hand side of a command doesn't make it a completely "fluent" approach.

    (On a side note: avoiding to repeat this LHS for method calls seems to be the principal mechanism in Ruby for transforming a simple class to what Rubyists like to call a "DSL", a bunch of function calls inside a block are somehow interpreted as method calls on the same object, hence very similar to the with(obj) {} mechanism in JavaScript - please correct me if not)

    Now, what are examples for internal DSLs in Perl?

    The only widely known one which comes to my mind is CGI::HTML::Functions which used to be part of CGI

    For example, here's one way to make an ordered list:
    print ul( li({-type=>'disc'},['Sneezy','Doc','Sleepy','Happy']) );
    This example will result in HTML output that looks like this:
    <ul> <li type="disc">Sneezy</li> <li type="disc">Doc</li> <li type="disc">Sleepy</li> <li type="disc">Happy</li> </ul>

    Nice but IMHO it could be even more fluent.

    But are there others?

    There is for instance Rex which I suppose was inspired by Ruby's Puppet

    use Rex -feature => ['1.0']; desc "Get Uptime"; task "uptime", sub { say run "uptime"; }

    And I think I could theoretically implement most Assembler languages as Perl code just with some minor changes. I just need to implement ASM-commands as uppercase subs, quote literals and adjust the comment symbol.


    CPX '3' ;# Year starts in March to bypass BCS 'MARCH' ;# leap year problem DEY ;# If Jan or Feb, decrement year MARCH: EOR '$7F' ;# Invert A so carry works right ...
    (original example)

    But there must be more examples of internal DSLs in Perl ...

    Could you please help me to find and classify them?

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

RFC: Confirming module being tested
No replies — Read more | Post response
by RonW
on Jul 31, 2017 at 19:33

    In #p5p finds your lack of failing tests disturbing, there was discussion about Perl no longer including the current directory in @INC and how this would affect modules that load other files. In the referenced post,, the author, Mr Fredric, seems concerned that testing environments, such as Test::More may not be working correctly.

    From that, it occurred to me that it might be wise for tests to confirm that the module being tested is the one the test expects.

    Today, the noderep ticker "reminded" me about this concern, so I wrote a first draft of a version confirmation test:

    use 5.006; use strict; use warnings FATAL => 'all'; use Parse::CPAN::Meta; use File::Spec::Functions qw(catfile); use Test::More tests => 2; BEGIN { diag( "Testing My::Module" ); use_ok( 'My::Module' ); } my $loaded_ver = $My::Module::VERSION; my @metafiles = qw( META.json META.yaml ); my $md; for (@metafiles) { my $mf = catfile('..', $_); next unless (-f $mf); $md = Parse::CPAN::Meta->load_file($mf); next unless (defined $md); if ($md->{version} == $loaded_ver) { pass('version check'); } else { BAIL_OUT('Expected ver ' . $md->{version} . ", got ver $loaded +_ver"); } } BAIL_OUT('Missing META.json and META.yaml') unless (defined $md);

    Edit: Corrected typo in title.

    Update: Use catfile()

I love Perl and I love Mojolicious
1 direct reply — Read more / Contribute
by coetry
on Jul 31, 2017 at 00:53
    I haven't done any web development in perl before today and I started with Mojolicious. I've worked with Rails apps before and that's where I learn about models, views, controllers, routes, etc. I understood what happens when a user sends a request from the client and how that request gets handled and served back by the browser. The Ruby ecosystem has a ton of gems (libraries) specifically tailored towards Rails and its definitely a huge full featured framework sort of like how Perl is such a full featured language. I've also actually played with other ruby web frameworks like Sinatra. I'm able to say with my experience with other languages and frameworks that web development with Perl and Mojolicious is very fun. I'm new to Perl in general but have fallen in love with its free-form syntax, plethora of operators, expressiveness, clever community, and of course the documentation is unparalleled to anything I've seen elsewhere. The fact that Mojolicious makes it so easy to expose our crafty Perl hacks from our terminals to the web in such an organized, clean, simple, and fun manner truly makes me smile. I do know that Mojolicious isn't the only option out there to do this. I've heard great thinks about Dancer2 and other options as well. I'm not sure what I'll end up building, but maybe I'll share snippets of that journey in due time!
Perl Security Testing
4 direct replies — Read more / Contribute
by zentara
on Jul 24, 2017 at 10:02
    Hi, the Test Driven Development, for software and for pancakes node interested me, and I went off on a tangent from talexb's original meditation. So I post a new meditation, with my reply as a starter.

    Original reply: ##########################

    I'm a total amateur compared to you fellows, but I do find when I write my code, for the first draft, I almost always print out arrays and variables after everytime I use them. I almost always get things wrong the first time thru, so my method is very helpful to me.

    It's my guess is that the reason TDD failed is that the Test that you didn't account for, is the one that causes the bug, ( if any).

    What is more worring to me is the security vulnerabilities which Perl5 is susceptible to.

    For instance, could a normal or guest user on your machine, with access to Perl scripts, cause a buffer-overflow of some sort, and gain root access? I'm sure the NSA would pay for that information. :-)

    How safe is Perl out there in the wild? Are systems being hacked thru Perl? As far as know, Perl has been very safe in my limited use. I guess security is the number one test.

    So what do you experts feel, know, and or are hiding concerning Perl's security, assuming the scripts are written and run correctly? Was there ever a real buffer overflow exploit? etc

    Should I worry about other users on my linux box getting root escalation if I let them login?

    I'm not really a human, but I play one on earth. ..... an animated JAPH
Test Driven Development, for software and for pancakes
3 direct replies — Read more / Contribute
by talexb
on Jul 23, 2017 at 11:16

    It's Sunday morning in Toronto, so that means pancakes for breakfast.

    I use the same recipe from the Joy of Cooking that I've used for the last fifteen years or so, which starts with me heating up my two cast iron pans to a 4.5 setting. After making the batter, I make just one pancake in the middle of each pan, and check that they cook at the right speed to confirm I have the correct pan temperature. And I taste the finished pancake, to make sure I haven't messed up the recipe. (Yes, it's possible to forget a crucial ingredient when following a well-worn recipe -- it's amazing what forgetting salt does do a dish.)

    I make pancakes four to a pan, so they're 'silver dollar' pancakes, not more than three inches across. I monitor the colour and the speed at which they're cooking as I go, adjusting the heat if necessary. Finished pancakes go into a covered dish to stay warm.

    I believe I'm a careful cook, and I'm also a careful developer. That means test early, and test often. You want no surprises when it's time to dish up the goods to your family (pancakes) or your customers (software). Dreaming up new dishes or new applications it's important to be creative, to try new approaches. Cooking or writing software, you have to be methodical, thoughtful, and meticulous.

    I consider myself very lucky to have gone to a really good university, but what I studied (Systems Design Engineering) bears only a tangential relationship to the work I do now. It was actually really good training at reading/comprehension, time management, mental gymnastics, and getting the job done. As long as another program had the same goals, I think pretty much any degree or diploma would have done. An Engineering degree from Waterloo just helped me get my foot in the door that much easier.

    Conclusion? Do your development with tests, right from the start. You'll thank yourself, again and again. And it makes for good pancakes. ;)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Perl, JavaScript and Strandbeests
2 direct replies — Read more / Contribute
by cavac
on Jul 20, 2017 at 08:19

    A little over a year ago, i have been playing with AI::Genetic, Javascript::V8 and Strandbeest evolution for a Linuxdays Demo. I wrote this originally as a distributed genetic evolution demo, complete with managment web interface, but for the sake of clarity, here is the single-user command-line version of it.

    (I might do another meditation later that show more of the distributed stuff, but my Raspberry Pi cluster is still in a packing crate in the attic... i hope... after moving house in a chaotioc fashion).

    Before i begin, you should note that the Javascript::V8 version on CPAN has a memory leak. You might try my own, non-official version at It is in a mercurial repo, so you should be able to do this:

    hg clone cd JavaScript-V8 perl Makefile.PL make test install distclean

    The magic of Strandbeests

    I was always fascinated my Strandbeests, the huge moving "animals" created by Theo Jansen. Although mostly made out of PVC tubes, they exhibit a really strange, lifelike movement.

    The key part of this creatures of the length relationship between their eleven leg parts that lets them walk with very little effort. As Theo put it: "But even for the computer the number of possible ratios between 11 rods was immense. Suppose every rod can have 10 different lengths, then there are 10,000,000,000,000 possible curves. If the computer were to go through all these possibilities systematically, it would be kept busy for 100,000 years. I didn't have this much time, which is why I opted for the evolutionary method."

    Computers? Doing evolution? Hell yeah, i needed to learn how thats done!

    Why JavaScript?

    While i'm a competent developer when it comes to PostgreSQL databases, HTML and similar stuff, a lot of the math required for analyzing and evaluating the movement of a Strandbeest leg is completely outside my comfort zone (i.e. i have absolutely no clue how to do it).

    Thankfully, someone on Stackoverflow made a nice Javascript demo, which (for my Linuxdays thing) also solved the HTML visualization problem for me.

    Perl to the rescue

    This is where i came in. Now i had two options. Either convert the JS code to Perl (and keep it compatible with the JS version, so the HTML visu was in sync), or just use Javascript without the drawing functions from within Perl. I chose the latter, because it seemed easier.

    To run the genetic evolution algorithm itself, i chose the AI::Genetic module, since this seemed the easiest interface for what i needed.

    Before we begin, we need to define (which is a simplified copy from the one in my maplat_helpers repo):

    We also need James Coglan's Sylvester Vector and Matrix library for JavaScript, saved as "sylvester.js"

    And last but not least in our list of "support" files, the Strandbeest evaluation function itself, saved in a file with the name "strandbeest.js". This is the one with all the graphical functions removed:

    Now that we have to copied portions of our code, let's write some of our own.

    This is basically our caller for the main module and runs the main loop. First we check for some command line arguments, set up the evolver, and then run in an infinite loop.

    We want to support 4 different arguments:

    • --load Loads previous best 3 fits from fittest.dat and seeds the population with them.
    • --save Every time we find a new best fit, we save the best 3 members of the population to fittest.dat.
    • --population_size=1000 Set how many members does each generation have.
    • --crosspolination_count=10 Set the level of cross-polination during evolution.

    Here is the code (file ""):

    Most of what does, is a pretty straight forward wrapper around AI::Genetic, except the Javascript-Handling:

    use JavaScript::V8; ... my $vectorjs = slurpBinFile('sylvester.js'); my $strandbeestjs = slurpBinFile('strandbeest.js'); my $js = $vectorjs . ' ' . $strandbeestjs; ... my $ok = -1; my $errortype = 'MATH_ERROR'; my %scores; my $ctx = JavaScript::V8::Context->new(); $ctx->bind_function(write => sub { print @_ }); $ctx->bind_function(setFailed => sub { $ok = 0; $errortype = shift @_; }); $ctx->bind_function(setFinished => sub { $ok = 1; %scores = @_; }); $ctx->bind(params => \%params); $ctx->eval($js); my $error = $@; my $total = -1000; # Default: Failed! if(defined($error)) { print("SCRIPT ERROR: $error\n"); } elsif($ok == -1) { print("Script didn't call setFailed() or setFinished()\n"); } ...

    Here is the full code of

    While i'm a bit sparse here with explanations (never was the type who could do the teaching stuff), it should give you a nice overview of both how to run Genetic algorithms in Perl, as well as how to use existing JavaScript code to solve a problem.

    Additional information

Fun with Prototypes
4 direct replies — Read more / Contribute
by haukex
on Jul 19, 2017 at 07:54

    Can you tell me:

    1. How does this get parsed? (e.g. try adding parentheses to show precedence)
    2. Given that the body of each of the four subs is { print Dumper(\@_) }, what is the output?
    $_ = "#x"; my @a = qw/b c/; foo /#(\w+)/, @a; bar /#(\w+)/, @a; quz /#(\w+)/, @a; baz /#(\w+)/, @a;



Add your Meditation
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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2017-12-17 22:04 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (466 votes). Check out past polls.