Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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
Understanding endianness of a number
2 direct replies — Read more / Contribute
by stevieb
on Jul 23, 2017 at 11:07

    Hey all,

    I ran into a situation recently where I was reading from an I2C device, and the byte ordering was in the opposite endianness that I was expecting. Although I fixed the problem by reversing the bytes before returning them, I went off to research exactly what endian was. I've spent numerous hours testing, reading and more testing, but I still can't grasp it fully. I'm hoping just one or two more examples will have it 'click'.

    So, I'll start off with a couple of examples here to see if I have the basics down. Please feel free to elaborate with other examples or comments etc.

    Set up our number, and two byte scalars (full version copy/pastable at the bottom of the post):

    use warnings; use strict; use feature 'say'; my $num = 1023; # 0x03ff my ($b1, $b2);

    Now, if I do the following bit shifting, the printf() is printing the bytes in big endian format, correct?

    $b1 = ($num & 0xff00) >> 8; $b2 = $num & 0xff; printf("%x, %x\n", $b1, $b2); # 3, ff

    Likewise, if I reverse the operations/bytes, this one will print in little endian format, right?

    $b1 = $num & 0xff; $b2 = ($num & 0xff00) >> 8; printf("%x, %x\n", $b1, $b2); # ff, 3

    Full code:

    use warnings; use strict; use feature 'say'; my $num = 1023; my ($b1, $b2); $b1 = ($num & 0xff00) >> 8; $b2 = $num & 0xff; printf("%x, %x\n", $b1, $b2); $b1 = $num & 0xff; $b2 = ($num & 0xff00) >> 8; printf("%x, %x\n", $b1, $b2);
sub variables
4 direct replies — Read more / Contribute
by Todd Chester
on Jul 22, 2017 at 19:33
    Dear Perl Monks,

    In Perl 5, is there a way to name my variables in the () of the sub declaration, as I do in Perl 6?
    sub DoSomething( $DataStr ) {;}

    Many thanks, -T
Space inserted into output file
3 direct replies — Read more / Contribute
by He77e
on Jul 22, 2017 at 11:36

    Greetings Esteemed Monks,

    I am relatively new to Perl so this may be an easy fix.

    My script here is meant to import a CSV, parse, output a new string containing the elements in FASTA format into an array and write to a file.

    The problem I come across is that before each new entry a blank space (\s) is inserted; I assume the problem is with the way I am exporting the array to a file but I cannot find a method which deals with the problem.

    Any help? (script shown below)

    use Text::CSV; use Data::Dumper qw(Dumper); print "Enter file name: \n"; my $file = <STDIN>; chomp $file; print "Enter output file name: \n"; my $ofile = <STDIN>; my $csv = Text::CSV->new({ sep_char => ',' }); my @fasta; open(my $data, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$data>) { chomp $line; if ($csv->parse($line)) { my @fields = $csv->fields(); #print Dumper \@fields; $fields[4]=~s/\s//gs; #removes spaces within the sequence push @fasta,"\>$fields[0]\_$fields[1]\_$fields[2]\_$fields[3]\n$ +fields[4]\n"; #outputs the correct format } else { warn "Line could not be parsed: $line\n"; } } #print Dumper \@fasta; open (FH,">$ofile"), print FH"@fasta", close; end;

    Sample input: (it is in TSV format but is read into the script anyway without a problem)

    XP_014917420.1 CYP26A1 Acinonyx jubatus Cheetah  MGFPFFGE +TLQMVLQRRKFLQMKRRKYGFIYKTHLFGRPTVRVMGADNVRRILLGEHRLV SVHWPASVRPILGSGC +LSNLHDSSHKQRKKVIMRAFSREALQYYVPVIAEEVGTCLEQWL SCGERGLLVYPQVKRLMFRIAMRI +LLGCEPRLANGGDAEQQLVEAFEEMTRNLFSLPIDV PFSGLYRGMKARNLIHARIEENIRAKICGLRA +AEAEAEAGGGCKDALQLLVDHSWERGER LDMQALKQSSTELLFGGHETTASAATSLITYLGLYPHVLQ +KVREELKSKGLLCKSNQDNK LDMEILGQLKYIGCVIKETLRLNPPVPGGFRVALKTFELNGYQIPKGW +NVIYSICDTHDV ADIFTNKEEFNPDRFMLPHPEDASRFSFIPFGGGAKILLKIFTVELARHCDWRLLN +GPPT MKTSPTVYPVDDLPARFTRFQGET XP_002916147.1 CYP26A1 Ailuropoda melanoleuca Giant Panda +MGLPALLASALCTFVLPLLLFLAAIKLWDLYCVSGRDRSCALPLPPGTMGFPFFGETLQM VLQRRKFL +QMKRRKYGFIYKTHLFGRPTVRVMGADNVRRILLGEHRLVSVHWPASVRTIL GSGCLSNLHDSSHKQR +KKVIMRAFSREALQCYVPVIAEEVGTCLEQWLSCGERGLLVYPQ VKRLMFRIAMRILLGCDPRLASGG +DAEQQLVEAFEEMTRNLFSLPIDVPFSGLYRGMKAR NLIHARIEENIRAKICGLRTAEAASGCKDALQ +LLIEHSWERGERLDMQALKQSSTELLFG GHETTASAATSLITYLGLYPHVLQKVREELKSKGLLCKSN +QDNKLDMEILEQLKYIGCVI KETLRLNPPVPGGFRVALKTFELNGYQIPKGWHVIYSICDTHDVADSF +TNKDEFNPDRFL QPHPEDASRFSFIPFGGGLRSCVGKEFAKMLLKIFTVELARHCDWRLLNGPPTMKT +SPTV YPVDGLPARFTHFQGEI XP_006276679.1 CYP26A1 Alligator mississippiensis American Al +ligator MGFALLASALCTLLLPLLLFLAAVKLWGLYCESGRDPGCPLPLPPGTMGLPFFGETLQ +MV LQRRKFLQVKRRKYGCIYKTHLFGRPTVRVLGADNVRRILLGEHRLVAVQWPASVRTILG SGCLS +NLHDARHKQRKKVIMRAFSRDALRHYAPVMQEEVSGCLARWLGRGGACLLVYPEV KRLMFRIAMRLLL +GFEPHQADSGSERQLVEAFEEMSRNLFSLPIDVPFSGLYRGLRARNI IHARIEANIRNRMARAEPGGG +PKDALQLLLEQAQRHGQPLNMQELKESATELLFGGHETT ASAATSLITFLGLHPEVLQKVRKELQGNG +LLCSPNQDSKTLDMEVLEQLKYTGCVIKETL RLSPPVPGGFRVALKTFELNGYQIPKGWNVIYSICDT +HDVAELFTNKDKFNPDRFMSPSP EDSSRFSFIPFGGGVRSCVGKEFAKILLKIFTVELARNCDWQLLN +GPPTMKTGPIVYPVD NLPAKFVGFSGQI XP_021123924.1 CYP26A1 Anas platyrhynchos Mallard  MGFSALL +ASALCTFLLPLLLFLAAVKLWDLYCVSSRDPSCPLPLPPGTMGLPFFGETLQM VLQRRKFLQMKRRKY +GFIYKTHLFGRPTVRVMGAENVRHILLGEHRLVSVQWPGSPPPPP LPRPPGQVIMRAFSRDALQHYVP +VIQEEVSACLARWLGAAGPCLLVYPEVKRLMFRIAMR ILLGFQPRQAGPDGEQQLVEAFEEMIRNLFS +LPIDVPFSGLYRGLRARNIIHAKIEENIR AKMARKEPEGGYKDALQLLMEHTQGNGEQLNMQELKESA +TELLFGGHETTASAATSLIAF LGLHHDVLQKVRKELQVKGLLCSPNQEKQLDMEVLEQLKYTGCVIKE +TLRLSPPVPGGFR IALKTLELNGYQIPKGWNVIYSICDTHDVADLFTNKDEFNPDRFMSPSPEDSSRF +SFIPF GGGLRSCVGKEFAKVLLKIFIVELARSCDWQLLNGPPTMKTGPIVYPVDNLPTKFIGFSG QI

    Sample output: (note the \s added to every entry excluding the first)

    >XP_002916147.1_CYP26A1_Ailuropoda melanoleuca_Giant Panda MGLPALLASALCTFVLPLLLFLAAIKLWDLYCVSGRDRSCALPLPPGTMGFPFFGETLQMVLQRRKFLQM +KRRKYGFIYKTHLFGRPTVRVMGADNVRRILLGEHRLVSVHWPASVRTILGSGCLSNLHDSSHKQRKKV +IMRAFSREALQCYVPVIAEEVGTCLEQWLSCGERGLLVYPQVKRLMFRIAMRILLGCDPRLASGGDAEQ +QLVEAFEEMTRNLFSLPIDVPFSGLYRGMKARNLIHARIEENIRAKICGLRTAEAASGCKDALQLLIEH +SWERGERLDMQALKQSSTELLFGGHETTASAATSLITYLGLYPHVLQKVREELKSKGLLCKSNQDNKLD +MEILEQLKYIGCVIKETLRLNPPVPGGFRVALKTFELNGYQIPKGWHVIYSICDTHDVADSFTNKDEFN +PDRFLQPHPEDASRFSFIPFGGGLRSCVGKEFAKMLLKIFTVELARHCDWRLLNGPPTMKTSPTVYPVD +GLPARFTHFQGEI >XP_006276679.1_CYP26A1_Alligator mississippiensis_American Alligator MGFALLASALCTLLLPLLLFLAAVKLWGLYCESGRDPGCPLPLPPGTMGLPFFGETLQMVLQRRKFLQVK +RRKYGCIYKTHLFGRPTVRVLGADNVRRILLGEHRLVAVQWPASVRTILGSGCLSNLHDARHKQRKKVI +MRAFSRDALRHYAPVMQEEVSGCLARWLGRGGACLLVYPEVKRLMFRIAMRLLLGFEPHQADSGSERQL +VEAFEEMSRNLFSLPIDVPFSGLYRGLRARNIIHARIEANIRNRMARAEPGGGPKDALQLLLEQAQRHG +QPLNMQELKESATELLFGGHETTASAATSLITFLGLHPEVLQKVRKELQGNGLLCSPNQDSKTLDMEVL +EQLKYTGCVIKETLRLSPPVPGGFRVALKTFELNGYQIPKGWNVIYSICDTHDVAELFTNKDKFNPDRF +MSPSPEDSSRFSFIPFGGGVRSCVGKEFAKILLKIFTVELARNCDWQLLNGPPTMKTGPIVYPVDNLPA +KFVGFSGQI >ARO89874.1_CYP26A1_Andrias davidianus_Chinese Giant Salamander MSLYTLFASALCTLVLPLLLFLAAVKLWELYCISTRDRSCRCPLPPGTMGLPFFGETLQMVLQRRKFLQM +KRRKYGCIYKTHLFGRPTVRVMGAENVKQILLGEHRLVSVHWPASVRTILGSGCLSNLHDSQHKNRKKV +IMQAFSREALQHYIPVIEEEVRGALAQWLGGGGASVLVYPEVKRLMFRIAMRILLGFEPHQTDREMEQQ +LVEAFEEMIRNLFSLPIDVPFSGLYRGLKARNVIHAKIEENIRAKMAKESDTQYKDALQLLIEHTQKNG +EQLNMQELKESATELLFGGHETTASAATSLMTFLALHSDVLHKVRKELQIKDLLCDNKPLNIEALEQLK +YTGCVIKETLRLSPPVPGGFRVALKTFELNGYQIPKGWNVIYSICDTHDVAEIFPNKEEFNPDRFMSSH +PEDNSRFNFIPFGGGLRSCVGKEFAKILLKIFTVELARTCDWQLLNGAPTMKTGPIVYPVDNLPTKFIG +FNGII >XP_012310130.1_CYP26A1_Aotus nancymaae_Nancy Ma's Night Monkey MGLPALLASALCTFVLPLLLFLAAIKLWDLYCVSGRDRSCALPLPPGTMGFPFFGETLQMVLQRRKFLQM +KRRKYGFIYKTHLFGRPTVRVMGADNVRRILLGEHRLVSVHWPASVRTILGSGCLSNLHDSSHKQRKKV +IMRAFSREALKCYVPVIIEEVGSSLEQWLSCGERGLLVYPEVKRLMFRIAMRILLGCEPQLAGDRDAEQ +QLVEAFEEMTRNLFSLPIDVPFSGLYRGVKARNLIHARIEQNIRAKICGLRASEASRGCKDALQLLIEH +SWERGERLDMQALKQSSTELLFGGHETTASAATSLITYLGLYPHVLQKVREELKSKGLLCKSNQDNKLD +MEILEQLKYIGCVIKETLRLNPPVPGGFRVALKTFELNGYQIPKGWNVIYSICDTHDVAEIFTNKEEFN +PDRFMLPHPEDASRFSFIPFGGGLRSCVGKEFAKILLKIFTVELARHCDWQLLNGPPTMKTSPTVYPVD +NLPARFTHFHGEI
    Any help would be greatly appreciated!
Passing hashes to sub
1 direct reply — Read more / Contribute
by PSP
on Jul 22, 2017 at 04:28
    my $loopCnt=0; my %prevsidstat=%{getSessStat()}; while (1) { debug("Sleeping for $ENV{SESSNAP_INTRVL}...\n"); sleep $ENV{SESSNAP_INTRVL}; my %cursidstat=%{getSessStat()}; if ((keys %cursidstat) != 0 ) { my $ComResult={sidStatComp(%prevsidstat,%cursidstat)}; my %prevsidstat=%cursidstat; } last; }

    Here I am getting hash reference when %{getSessStat()} is executed. Now I want to pass two hash references to sub to process them further, however just printing them in sub getting following error. Appreciate in advance for help. Odd number of elements in anonymous hash at line 65, <SQLPLUS> line 20.

    Sub code:
    #***************************************************** sub sidStatComp { #***************************************************** my (%prevsidstat,%cursidstat)=@_; print "Previous\n"; for my $instsid (keys %prevsidstat) { print "$instsid => $prevsidstat{$instsid}{SQLEXECINFO}\n"; } print "Current\n"; for my $instsid1 (keys %cursidstat) { print "$instsid1 => $cursidstat{$instsid1}{SQLEXECINFO}\n"; } return 1; }
Get TAP output when compiling Perl and running "make test"
1 direct reply — Read more / Contribute
by Rjevski
on Jul 21, 2017 at 15:53

    Hello,

    I am currently working on automating our Perl builds and I'd like to know how do I get the TAP output when I run make test so that the continuous integration system can pick up failures and display them nicely (manually going through a 5k line build log is not fun).

    I've already tried setting PERL_TEST_HARNESS_DUMP_TAP as well as HARNESS_OPTIONS but those have no effect when running through make for some reason.

    Steps to reproduce:

    curl https://cpan.metacpan.org/authors/id/S/SH/SHAY/perl-5.20.3.tar.bz +2 | tar -jxC /root cd /root/perl* ./Configure -des -Dprefix=/opt/perl-5.20.3 make -j $(nproc) make test

    I am running Perl 5.20.3 on Debian 9.0 amd64.

    Regards.

Tk::TableMatrix won't build on Strawberry 5.26.0
3 direct replies — Read more / Contribute
by aplonis
on Jul 21, 2017 at 09:17

    Running Strawberry version 5.26.0 on Win 10 Pro. And, alas, Tk::TableMatrix will not build. I get this error...

    Checking dependencies from MYMETA.json ... Checking if you have ExtUtils::MakeMaker 0 ... Yes (7.24) Checking if you have Tk 800.022 ... Yes (804.033) Building and testing Tk-TableMatrix-1.23 cp TableMatrix.pm blib\lib\Tk\TableMatrix.pm AutoSplitting blib\lib\Tk\TableMatrix.pm (blib\lib\auto\Tk\TableMatrix +) cp TableMatrix/Spreadsheet.pm blib\lib\Tk\TableMatrix\Spreadsheet.pm cp TableMatrix/SpreadsheetHideRows.pm blib\lib\Tk\TableMatrix\Spreadsh +eetHideRows.pm cd pTk && gmake gmake[1]: Entering directory 'C:/Users/gan/.cpanm/work/1500635987.1034 +8/Tk-TableMatrix-1.23/pTk' gmake[1]: *** No rule to make target '..\blib\arch\Tk\pTk\.exists', ne +eded by 'config'. Stop. gmake[1]: Leaving directory 'C:/Users/gan/.cpanm/work/1500635987.10348 +/Tk-TableMatrix-1.23/pTk' gmake: *** [Makefile:1171: pTk/libpTk.a] Error 2 -> FAIL Installing Tk::TableMatrix failed. See C:\Users\gan\.cpanm\wor +k\1500635987.10348\build.log for details. Retry with --force to force + install it.

    And --force also failed. Anyone know how to work through this?

blank pdf generated using PDF::API2
1 direct reply — Read more / Contribute
by lennelei
on Jul 21, 2017 at 02:46

    Hi all,

    I've a very small Perl script that splits PDF files bigger than 100 pages :

    use strict; use warnings; use PDF::API2; #...some code here... #for testing purpose: my $file='path_to_some.pdf'; # my $oldpdf = PDF::API2->open($file); if ($oldpdf->pages > 100) { my $newpdf = PDF::API2->new; printf " (%d pages)\n", $oldpdf->pages; for my $page_nb (1..10) { $newpdf->importpage($oldpdf, $page_nb); } $newpdf->saveas("_$file"); }

    I'm running this on Windows (Windows 7 for my desktop, Windows 2008/2012 for the servers) with a Strawberry Perl 5.14 and PDF::API2 module installed using cpan.bat

    It's working and used for weeks now without trouble until this week. With a pdf received a few days ago, the script output is a 100 blank pages document.

    I tried using the alternative with importPageIntoForm by snoopy from http://www.perlmonks.org/?node_id=615492 with the same result.

    I also tried another tool (sejda) and the pages are correctly extracted so it's probably an issue with PDF::API2 or a misconfiguration but I don't know what to add/change in the script.

    FYI, the sejda command line:

    sejda-console.bat extractpages -f SOURCE.PDF -o TARGET.PDF -s 1-100

    Any idea/alternative I could try? I'd like to keep the Perl as this is only a small part of a bigger script, but if I have no other option, I'll use sejda for the split.

    Unfortunately, I cannot provide the PDF :(

    Thank you

    Edit : I just found and tried with CAM::PDF using the following code and it's working!

    For what I've seen, the difference between both code is that PDF::API2->import_page function tries to copy the content of the pages where CAM::PDF->extractPages function removes the pages outside the given range. Maybe there is a similar method in PDF::API2 but I couldn't find it yet?

    use strict; use warnings; use CAM::PDF; #...some code here... #for testing purpose: my $file='path_to_some.pdf'; # my $oldpdf = CAM::PDF->new($file) or die "$CAM::PDF::errstr\n"; if ($oldpdf->numPages() > 100) { printf " (%d pages)\n", $oldpdf->numPages(); $oldpdf->extractPages(1..100); $oldpdf->cleanoutput("_$file"); }
where the module file *.pm located
5 direct replies — Read more / Contribute
by ytjPerl
on Jul 20, 2017 at 11:57
    Hi folks, I am facing an issue that I have no internet access on my VM, so when I running my perl script containing external modules, I have to manually copy *.pm from local machine to VM. For instance, I need to use File::Readbackwards. I have that locally at C:\Perl64\site\lib\File\Readbackwards.pm, but in my VM, I only have directory perl\site\lib(no sub-directory File), so I manually copied there, \Perl\lib\ as well. When I execute my script, it is still saying cannot locate File::Readbackwards. Please help! Thanks
Use 'use' in foreach
5 direct replies — Read more / Contribute
by zidi
on Jul 19, 2017 at 06:41
    Hey, I am currently storing some non core perl modules in an array like this:
    my @NotInstalledModules = ( 'Foo::Bar', 'Bar::Foo', )
    And I am able to install these modules (install, if eval("use $_") not possible...). The only problem is that I can't 'use' them afterwards. I tried:
    foreach (@Dependencies){ use $_; }
    which is not working. Any help is appreciated Best regards
Running script only within a specific domain/network
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 19, 2017 at 06:38

    Dear monks

    I want to allow a script to run only if it is physically inside a specific domain. Let's say that the script should work only if it is on a computer which is inside the network of the University of Wien (www.univie.ac.at). In order to do so, the script needs to read the network/domain in which the computer is (As you can see from the terms I am using, I am really a non-expert in all these IP & co. matters, but I am fairly okay in Perl programming ;). At the moment I can read the IP with the following:

    use warnings; use Net::Address::IP::Local; print my $address = Net::Address::IP::Local->public;

    Is the ip adress I am getting here stable enough to allow me to check if the computer is inside the university network over a longer period of time? If not, what is the best way to check it? Thanks

search a string in a non-regular file
6 direct replies — Read more / Contribute
by ytjPerl
on Jul 18, 2017 at 15:41

    Hi guys, I am new babie in perl. I happened to come across an issue of finding a string in 'non-regular' log file. I know how to use like if(/*/) {code} to find a string "*" in .txt file, but I could not use this to find a string in the file generated by command 'tmboot -y'. The string actually is there and I am able to use notepad++ search function to find it. so the only reason I could think of is that the file is not the regular txt file. any input? thanks!

HMAC Sha256 Help!
3 direct replies — Read more / Contribute
by ribble
on Jul 18, 2017 at 05:13

    Hello all. I am after some assistance regarding the creation of a basic authentication header.

    I am been asked to code against a restAPI that requires the creation of the authentication header using hmac sha256. I am running into issues getting the hash values to match against the restAPI yet when I check using an online hmac generator my values are correct

    This leads me to believe I am doing something wrong when using the app key as part of the hash

    The documentation states that the key is comprised of 32 two-character base-16 hexadecimal values. I guess this where my issue is

    I don't know how I should be handling the key other than just parsing it as a string.

    my simple code so far

    #set the strings $method="GET"; $appid="d6468df6c1e8419fb5ec50f62be9a28b"; $appkey="15c7a6e1a5bd73500db29dffd0e19b6c6228b044c64348a6f5d6d470c54fc +208"; $path="/xxxxx/xxxx/api/v1/users/xxxx/xxxxx"; #build the string to hash $step1="$method\\n $datestring\\n $appid\\n $path"; #hmac sha256hash the string $step2=hmac_sha256_hex($step1, $appkey);

    I am using hmac_sha256_hex as the app key string is in hex. I am clearly doing something wrong but no idea what?

    Do i need to perform some conversion of the app key before using it? I am lost!

    thanks for looking

New Meditations
Test Driven Development, for software and for pancakes
2 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 https://cavac.at/public/mercurial/JavaScript-V8/. It is in a mercurial repo, so you should be able to do this:

    hg clone https://cavac.at/public/mercurial/JavaScript-V8/ 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 FileSlurp.pm (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.

    beest.pl

    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 "beest.pl"):

    Evolver.pm

    Most of what Evolver.pm 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 Evolver.pm:

    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

New Cool Uses for Perl
Reading/writing Arduino pins over I2C with Perl
No replies — Read more | Post response
by stevieb
on Jul 23, 2017 at 14:25

    In today's episode of Cool Uses for Perl, loosely inspired by this thread, I'm going to show how to set up an Arduino (Uno in this test case) with a pseudo-register that allows toggling one if its digital pins on and off, and another pseudo-register to read an analog pin that the digital pin is connected to, over I2C. Because it's digital to analog, the only possible values of the analog read will be 0 (off) or 1023 (full on, ie. 5v). This is an exceptionally basic example, but with some thought, one can imagine the possibilities (read/write EEPROM, set PWM etc etc).

    We'll then use RPi::I2C to toggle the digital pin and read the analog pin over the I2C bus. Note I'm not using the encompassing RPi::WiringPi distribution in this case. The benefit to using that is to clean up Raspberry Pi's GPIO pins, which we aren't using any. In fact, any Linux device with I2C can be used for this example, I just so happen to be using one of my Pi 3 boards.

    First, the simple Arduino sketch. All I2C devices require putting themselves on the "wire" with a unique address. I'm using 0x04, which is how the Pi will identify the Arduino on the I2C bus.

    #include <Wire.h> // Arduino I2C address #define SLAVE_ADDR 0x04 // pseudo register addresses #define READ_A0 0x05 #define WRITE_D2 0x0A uint8_t reg = 0; void read_analog_pin (){ switch (reg){ case READ_A0: { __read_analog(A0); break; } } } void __read_analog (int pin){ int val = analogRead(pin); uint8_t buf[2]; // reverse endian so we're little endian going out buf[0] = (val >> 8) & 0xFF; buf[1] = val & 0xFF; Wire.write(buf, 2); } void write_digital_pin (int num_bytes){ reg = Wire.read(); // global register addr while(Wire.available()){ uint8_t state = Wire.read(); switch (reg){ case WRITE_D2: { digitalWrite(2, state); break; } } } } void setup(){ Wire.begin(SLAVE_ADDR); // set up the I2C callbacks Wire.onReceive(write_digital_pin); Wire.onRequest(read_analog_pin); // set up the pins pinMode(2, OUTPUT); pinMode(A0, INPUT); } void loop(){ delay(10000); }

    Now, I'll show a simple script that loops 10 times, toggling the digital pin then displaying the value from the analog pin. Arudino's Wire library sends data a byte at a time, so we have to do some bit manipulation to turn the two bytes returned in the read_block() call back together into a single 16-bit integer. I wrote the merge() sub to take care of this job.

    use warnings; use strict; use RPi::Const qw(:all); use RPi::I2C; use constant { ARDUINO_ADDR => 0x04, READ_REGISTER => 0x05, WRITE_REGISTER => 0x0A, }; my $device = RPi::I2C->new(ARDUINO_ADDR); for (0..9){ my (@bytes_read, $value); $device->write_byte(HIGH, WRITE_REGISTER); @bytes_read = $device->read_block(2, READ_REGISTER); $value = merge(@bytes_read); print "$value\n"; # 1023 $device->write_byte(LOW, WRITE_REGISTER); @bytes_read = $device->read_block(2, READ_REGISTER); $value = merge(@bytes_read); print "$value\n"; # 0 } sub merge { return ($_[0] << 8) & 0xFF00 | ($_[1] & 0xFF); }

    Output:

    1023 0 1023 0 1023 0 1023 0 1023 0 1023 0 1023 0 1023 0 1023 0 1023 0

    update: I must acknowledge Slava Volkov (SVOLKOV) for the actual XS code. Most of the low-level hardware code I've been working on over the last year has been wrapping C/C++ libraries, a decent chunk of it has had me following datasheets to write my own, but in this case, I bit the whole XS file from Device::I2C and just presented a new Perl face to it so it fit in under the RPi::WiringPi umbrella. It just worked.

Download free Microsoft ebooks, fun with Mojolicious and CSS selectors
No replies — Read more | Post response
by marto
on Jul 21, 2017 at 11:35

    I was made aware that Microsoft giving away free ebooks. Excuse the clickbaity page title, I have nothing to do with it. While people have posted wget scripts to download them all, it doesn't rename them so you end up with some random file names. I threw the script below together really quickly, consider it a cheap hacky but functional (no errors here) script. For each 'Category' it creates a directory, and uses Mojolicious/Mojo::UserAgent to get the page, parse what we need from it, download each file to the it's associated category directory, with the actual ebook name.

    Caveats:

    • Ensure you have an up to date Mojolicious installed (cpanm Mojolicious).
    • Copy the script below into it's own directory before running.
    • Not all ebooks are available in all formats. I just select the top one in the list. Most are PDF, some are ebup or .doc
    #!/usr/bin/perl use strict; use warnings; no warnings 'utf8'; use Mojo::UserAgent; my $ebookURL = 'https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free-m +icrosoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-ebo +oks-again-including-windows-10-office-365-office-2016-power-bi-azure- +windows-8-1-office-2013-sharepo/'; =head1 NAME ms-ebook-dl - Download free Microsoft ebooks =head1 DESCRIPTION A quick hack using L<Mojolicious> to download and properly name a bunc +h of free ebooks from Microsoft. =head1 INSTALLATION Ensure you have an up to date L<Mojolicious> installed: C<cpanm Mojolicious> Clone the repo: C<git clone https://github.com/MartinMcGrath/ms-ebook-dl> =head1 LICENSE This is released under the Artistic License. See L<perlartistic>. =head1 AUTHOR marto L<https://github.com/MartinMcGrath/> =head1 SEE ALSO L<http://perlmonks.org/?node_id=1195726> L<https://blogs.msdn.microsoft.com/mssmallbiz/2017/07/11/largest-free- +microsoft-ebook-giveaway-im-giving-away-millions-of-free-microsoft-eb +ooks-again-including-windows-10-office-365-office-2016-power-bi-azure +-windows-8-1-office-2013-sharepo/> =cut my $ua = Mojo::UserAgent->new; print "Get page\n"; my $res = $ua->get( $ebookURL )->res; # css selector we want the first table witin the entry-content div, sk +ipping # the first row which is a header, but not a 'th' tag. my $selector = 'div.entry-content table:first-of-type tr:not(:first-of +-type)'; warn "Parse page\n"; $res->dom->find( $selector )->each( sub{ my $category = $_->children->[0]->all_text; my $title = $_->children->[1]->all_text; my $url = $_->children->[2]->at('a')->attr('href'); my $type = $_->children->[2]->at('a')->all_text; # download each file print "downloading: $title\n"; # create category directory unless it already exists mkdir $category unless( -d $category ); $ua->max_redirects(5) ->get( $url ) ->result->content->asset->move_to($category . '/' . $title . '.' + . $type); # play nice # play nice sleep(7); });

    Update: code updated with some POD, also on on github.

New Monk Discussion
Feature: Auto hide. Change: show node vote counts to all, not just logged in users.
9 direct replies — Read more / Contribute
by marto
on Jul 18, 2017 at 05:22

    A couple of suggested changes. I propose that if a node not under considereation hits the same level of downvotes required to trigger auto reaping, the node content should be automatically hidden, a message displayed warning readers that it's quality/relevance is in question, a link would allow reading the original. Something along the lines of:

    This node was taken out by the NodeReaper on Jul 16, 2017 at 09:42 BST Reason: [NodeReaper] - Post contents hidden due to sufficiently negati +ve reputation. You may view the original node and the consideration vote tally.

    All other cases I can think of (trolling, abuse, duplicate post etc) are already catered for by the consideration system as is.

    Secondly, the vote count be displayed to all, not just logged in users.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2017-07-24 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (347 votes). Check out past polls.