Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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
where the module file *.pm located
2 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

How can I print variable contents from the debugger non-interactively?
6 direct replies — Read more / Contribute
by davehorner
on Jul 14, 2017 at 20:16
    I am trying to debug a perl script without modifying it.

    I'm using perl -d -w -Mdiagnostics=-verbose file.pl --args-to-file.pl 2>&1.

    This gives me great visibility into the lines of code that are being run, I can identify which lines are causing the issue and need to be investigated further but I'm not sure how to tell the debugger to print the contents of the variables. I don't care if I have to tell the debugger to print all variables (which would produce a lot of output). But what I would really prefer is to tell the debugger to print all the variable contents when executing line XXX or print variables named via regex.

    I learned how to use the diagnostics pragma -d -w -M here: http://perldoc.perl.org/diagnostics.html

    I have read the http://perldoc.perl.org/perldebug.html and see there is a $ENV{PERLDB_OPTS} that seems like it might be modified to provide such capabilities but I'm still not sure what debugger options I should be using.

    Any help would be greatly appreciated!

    Update: some feel this description is a bit vague so here's additional justification for not modifying the script and not running from an interactive debugger on the cmdline:
    I'm interested in trying to use the debugger as a diagnostic tool when the script is run non-interactively from another script/system, you can change environment variables and cmdline args but you can't easily change the script or execute the script from an interactive debugger session. Lets imagine that there's something about this machine that it is running on that is causing the issue, modifying the script and getting it on the machine require a lot of effort. Maybe it's in an air gap network and getting changes requires new physical media to be shipped to a customer and physically mounted. Meaning it could take significant time to just get a debug script (i.e. additional print lines) on the specific machine, which then requires you to get the output delivered physically out of the air gap network for analysis externally and then you'd need to send another updated script with the actual fix and prune the debug statements. This back and forth takes a lot of time and could require multiple iterations as you are placing debug statements in places you suspect where the issue to reside, it might take 3 or 4 debug scripts to nail down the actual bug.

    Changing the cmdline args and using perl -d -w -Mdiagnostics=-verbose file.pl --args-to-file.pl 2>&1 gets all the output required to understand what is actually being executed but it doesn't provide enough information about the variable contents to understand what it is about the environment/data that is causing the script to exhibit strange behavior.

    --dave
Monkey patching all subs with a basic 'starting subname' message
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 14, 2017 at 12:09

    While working on an existing Perl package file, I would like to log the start/end of each method call (preferably without adding any new dependencies to the module). I've already added a rudimentary __dbg sub, which I'm fond of, like so...

    our $DEBUG_LEVEL = 5; sub __dbg (&;$) { my ($code, $verbosity) = @_; if ($DEBUG_LEVEL >= ($verbosity // 1)) { my $msg = join('', $code->()); print STDERR "DEBUG: ", $msg, "\n" if length($msg); } } # ...the meat of the package is below...

    I think I have been able to monkey-patch all the methods in the package, by adding the following to the bottom of the package file:

    # ...the meat of the package is above... __dbg { no strict 'refs'; no warnings 'redefine'; my $package = __PACKAGE__; my %patched = ('__dbg' => 1); foreach (grep { defined &{"${package}::$_"} } keys %{"${package}:: +"}) { unless (exists $patched{$_}) { $patched{$_} = 1; __dbg { "Monkeypatching '${package}::$_' for tracing..." } + 5; my $fname = $_; my $fn = \&{"${package}::$_"}; *$_ = sub { __dbg { "Starting ${package}::${fname}(", join(',', @_ +), ')' } 4; my @ret = $fn->(@_); __dbg { "Ending ${package}::${fname} with return value +s: ", join(',', @ret) } 4; return unless defined(wantarray); return @ret if wantarray; return @ret[0] if scalar(@ret); } } } }; 1;

    Now, whether this is a good thing to do or not (I promise to remove it at the end of the dev cycle), it seems to be working, but now I'm worried that my 'light-touch' profiling will actually have unintended side-effects on the subs it touches. For example:

    1. It warns on subs that have prototypes (e.g. Prototype mismatch: sub MyModule::method1 (&;$) vs none at /MyModule.pm line Y.), where Y is the closing brace to the unless (exists... block. I can suppress with no warnings 'prototype' but does that mean that the interpreter may no longer apply the syntactic distinctions that prototypes intend to provide?
    2. Even though I'm trying to reflect the requirements of the caller using wantarray, am I altering the results of wantarray in the actual wrapped call? Better to check wantarray first?
    3. I think it's also attaching to subs that are imported into the module (i.e. I don't think try, with or time are actually part of the package), but I can't see the 'best' way to exclude them
    4. (Also, it doesn't actually output the arguments, but I can't see the bug -- should I copy @_ to a local variable first (i.e. my @args = @_)?)

    Any other wisdom to be shared here (bar the obvious "don't" and "use a cpan module")?

New Meditations
Perl, JavaScript and Strandbeests
1 direct reply — 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 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?
What's the matter? Cat got your tongue?...

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
























    Results (318 votes). Check out past polls.