Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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
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

references - proper use of infix (arrow) and deprecated hash references
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 18, 2017 at 04:23
    Hello Monks,

    I just learned how to use References, but I am still confused. Currently, I try to write a script that reads a txt file that has 5 columns, each separated by some whitespace. Each Row corresponds to a gene and the columns are the 1. Gene identifyer 2. sequence 3. sequence length 4. GC content 5. CAI. It's not important what that is.

    I want to store the individual attributes into a hash with the identifyer as keys, and a reference to an array containing the remaining attributes as the values. When I try to print the hash, perl complains about my infix operator, except when I write the % symbol instead of the $ symbol. But I learned it the other way. In addition, Perl complains that hash references are deprecated. I'm confused.

    Here's my code:
    my $file = 'data.txt'; open READ, $file || die "Cannot open $file: $!\n"; my %hash; while (<READ>){ chomp; my ($id, $seq, $length, $gc, $cai) = split /\s+/, $_; $hash{$id} = [ $seq, $length, $gc, $cai ]; } close (READ); foreach (keys %hash){ print $_, "\t", %hash->{$_}[1], "\t", %hash->{$_}[2], "\n"; }

    this code only leads to the warning: Using a hash as a reference is deprecated! When I change the last line to  print $_, "\t", $hash->{$_}[1], ... it also complains: Global symbol $hash requires explicit ...

    Can anyone clarify the situation? Thanks in advance!
How can I use printf FORMAT strings for (Win32) shell globs?
7 direct replies — Read more / Contribute
by ozboomer
on Jul 16, 2017 at 21:10

    Hi, again, folks...

    I'm trying to work-out a simple/reliable regex to deal with printf 'format' strings with shell globs.

    I've experimented a lot and had a look on-line... I've gone though the O'Reilly books (Perl & Regular Expression books, cookbooks, etc) and although they mention glob2pat sub (Perl Cookbook, Sect 6.9), that converts the wrong way (but see Bug in glob2pat?)... and I can't work out an 'elegant' regex to do the job I need to do.

    Although... the following works Ok... but it appears pretty long-winded and ugly to me... and I'm guessing there must be a cleaner way to do it...

    use Data::Dumper; ($match) = (@ARGV); # Get the 'printf format' s +tring to match printf(" \$match: >%s<\n\n", $match); # Input as `Img%04d.png` (b +ackprime quoted) $match =~ /([\w]+)(%[0-9]+d)\.([\w]+)/i; # EX: Img%04d.png $prefix = $1; $format = $2; $ext = $3; printf(" \$prefix: >%s<\n", $prefix); printf(" \$format: >%s<\n", $format); printf(" \$ext: >%s<\n\n", $ext); $format =~ /%([0-9]+)d/; # Note the '%04d' portion $count = $1; $glob_str = sprintf("%s%s\.%s", # ...and build the DOS/glob + match strings $prefix, "\*", $ext); $grep_str = sprintf("%s[0-9]{%d}\.%s", $prefix, $count, $ext); printf("\$glob_str: >%s<\n", $glob_str); printf("\$grep_str: >%s<\n\n", $grep_str); @tmpfiles = glob($glob_str); # Get all the glob-matched +files printf("\@tmpfiles:\n"); # ...EX: Img*.png print Dumper(@tmpfiles); printf("\n"); @files = grep(/$grep_str/i, @tmpfiles); # Filter to match the 'prin +tf format' printf("\@files:\n"); print Dumper(@files); printf("\n");

    In this case, I'm looking at running ActiveState Perl 5.16.3 under Win32, although I'll probably also need it to work under a similar Perl version on Linux as well.

    I'd greatly appreciate any guidance, please.

    Thanks a heap.

Date Timezone
4 direct replies — Read more / Contribute
by kepler
on Jul 15, 2017 at 16:53
    Hi

    I'm having some troubles making a sub function that given an year, month, day and hour and the zone would return the offset in seconds. The general code is this (and works if used in the main routine):

    my $tz = DateTime::TimeZone->new( name => $zone ); my $dt = DateTime->new( year => $year_init, month => $month_init, day => $day_init, hour => $hour_init, minute => $minutes_init, second => $seconds_init, nanosecond => 0, ); $offset = $tz->offset_for_datetime($dt); #seconds $offset_days = $offset / 86400; #days

    BUT if I place it in a function, let's say sub TZone (), where I receive all the parameters, the program does not give an error but exits (I say this because no more output is done...). If I make a return before the above, it returns what I want (so the function is well formated, except when it calls the offset). Can someone help me out?

    Kind regards, Kepler

New Meditations
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
Free Microsoft ebook downloader.
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 cooling their heels in the Monastery: (3)
As of 2017-07-22 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (340 votes). Check out past polls.