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
Parsing HTML/XML with Regular Expressions
8 direct replies — Read more / Contribute
by haukex
on Oct 16, 2017 at 07:48

    Your employer/interviewer/professor/teacher has given you a task with the following specification:

    Given an XHTML file, find all the <div> tags with the class attribute "data"1 and extract their id attribute as well as their text content, or an empty string if they have no content. The text content is to be stripped of all non-word characters (\W) and tags, text from nested tags is to be included in the output. There may be other divs, other tags, and other attributes present anywhere, but divs with the class data are guaranteed to have an id attribute and not be nested inside each other. The output of your script is to be a single comma-separated list of the form id=text, id=text, .... You are to write your code first, and then you will be given a test file, guaranteed to be valid and standards-conforming, for which the expected output of your program is "Zero=, One=Monday, Two=Tuesday, Three=Wednesday, Four=Thursday, Five=Friday, Six=Saturday, Seven=Sunday".

    1 Update: Clarification: The class attribute should be exactly the string data (that is, ignoring the special treatment given to CSS classes). Examples below updated accordingly.

    Ok, you think, I know Perl is a powerful text processing language and regexes are great! And you write your code and it works well for the test cases you came up with. ... But did you think of everything? Here's the test file you end up getting:

    I encourage everyone to try and write a parser using your favorite module, be it:

    I'll kick things off with Mojo::DOM (compacted somewhat, with potential for a lot more golfing or verboseness):

Orbital starters
2 direct replies — Read more / Contribute
by holyghost
on Oct 10, 2017 at 04:50
    I started out on some code for making molecular orbitals, here'sthe beginning for atomic oribtals :
    package orbital2::TimeD; sub new { my ($class,$d) = shift; my $td = $d; bless $self, $class; return $self; } package orbital2::TimeDelta; sub TimeDelta { my ($class) = shift; bless $self, $class; return $self; } sub Tick { my ($class) = shift; my ($seconds, $interval, $milliseconds) = shift; ### 2 args my $hope = undef; bless $self, $class; } sub TickCalculate { my ($self) = shift; return my $self->hope = $self->$seconds / $self->interval } package orbital2::TimeF; sub new { my ($class,$f) = shift; my $tf = $f; bless $self, $class; } package orbital2::TimeP; sub new { my ($class,$p) = shift; my $tp = $p; }
[Perl6] Small 6 discoveries V, Sigils
1 direct reply — Read more / Contribute
by holli
on Oct 08, 2017 at 19:02
    Some people like sigils. Some people don't. In Perl 6 you don't have to use them (as much).
    my $x = 1; say $x; my \y = 1; say y; my @x = 1, 2; say @x; my \y = @ = 1, 2; say y; # also works with signatures sub foo(\x) { say x }; foo(1);
    However you can't use sigilless attributes
    class ThisExplodes { has \.y; #doesnt work has \y; #neiter does this }
    You can use this for example to distinguish normal variables from ones in closures, or lexicals from function arguments or whatever. Or maybe you event want to get rid of all sigils where possible.

    The choice is yours.


    Addendum
    Sigilless variables do not create containers and are always immutable after initialization. They are therefore not a simple replacement with different visuals.

    Edit: Fixed typo as hinted by NetWallah


    holli

    You can lead your users to water, but alas, you cannot drown them.
[OT] Slashdot is 20
2 direct replies — Read more / Contribute
by 1nickt
on Oct 05, 2017 at 14:45

    If you are of a certain age and have been developing for the web for long enough, you will recognize a lot of the details in CmdrTaco's meditation on the beginnings of Slashdot on its 20th birthday, from watching the output of tailing your Apache referrers log in amazement, to the Kai's Power Tools drop shadow on the logos. Good times! (Too bad he doesn't mention that the whole thing was built in Perl.)


    The way forward always starts with a minimal test.
[Perl6] Perl 6 discoveries IV, hash access
2 direct replies — Read more / Contribute
by holli
on Oct 04, 2017 at 19:41
    Perl 6 has different ways to access hash elements:
    use v6; my %hash = :a<A>, :b<B>; say %hash{'a'}; # says A say %hash<b>; # say B
    This however leads inevitably to
    #most likely not what you want, but prints an undefined warning say %hash<$somekey>;
    which is a hard to spot bug since these: <> do not interpolate. That did just cost me quite a while.

    Edit: Renamed as per advice


    holli

    You can lead your users to water, but alas, you cannot drown them.
Paragraph grep: request for testing, comments and feedbacks
2 direct replies — Read more / Contribute
by siberia-man
on Oct 04, 2017 at 14:30
    Hello Monks, I came here for your critics, feedbacks and proposals for improvements. I have developped the simple script for grepping paragraphs (block of text lines delimited by the specific separator (blank lines, by default).

    The common use case is parsing of java log entries that can be extended onto multiple lines:
    paragrep -Pp '^\d+/\d+/\d+ \d+:\d+:\d+' PATTERN FILENAME
    Another use case is filtering sections from ini files matching particular strings:
    paragrep -Pp '^\[' PATTERN FILENAME
    For now I am going to improve searching patterns and add support for -a/--and and -o/--or options to control matches. Using this message I ask you to test the script and point me on possible leaks in performance and efficiency.

    The original and actual code is hosted on github (It's not permitted to post external links but you can search for ildar-shaimordanov/perl-utils)
    Here is the latest (to the moment of creating this message) version of the script: Thank you
Lesson Learned: not all 32b perls are created equal
1 direct reply — Read more / Contribute
by pryrt
on Sep 30, 2017 at 11:22

    Given the pretty CPAN Testers Christmas Tree, especially in the *BSD columns, I learned that not all 32-bit perls are created equal. After some more debug, I was able to show that it was those 32-bit perls with $Config{ivsize}==4 (32bit integers; aka perl -V:ivsize) that were failing, but those with $Config{ivsize}==8 (64bit integers in 32bit perl) would pass.

    I had assumed (without looking) that the default ivsize on the 32bit perl was 4 bytes (32 bits), so thought that I had already tested and verified my IV weren't overflowing (or, if they were, they were promoting to NV). After seeing the problem, I discovered that when using left-shift, it would go from IV to NV... but it didn't. However, *=2 did promote the way I expected:

    Conclusion: when doing a testing suite across versions, it's not always enough to just have a "32bit perl"; especially if you are dependent on integer sizes, also check that your test suite includes multiple ivsize values.

[Perl6] Small discoveries I, __DATA__
2 direct replies — Read more / Contribute
by holli
on Sep 27, 2017 at 10:38
    Perl6 knows no __DATA__ construct. One can however, use the =finish pod marker and the $=finish variable for similar effect.
    use v6; say $=finish; # says "foo" =finish foo
    Edit: Renamed as per advice


    holli

    You can lead your users to water, but alas, you cannot drown them.
[RFC] File::Replace
2 direct replies — Read more / Contribute
by haukex
on Sep 20, 2017 at 05:50

    Many of you are probably aware of the pattern of opening a temporary file, reading from the original file and writing the modified contents to the temporary file, and then renameing the temporary file over the original file, which is often an atomic operation (depending on OS & FS). I recently wrote a module to encapsulate this behavior, and here is one of three interfaces that are available in File::Replace. There are several options to configure the behavior, including the ability to specify PerlIO layers, what happens if the file doesn't exist yet, etc.

    use File::Replace 'replace2'; my ($infh,$outfh) = replace2($filename); while (<$infh>) { # write whatever you like to $outfh here print $outfh "X: $_"; } close $infh; # closing both handles will close $outfh; # trigger the replace

    Since I hope this is something that you might find useful, I would be happy about any feedback you might have!

    To give a practical example, here is an update of my code from this node. As you can see I was able to get rid of eight lines of fairly complicated code, while keeping the main loop entirely unchanged. The module also adds some more robustness, as it incorporates a few more checks on whether operations were successful or not.

Encoding Decoding on multiple formats RFC
2 direct replies — Read more / Contribute
by thanos1983
on Sep 19, 2017 at 06:44

    Hello fellow Monks,

    I am looking for your advice on updating and my implemented module for encoding and decoding multiple formats. I wrote the module and tried to include as many formats I could. I know that there other formats that I have not added but in my case during the encoding decoding process has to be also converted to hex and vise versa, where I found problems with more formats that I have not included on my sample of code.

    The whole idea behind the module, I am working for a telecommunication company and part of my daily job is to correct problems. The languages can vary globally since it is a live network with live customers and the format is in hex on a variety of encoding patterns. I had some cases that I had to create small scripts to process the packages before and after the nodes so I can observe encoding corruptions or not. Sample of previous questions that I was working that are similar with the module (Chinese to Hex and Hex to Chinese, Arabic to Hex and Hex to Arabic). After seeing my self that I need more and more encodings for more and more languages I end up saying that I need to write a simple module to do that for me instead of creating more or less the same code again and again.

    So having said that, sample of code as the user would use the module based on the encodings that can be handled:

    The actual module, that I still have not found a good name to apply. Any ideas for naming please feel free to propose.

    The module by it self is extremely simple, but at the same time on my position and for my colleagues is extremely useful. Any suggestions on code or any other improvement please feel free to suggest.

    Hope this tiny module will help others also.

    BR, Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Kindness and support for Meredith
No replies — Read more | Post response
by stevieb
on Sep 18, 2017 at 11:47

    This isn't about Perl; it's about the community.

    Early last week, I wrote in CB about a tremendously disturbing event that took place with my family.

    In response, several Monks reached out to offer condolences and offers of help.

    In my near absence from here since then, a bunch of Monks got together, and 1nickt reached out a few times to say that a group of Monks wanted to do something. Initially, I was advised that the offer could be in the form of finance for travel etc. After I carefully deliberated this kind gesture and discussed with my wife, I decided that I wouldn't feel comfortable taking any funds directly, so I let Nick know that it would be preferred to send flowers or donate to a charity instead.

    I was advised by Nick that a beautiful arrangement had been sent on behalf of the Monks, and any left over funds plus any more funds that may trickle in would be donated to some form of preventing violence charity. I advised Nick that I was too busy to deal with it, so I asked if he'd spearhead the decision of which one.

    I want to express my (and my wife's) deepest gratitude for such an overwhelmingly kind gesture by everyone involved; those who provided funding, as well as those who reached out to offer emotional support. I'd like to thank Nick directly as well for taking the time to organize everything he did.

    This goes to show that this is a great place of caring, not just another forum to get help with questions.

    Perlmonks is the only group I let in on what had happened, as it's the only online forum where I feel so comfortable, and people here came through with flying colours... the manner was absolutely unexpected; stunning actually.

    Thank you very much everyone, it's kind of hard to put into words, so instead, I'll just try to get back into the groove and give back the best way I can; by continuing to help those who need it here.

    Cheers,

    -stevieb

More Betterer Game of Life
2 direct replies — Read more / Contribute
by eyepopslikeamosquito
on Sep 18, 2017 at 03:11

    Without good design, good algorithms, and complete understanding of the program's operation, your carefully optimized code will amount to one of mankind's least fruitful creations - a fast slow program.

    -- Michael Abrash

    In High Performance Game of Life, I chose a very simple design, storing all live cells in a single set. Though pleasing for its simplicity and unboundedness, its drawback is that counting live neighbours becomes a hash lookup, a chronic performance bottleneck. What to do?

    Rather than spending more time optimizing my original design -- thus creating a "fast slow program" -- I researched the domain, learning of many different ways to do it. From the many possible approaches, I chose the simplest one I could find that looked interesting and enjoyable, and implemented it in pure Perl.

    To try to keep my initial attempt short and understandable, I started with a simplified version based on the the brilliant works of Adam P. Goucher (apg), tiling the universe with 64 x 64 tiles in a conventional way, each tile having eight neighbours. Note that this was chosen for simplicity; more efficient schemes are available, such as the "brick wall" tiling used by Goucher in later versions. For background on the concept of breaking the game of life universe into overlapping tiles, see this description of Life128 and vlife.

    My code is loosely based on apgnano (version 2) but advances one tick at a time (rather than two at a time, as apg did) and does not attempt to use universe history. Fair warning though. Despite striving to keep the code simple and short, it's way more complex than my original, Organism.pm swelling from 66 lines of code to 414.

    Benchmark Results

    I've updated the benchmark results given in my original node. As you can see, even this simplified version, with no attempts made at code optimization, is already an order of magnitude faster than the optimized version of the original.

    Version375K cells750K cells1.5 million cells3 million cells
    new Organism.pm (see below)1 secs1 secs3 secs5 secs
    Organism.pm (Mario improvements)13 secs26 secs52 secs108 secs
    Organism.pm (Original)35 secs70 secs141 secs284 secs
    Game::Life::Infinite:Board37 secs96 secs273 secs905 secs

    As for memory use, the maximum Windows Private Bytes used for the three million cell case by each process was:

    • New Organism.pm (see below): 700,000K - 1,100,000K (update: seems to vary)
    • Organism.pm (Original): 1,455,004K
    • Organism.pm (Mario improvements): 1,596,368K
    • Game::Life::Infinite:Board: 18,138,504K

    Benchmark timings running AppleFritter's Lidka test for 30,000 ticks were:
    VersionLidka 30,000 ticks
    new Organism.pm (see below)58 secs
    Organism.pm (Mario improvements)450 secs
    Organism.pm (Original)1635 secs
    Game::Life::Infinite:Board640 secs

    Improving My Initial Attempt

    There is certainly plenty of scope for improving my initial attempt. After all, I have not attempted any optimizations at all, just tried to implement ideas from apg's C++/assembler programs in a pure Perl form in a simple and clear way. While all feedback is welcome, I'm especially eager to see:

    • Refactorings that make the Perl code shorter, clearer, more idiomatic.
    • Performance optimizations.
    • Explanations of (and alternatives to) the bit-twiddling code below, specifically the bit operations in st64_tiletick() below I find especially hard to follow.
    • Bug fixes. I was shocked when my code worked the second time I ran it - just one coding blunder was corrected before my new Organism.pm passed tgol.t, tgol2.t, tgol3.t and the 30,000 lidka test! So I suspect there may be more bugs lurking in this brand new implementation.
    As a minimum, any code refactorings should be tested by running tgol.t and tgol3.t from my original node. Note that this new version of Organism.pm is (or should be) 100% interface compatible with my original.

PBP in German out of print - solutions?
5 direct replies — Read more / Contribute
by LanX
on Sep 11, 2017 at 08:57
    Hi

    I'm in the perverted situation that I want to equip my team with prints of Perl Best Practices and the German version is not only out of print, but O'Reilly is even not providing an eBook Version.

    In Germany it's legal to photocopy books which got out of print after two years.

    My best idea at the moment is to resort to copying relevant chapters of the German version and buying same amount of English version as compensation for bad conscience.

    What you think?

    PS: My colleagues do speak some kind of English but I don't want to accept the loss of productivity here.

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

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.

    Laurent.

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

    When I went to eclipse.org 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.


Add your Meditation
Title:
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.