Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

The Monastery Gates

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

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Keeping relationally child objects alive with Moo
3 direct replies — Read more / Contribute
by SimonClinch
on Sep 21, 2017 at 06:24
    Hi Monks, The concept of inheritance carries with it a concept of parent and child, but what if I want a relational parent/child model?

    So using Moo, I tried this:

    package Myproject::Model; use Moo; has table => (is => 'rw'); sub newtable { my ($self, $table) = @_; my $tob = Myproject::Model::Table->new( # also uses Moo and define +s these ro attributes:- name => $table, model => $self, ); $self->table or $self->table({}); my $tables = $self->table; $tables -> {$table} = $tob; } 1;
    Problem is that sometimes a $tob gets destroyed in spite of being nested in a living model object.

    But rather than hack my way around what I did, I am wondering if I am missing a tried and tested way of storing relationally child objects in their relationally parent object (including if necessary upgrading from Moo to Moose). Many thanks in advance for suggestions (I am looking for architectural insight/tips rather than merely fixing the code I already have).

    One world, one people

Problem upgrading XML::Fast from 0.11 to 0.17
No replies — Read more | Post response
by mje
on Sep 21, 2017 at 05:33

    I've been using XML::Fast to process XML files for some time and successfully. However, the code was moved to a newer machine and has stopped working in some circumstances. A difference between the machines is XML::Fast version, 0.11 on original machine (working) and 0.17 on new machine (not working). When no other changes are made but to upgrade to 0.17 on the old machine it also stops working.

    The error I'm getting is:

    Failed to encode 2017-9-21T08-49-17.XML to JSON for indexing - malform +ed or illegal unicode character in string [�ndby IF], cannot c +onvert to JSON at line 1827.

    The XML file comes from a 3rd party and is ISO-8859-1 encoded. The bit it is complaining about is <Value>Br<F8>ndby IF</Value>. A cut down version of the XML which fails is:

    <?xml version="1.0" encoding="ISO-8859-1"?> <xx feedtype="delta"><Timestamp CreatedTime="2017-09-21T06:49:17" Time +Zone="GMT"/><Value>Brøndby IF</Value></xx>

    The code which is now failing is:

    use Cpanel::JSON::XS; use XML::Fast; sub esIndexFile2 { my ($self, $file) = @_; my $xml = do { local $/ = undef; open (my $fh, "<:encoding(ISO-8859-1)", $file) or die "Failed +to open $file - $!"; <$fh>; }; $xml =~ s/^(?:.*\n)//; # remove first line - the encoding lin +e my $hash; eval { $hash = xml2hash $xml; }; if (my $ev = $@) { warn("Failed to parse file $file for indexing - $@ - SKIPPING" +); return; } my $json = eval { encode_json($hash); # <------------ fails here }; if (my $ev = $@) { $self->logwarn("Failed to encode $file to JSON for indexing - +$@ - SKIPPING"); return; } return 1; }

    The changes file for XML::Fast is not too helpful. I have discovered adding utf8decode => 1 to the xml2hash makes it work now but I don't really understand why. I am doing anything wrong here? What might have changed in XML::Fast to cause this to happen?

Trouble with Perl Heroku buildpack
1 direct reply — Read more / Contribute
by BeneSphinx
on Sep 20, 2017 at 23:58

    Hello Perl Monks

    I ask for your wisdom from a position of great humbleness. I have tried to get Perl running in Heroku, but this simple task is eluding me for now. I have started with the buildpack here:

    My understanding is that all I need is an app.psgi file, a cpanfile, and a Procfile. My procfile just calls out to starman, which I think (but don't know) will automatically notice my app.psgi.

    It was building fine back when I didn't have an app.psgi and just a - but of course that wouldn't do anything. Once I changed to app.psgi (hoping that Starman would launch, correctly hook into it, and use my app.psgi to start serving web pages), I started getting the error "Can't locate Plack/ in @INC"

    But in my cpanfile I do indeed have Plack listed as a requirement, as stated on the buildpack homepage: requires 'Plack', '1.0000';. My understanding is that Plack::Runner should come along with that. I guess somehow the buildpack isn't properly installing this, but I'm not sure what to do next.

XS linking problem
1 direct reply — Read more / Contribute
by frazap
on Sep 20, 2017 at 08:41
    I'm trying to replicate the example of a simple XS module given at

    I have this send_string.cpp file

    #include "send_string.h" #include "stdio.h" void send_string (const wchar_t * str) { printf ("string\n"); }
    And the header send_string.h file is
    void send_string (const wchar_t * str);
    The package name is Kbh, and the Kbh.xs file is
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include "send_string.h" MODULE = Kbh PACKAGE = Kbh PROTOTYPES: DISABLE void send_string(s) const wchar_t * s TYPEMAP: <<HERE const wchar_t * T_PV HERE
    My is
    use ExtUtils::MakeMaker; WriteMakefile ( NAME => 'Kbh', VERSION_FROM => 'lib/', OBJECT => 'Kbh.o send_string.o', )
    My lib/ file is
    package Kbh; our $VERSION = '0.01'; require XSLoader; XSLoader::load('Kbh', $VERSION); 1;
    When I run dmake I get the linking error "Kbh.o:Kbh.c:(.text+0x75): undefined reference to `send_string'"

    Thanks for any help


Accent file names issue
7 direct replies — Read more / Contribute
by ruimelo73
on Sep 20, 2017 at 06:41

    I'm portuguese and like so many people that live in countries with latin languages (portuguese, spanish, french, italian, etc) I have to deal with accent file names. Other non-latin languages have the same problem for sure (german, dutch, etc). The context here is Windows using NTFS drives, using Unicode to set up the files names. I'm using the latest perl version, that supports Unicode.

    For example, I have a directory/folder in "c:\users\someuser\documents" named "documentação" ("documentation" in english). The full path will be "c:\users\someuser\documents\documentação". Now, if I do this:

    use strict; our $dp; $dp = "c:\\Users\\someuser\\Documents\\documentação"; if (-d $dp) { print "ok\n"; } else { print "nope\n"; }

    It will return "nope"...
    If I change the text to "documenta\x{00E7}\x{00E3}o", it returns "ok"...
    Printing the string variable will show the same thing...
    If I use opendir/readdir in the "c:\users\someuser\documents" directory it will read "documentação" perfectly and -d will work fine...
    The -d simply does not work with the direct text on the string variable...
    If I add code to set the variable using command line argument in a dos console it will return "ok" also.

    I wasted hours reading unicode and perl documentation, and trying diferent methods (utf8, encoding, deconding, locale, etc) for correcting this, but nothing works. It is a problem with the way perl codifies the string internaly. I suppose that using some sort of perl command line option would do some thing that could solve the issue but this is not the way to resolve this.

    (post edited meanwhile, the solution I have found did not work)

    Unicode is a wonderful thing but reading about the evolution of Unicode you start thinking that Unicode is now on the same level of confusion to what happened to the ancient codepages... I hope that some one teachs me a lesson, or this sort of weirdness can be solved in future versions of perl.

    Thank you / Obrigado.

Perl -T vs Mime::Types
4 direct replies — Read more / Contribute
by roperl
on Sep 19, 2017 at 15:03
    What method is better to test if file is a plain ascii text file. I'm already checking MIME types of non-text file with File::Type.
    Should I check the text files with perl's -T like so:
    if(-T $file) { print "$file is an ascii text file \n"; } else { print "Not an ascii text file \n"; }
    Or check that it matches application/octet-stream MIME type like so:
    my $ft = File::Type->new(); my $type = $ft->mime_type($file); if ( $type eq "application/octet-stream" ) { do this.. } else { do that.. }
[emacs] dual files mangling Perl and Lisp code?
4 direct replies — Read more / Contribute
by LanX
on Sep 19, 2017 at 04:15

    is there a recommended way to mix Perl and eLisp code in one file without caring about escaping, such that it can be executed in both ways?

    Something like

    my $perl; #code ... __DATA__ (ELisp...)

    Just that emacs ignores the Perl part when executing the file?

    There are similar examples with bash and bat...


    Just realising that ; is a comment symbol in lisp and a no op in Perl, this might be simplistic way to do it. ..

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

Inline::C in a Perl module
2 direct replies — Read more / Contribute
by enemyofthestate
on Sep 18, 2017 at 14:33

    I am trying to use Inline::C in a perl module. The C code appears to compile fine (at least there is a .so file in the right place) but the module does not return a true value to the calling program and I cannot figure out how to make that work. I've tried putting the usual "1;" at various places in the code but it either chokes when compiling or tells me:

    " did not return a true value at bin/dldtest.fcgi line 11."

    Anyone know how to make this work?

    This is some stripped down code I am using to test with:


    #!/usr/bin/perl use strict; use warnings; use File::Basename; use FindBin; use lib dirname($FindBin::Bin) . "/modules"; use DLDtest; my ($dlid,@dlfiles) = dld_initialize("lic_file", "123456", 5, "/path/t +o/data/files"); exit 0;

    package DLDtest; use strict; use warnings; use Exporter qw(import); use English; our @EXPORT = qw (dld_initialize); #------------------------------------------------ # initialize dld and open data files #------------------------------------------------ sub dld_initialize { my ($lic_file, $password, $num_files, $dl_format) = @_; my @files = (); # initalize the DLD library my $dlid = dl_DlInit(""); # set the license file and password unless (dl_DlSetLicense($dlid, $lic_file, $password)) { return (0,@files); } # open the DLD files for (my $cntr = 0; $cntr < $num_files; $cntr++) { my $path = sprintf($dl_format, $cntr + 1); my $fileid = dl_DlFileOpen($dlid, $path); $files[$cntr] = $fileid ? $fileid : 0; } return ($dlid,@files); } #------------------------------------------------ # C functions to interface with Pitney library #------------------------------------------------ use Inline (C => Config => DIRECTORY => '/var/www/addrez/Inline', INC => "-I/var/www/addrez/ext.att/include", LIBS => '-L/var/www/addrez/ext.att/lib -ldemolibMT'); use Inline "C"; Inline->init; __DATA__ __C__ #include "dl.h" /*----------------------------------------------- * Initialize the DLD librariy *---------------------------------------------*/ long dl_DlInit (SV* initPath) { return DlInit(SvPV (initPath, PL_na)); } /*----------------------------------------------- * associate a license file and password with dl *---------------------------------------------*/ int dl_DlSetLicense(long dl, char* licenseFile, long password) { return DlSetLicense(dl, licenseFile, password); } /*----------------------------------------------- * open a data file and return FileID *---------------------------------------------*/ long dl_DlFileOpen (long dl, char *path) { return DlFileOpen(dl, path); }
RFC: Math::Triangle (Perl 6)
3 direct replies — Read more / Contribute
by holli
on Sep 17, 2017 at 15:40
    So, I was toying around with Perl 6 lately. The following is a first iteration for a class to calculate all things triangle. I'd be happy about input about the code as I am new to Perl 6 and I am sure there is a lot to be improved. Also, when I post this thing (and the rest of its package), should I name the package Math::Geometry? Or better something less intrusive like Homework::Geometry?

    Edit: The site mangles up the unicode operators. &#9651; is a triangle, &#945; to &#947; are the greek letters alpha to gamma.

(Solved) Extracting sometimes null string from text variable
2 direct replies — Read more / Contribute
by Marshall
on Sep 17, 2017 at 14:24
    I am parsing a $text variable from which I want to extract a Club name.
    I have a solution that "works" as shown below.
    I have been unable to do this in a single regex which handles both of my cases. So I just used 3 statements. This is a pragmatic "do what works" situation and is fine as far as that goes. However, in the search for elegance, I suspect that a single regex could be constructed that handles both cases?

    In case2, there is a new line right after Club:. My attempts at a single regex often wind up capturing Comments: instead of a null string for Club name. Ideas welcome.

    #!/usr/bin/perl use strict; use warnings; my $case1 = "Club: Some Club Comments: "; my $case2 = "Club: Comments: "; foreach my $text ($case1, $case2) { # extract Club name (my $club) = $text =~ /Club:(.*)/; # need to allow for blank Club + name $club =~ s/^\s*//; $club =~ s/\s*$//; print "line before name\n"; # print out to make sure $club doesn' +t have a \n print "extracted Club name: '$club'\n"; print "line after name\n"; } __END__ line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: '' line after name ======== I've tried things like: /Club:\s*(.*)[ ]*\n/ and various other encantations, but that picks up 'Comments:' intead of a blank Club name line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: 'Comments:' line after name
    LanX and Anonymous Monk came up with the ball!
    I hadn't seen \h before, but it works here!
    #!/usr/bin/perl use strict; use warnings; my $case1 = "Club: Some Club Comments: "; my $case2 = "Club: Comments: "; foreach my $text ($case1, $case2) { # extract Club name (my $club) = $text =~ /Club:\h*(.*?)\h*\n/; # need to allow for b +lank Club name print "line before name\n"; # print out to make sure $club doesn' +t have a \n print "extracted Club name: '$club'\n"; print "line after name\n"; } __END__ line before name extracted Club name: 'Some Club' line after name line before name extracted Club name: '' line after name
tkpp fails at very end
3 direct replies — Read more / Contribute
by smh
on Sep 17, 2017 at 01:07

    I am running tkpp to convert my perl script to an "exe" file. my environment is ASP perl 64 bit V5.24 and I have installed "PAR-Packer-1.037". The script I am trying to convert, had no issues when I was using ASP PDK v9.2. Now every thing seems to work find except the end with the following error message at the veru last line in the log file.

    C:\Perl64\site\bin\pp.bat: Failed to extract a parl from 'PAR::Strippe +dPARL::Static' to file 'C:\Users\Shahriar\AppData\Local\Temp\parlGiqe +nzz.exe'Building Q:\W_PERL\_000_Dev\Netlister\Netlister.exe failed : The system cannot find the file specified.
Regex get Text between two strings with colon
3 direct replies — Read more / Contribute
by MurciaNew
on Sep 16, 2017 at 13:49
    Hi Monks,

    i have a regex problem. I want to get the text between two words with colon (like Example: or Epikrise:)

    I have files (newLine is "\r\n").

    Example of just two files: <<< start file 1>>> Test:\r\n blablabla1\r\n blablablabla2\r\n blablablablabla3\r\n blablablab4\r\n blablablaba5\r\n Test1:\r\n lalala1\r\n lalala2\r\n Hello3:\r\n mymymymy\r\n <<< end file 1>>> <<< start file 2>>> Test:\r\n blablabla1\r\n blablablabla2\r\n blablablablabla3\r\n blablablab4\r\n blablablaba5\r\n blablablaba6\r\n Test3:\r\n lalala1\r\n lalala2\r\n lalala3\r\n City:\r\n Gigi\r\n lulu\r\n Kuku\r\n <<< end file 2>>>

    With a regexp I want to get all text between "Test:" to that next line with a string with ":"(colon) (for the examples "Test2:" for file 1 or "Test3:" for file 2

    as result Test="blablabla1\r\n blablablabla2\r\n blablablablabla3\r\n blablablab4\r\n blablablaba5\r\n"

    or Test="blablabla1\r\n blablablabla2\r\n blablablablabla3\r\n blablablab4\r\n blablablaba5\r\n blablablaba6\r\n" )

    I tried (.+?(\r\n.+){1,}?(?!\w+?:)) but it does not work

    Please help me..... Thanks

    MurciaNew (Guido)

New Meditations
[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.



More Betterer Game of Life
1 direct reply — 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, 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 (see below)1 secs1 secs3 secs6 secs (Mario improvements)13 secs26 secs52 secs108 secs (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 (see below): 700,000K
    • (Original): 1,455,004K
    • (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 (see below)58 secs (Mario improvements)450 secs (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 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 is (or should be) 100% interface compatible with my original.

New Monk Discussion
How to get raw node content
2 direct replies — Read more / Contribute
by roboticus
on Sep 15, 2017 at 14:25

    Hello, gang:

    I was wondering if there was an API available that would let me fetch the raw content of a perlmonks node?

    Every once in a while, I see a node that I'm interested in dismantling, but it gets a bit butchered by the HTMLification/templating/(or something). The latest example is Reaped: Re: .pl to .exe, where I'm wanting to figure out just what the code is doing. But with the HTML entities, font rendering and whatnot, I can't really tell exactly what the code *was*. So rather than hand-editing it and trying to put it in a form that I can analyze, I'd love to be able to occasionally fetch just the raw node contents.

    I did a little googling and trying to browse the SiteDocClan nodes, but I'm not a member, so there's a limit to what I can see (e.g., I can't see sdc to-do wiki, SDC Wiki, the PMDev, editor, cabalist and pedagogue wikis...).

    I've found some public information, like What XML generators are currently available on PerlMonks?, WWW::PerlMonks, but I didn't see anything that provides the raw node content. I'm not asking for a new feature if it's not available, just a pointer to it if it exists.



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

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 examining the Monastery: (1)
As of 2017-09-22 02:53 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (256 votes). Check out past polls.