Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot


( #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
A small Deity AI class system
4 direct replies — Read more / Contribute
by holyghost
on Nov 05, 2017 at 09:49
    ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package HollyGameAI::AIInterface; our @ISA = "Interface"; sub AIInterface { my $class = shift; $self = $class->SUPER::Interface(qw(@_)); ### e.g. qw(swim fly +) bless $self, $class; } ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package HollyGameAI::Factory; our @ISA = "HollyGameAI::Interface"; sub Factory { my $class = shift; my $self = $class->SUPER::Interface(@_); ### include abstract method names return bless $self, $class; } ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. ### with thanks to gregorovius from perlmonks package HollyGameAI::Interface; use Carp; sub Interface { my $class = shift; my $self = {inheritors => (), abstract_methods => shift, ### e.g. qw(swim fl +y) }; bless $self, $class; } sub import { my $method = caller; push (@{ $self->{inheritors}}, $method); } sub INIT { my $bad = 0; for my $class ($self->{inheritors}) { for my $meth ($self->{abstract_methods}) { no strict 'refs'; unless (defined &{"${class}::$meth"}) { $bad = 1; warn "HolyGameAI : Class $class should + implement HolyGameAI Interface but does not define $meth.\n"; } } } croak "HollyGameAI : Source compilation aborted at interface b +inding time\n" if $bad; } ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package HollyGameAI::MutualExclusiveAI; use lib "../HollyGameAI"; use Factory; sub MutualExclusiveAI { my $class = shift; my $self = { aiclass => HollyGameAI::Factory->Factory(@_) }; bless $self, $class; } ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package HollyGameAI::MutualExclusiveDeityAI; our @ISA = "MutualExclusiveAI"; sub MutualExclusiveDeityAI { my $class = shift; my $self = $self->SUPER::MutualExclusiveAI(qw(cast donate swim + fly empower)); bless $self, $class; } ### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package HollyGameAI::RNG; ### Random Number God, dice class sub RNG { my $class = shift; my $self = { dx => 0 }; return bless $self, $class; } sub set { my ($self, $dxx) = @_; $self->{dx} = $dxx; } sub rollDX { my $self = shift; return rand($dx); } sub rollD1 { my $self = shift; return rand(1); } sub rollD3 { my $self = shift; return rand(3); } sub rollD6 { my $self = shift; return rand(6); } sub rollD10 { my $self = shift; return rand(10); } sub rollD20 { my $self = shift; return rand(20); } sub rollPreviousDX { my $self = shift; return rand($self->{dx}); } sub roll { my ($self, $dxx) = shift; $self->set($dxx); given ($self->{dx}) { when ($_ = 0) { return 0; } when ($_ == 1) { return rollD1; } when ($_ == 3) { return rollD3; } when ($_ == 6) { return rollD6; } when ($_ == 10) { return rollD10; } when ($_ == 20) { return rollD20; } } return 0; }
The Perl Paradox
3 direct replies — Read more / Contribute
by reisinge
on Oct 29, 2017 at 07:20

    An interesting meditation by Tom Radcliffe of ActiveState.

    In general, they do what you want, unless you want consistency. -- perlfunc
[Perl 6]: Small discoveries VII, Flattening
3 direct replies — Read more / Contribute
by holli
on Oct 28, 2017 at 19:16
    Perl 6 tries to flatten lists. This:
    my $a = [[["hello"]]]; #not a 3d array!, same as: ["hello"]
    is not what you might think it is, as single element lists get flattened. To get what you mean you must write
    my $a = [[["hello"],],]; #now it is!
    Note the trailing comma. See also 2015 The Year of The Great List Refactor.


    You can lead your users to water, but alas, you cannot drown them.
Code Structure Changes
8 direct replies — Read more / Contribute
by Anonymous Monk
on Oct 27, 2017 at 15:17
    You are usually hired to change the code to achieve a change in functionality. You begin to think that if the code is written in a different way, this type of problem could easily be solved. Having short of time, you always try to change the code and commit it, but the idea that you have power to change the structure of code and you should do it, keeps bothering you. What you should do?
Variable-Width Lookbehind (hacked via recursion)
1 direct reply — Read more / Contribute
by haukex
on Oct 24, 2017 at 13:43

    Warning: Since this uses recursion it is horribly inefficient and may easily blow up on longer strings. If you think you need this for variable-width lookbehind, then first think about how you might solve this with other techniques like lookahead, which is variable-width out of the box, or simply with multiple regular expressions. /Warning The following is presented as a curiosity as the result of the discussion here - thank you LanX and QM for providing the inspiration :-)

    Zero-width Lookaround Assertions are incredibly useful, but unfortunately the lookbehind assertions (?<=pattern) and (?<!pattern) are restricted to fixed length lookbehinds, and sometimes you just really want to be able to say something like e.g. (?<=ab+.*)c. With the following technique, you can emulate these kinds of variable-width lookbehind assertions.

[Perl 6]: Small discoveries VI, die
2 direct replies — Read more / Contribute
by holli
on Oct 19, 2017 at 18:41
    In Perl 6, die still prints an error to STDERR and exits (unless caught), however adding a newline to the end of the error message will produce a stack trace.

    The idiom for printing an error message and stopping the program in Perl 6 is:
    note "Error Message" and exit 42; # or some other number (except 0)


    You can lead your users to water, but alas, you cannot drown them.
[Perl 6]: Small discoveries V, True / False / FileNotFound
1 direct reply — Read more / Contribute
by holli
on Oct 19, 2017 at 13:54
    Omg, I love this. Did you ever have a clear, slick little function that needs to return a boolean, and you also want to communicate an error condition? You basically have the choice of returning two values, reversing the consuming condition (meaning an empty return value be considered true), or using a string reference as an argument to the function.

    Witness Perl 6:
    sub slick() { if do-stuff { return "SomeValue"; } else { return "Some error message" but False; } } if my $result = slick { process( $result ); } else { log-error( $result ); }


    You can lead your users to water, but alas, you cannot drown them.
Be prepared for CSV injections in spreadsheet
3 direct replies — Read more / Contribute
by Tux
on Oct 18, 2017 at 07:34

    Read this article to get an idea of how dangerous it can be to blindly accept macro's in spreadsheets. Be it MS Excel or Google spreadsheets, they all suffer.

    You cannot blame CSV for it. CSV is just passive data.

    Once you load or open a CSV file into something dangerous as a spreadsheet program that allows formula's to be execcuted on open, all bets are off. Or are they?

    The upcoming Text::CSV_XS has added a new feature to optional take actions when a field contains a leading =, which to most spreadsheet programs indicates a formula.

    On both parsing and generating CSV, you will be able to specify what you want to do (where "formula" does not go beyond the fact that the field starts with a =):

    • Do nothing special (default behavior) and leave the text as-is
    • Die whenever a formula is seen
    • Croak when a formula is seen
    • Give a warning where a formula is seen
    • Replace all formulas with an empty string
    • Remove all formulas (replace with undef

    Code speaks loader than words ...

    I'm pretty pleased with the diagnostics

    $ cat formula.csv a,b,c 1,=2+3,4 6,,7,=8+9, $ perl -MCSV -e'$_ = dcsv (in => "formula.csv", bom => 1, formula => " +diag")' Field 2 (column: 'b') in record 1 contains formula '=2+3' Field 4 in record 2 contains formula '=8+9'

    Expect this to be available by next week.

    Enjoy, Have FUN! H.Merijn
Perl6 discoveries ó floating-point
2 direct replies — Read more / Contribute
by Grimy
on Oct 18, 2017 at 06:59
    Anonymous Monk brought up a really interesting discovery here. Unfortunately, that thread got derailed, so Iím making a separate one, as suggested by Your Mother. One of the first things I found while testing is this really interesting tidbit:
    $ perl6 -e 'say 0.99999999999999999000001' 1.000000000000000073886090 $ perl6 -e 'say 0.99999999999999999000001 > 1' True
    But then I realized I was using an outdated Rakudo (2017.04). So I updated to 2017.09, and now those print 1 and False, respectively. Thereís still some interesting behavior in 2017.09, though:
    $ perl6 -e 'say 0.7777777777777777777770' 0.77777777777777785672697 $ perl6 -e 'say 0.7777777777777777777771' 0.777777777777777767909129
    Note that the second number printed is strictly smaller than the first one, even though the second source number is strictly larger than the first one, spelled in the same fashion and to the same number of significant digits! However, comparison and subtraction still return exact results:
    $ perl6 -e 'say 0.7777777777777777777771 > 0.7777777777777777777770' True $ perl6 -e 'say 0.7777777777777777777771 - 0.7777777777777777777770' 1e-22
    Okay, thatís probably because one is a Num and the other is a Rat, so letís convert everything to Num explicitly:
    $ perl6 -e 'say Num(0.7777777777777777777770)' + 0.777777777777778 $ perl6 -e 'say Num(0.7777777777777777777771)' 0.777777777777778 $ perl6 -e 'say Num(0.7777777777777777777770) > Num(0.7777777777777777 +777771)' True $ perl6 -e 'say Num(0.7777777777777777777770) - Num(0.7777777777777777 +777771)' 1.11022302462516e-16
    Huh. Now they print the same, but theyíre still different numbers when compared. Note that the sign of the difference got switched:
    $ perl6 -e 'my $a = 0.7777777777777777777770; my $b = 0.77777777777777 +77777771; say $a <=> $b; say Num($a) <=> Num($b)' + Less More
    Also interesting is that many Nums donít survive a round-trip to Str:
    $ perl6 -e 'my $a = Num(1/9); say $a == Num(Str($a))' False
    Can anyone point me to the Perl6 specs/docs/whatever that explain those behaviors?
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"2.

    Updates - Clarifications:
    1 The class attribute should be exactly the string data (that is, ignoring the special treatment given to CSS classes). Examples below updated accordingly.
    2 Your solution should be generic enough to support any arbitrary strings for the id and text content, and be easily modifiable to change the expected class attribute.

    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:

    Honorable mentions: Grimy for a regex solution and RonW for a regex-based parser :-)

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

    Update 2017-10-18: Thank you very much to everyone who has replied and posted their solutions so far, keep em coming! :-)

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.

    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


    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


    You can lead your users to water, but alas, you cannot drown them.
Paragraph grep: request for testing, comments and feedbacks
3 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 --
    Here is the latest (to the moment of creating this message) version of the script: Thank you

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 taking refuge in the Monastery: (7)
    As of 2018-03-17 11:44 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (224 votes). Check out past polls.