Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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.

Quests
poll ideas quest 2020
Starts at: Jan 01, 2020 at 00:00
Ends at: Dec 31, 2020 at 23:59
Current Status: Active
5 replies by pollsters
    First, read How do I create a Poll?. Then suggest your poll here. Complete ideas are more likely to be used.

    Note that links may be used in choices but not in the title.

Supplications
Building ARRAY with grep expression NOT working
3 direct replies — Read more / Contribute
by denting24by7
on Mar 31, 2020 at 16:02

    I am doing something wrong trying to build an array that should contain all jscript extension file names. I am very new to perl. I have this working in bash. I know that the first array has the lines containing the jscript file names. Just can't figure out how to remove just them and put them in an array to sort unique items.

    #!/usr/bin/perl use strict; use warnings; sub main { my @js = (); my @ujs =(); my $file = 'access_log.txt'; open(FH, $file) or die("File $file not found"); while(my $String = <FH>) { if($String =~ '[^/]*\.js') { push(@js,($String)); #print "$String \n"; @ujs = grep {/[^/]*\.js/}, @js; # print @ujs; } } close(FH); foreach (@ujs) { print "$_\n"; } } main();
declaring lexical variables in shortest scope: performance?
7 direct replies — Read more / Contribute
by bliako
on Mar 31, 2020 at 06:22

    Hello lockdown ones,

    Everytime I declare a variable I believe a flop dies in my CPU. Probably because I fell in the C cauldron in my formative years. But now, and in Perl, the paradigm is to "lexicalise" variables within the innermost scope. For example, for:

    my $x; for $x (1,2,3){ print "x=$x\n" }

    perlcritic a.pl says Loop iterator is not lexical at line 6, column 1.  See page 108 of PBP.  (Severity: 5)

    But this pacifies perlcritic:

    for my $x (1,2,3){ print "x=$x\n" }

    But wakes in me primordial fears of will that variable be created 3 times and decrease performance? (for the sake of readability and, perhaps, stability and not introducing subtle bugs). Does anyone know the difference in performance between the two scripts? Even if it is tiny!

    bw, bliako

How install multiple version of perl
4 direct replies — Read more / Contribute
by maria80e
on Mar 31, 2020 at 03:04
    I have the following question in perl.

    1) Is it possible to install multiple version of perl(example perl version: 5.6 and perl version 5.24) in windows system.

    2) How to call the perl file using version. For example, call the new version perl file(5.24) from old version of perl file(5.6).

Grab username from WP Cookie
2 direct replies — Read more / Contribute
by htmanning
on Mar 31, 2020 at 00:45
    Monks,

    I don't know if this is possible, but I need to grab a Wordpress user's username from the cookie once they are logged in. I will pass that to another Perl based part of the site. There are 3 cookies with the username followed by some hash. I'm not having much luck finding a way to read it when I won't know the cookie name ahead of time. Any ideas?

bracket processing
5 direct replies — Read more / Contribute
by rajaman
on Mar 30, 2020 at 21:06

    Hi Monks,

    I am trying to split a string delimited at the longest balanced-bracket, i.e., in a greedy way.

    For example, I want to split the string shown below into two parts: i) 'The use of parentheses.', and ii) '(indicates that the (writer [considered] the {information}) less <important-almost> an afterthought)'.

    While part ii) could be obtained using the code below, I am not sure how to get the remaining part of the string, i.e., part i). I also tried to work with 'Text::Balanced' module but couldn't make it work.

    Thank you for suggestions.

    use Regexp::Common 'RE_ALL'; my $string='The use of parentheses (indicates that the (writer [consid +ered] the {information}) less <important—almost> an afterthought).'; $string =~ RE_balanced(-parens=>'(){}[]<>'} and print "$1\n";
for loop less than test ill behaved...
4 direct replies — Read more / Contribute
by JCyow2020
on Mar 30, 2020 at 11:23

    Dear Monks, I have a simple for loop which use the < less than operator in the TEST condition. This works as expected for an assigned scalar, but when I assign the scalar thru a rand call the loop goes 1 iteration beyond the < less than condition to <=. Your wisdom is greatly appreciated. J

    #!/usr/bin/perl # use v5.10; use warnings; #use strict; $data_length = 5; printf "data length = %d\n",$data_length; for($k=0; $k < $data_length; $k++) # the includes up to $data_l +ength - 1 { printf "loop k = %d\n",$k; }# end for() $my_rand5 = rand(5); $data_length = 5 + $my_rand5; printf "data length = %d\n",$data_length; for($k=0; $k < $data_length; $k++) # the test includes $k == $d +ata_length { printf "loop k = %d\n",$k; }# end for()
    Results are as follows: mytest data length = 5 loop k = 0 loop k = 1 loop k = 2 loop k = 3 loop k = 4 <- loop stops at N-1 data length = 9 loop k = 0 loop k = 1 loop k = 2 loop k = 3 loop k = 4 loop k = 5 loop k = 6 loop k = 7 loop k = 8 loop k = 9 <- loop goes 1 iteration too far
i2c bus contention
3 direct replies — Read more / Contribute
by anita2R
on Mar 29, 2020 at 19:04

    Hello,

    I am looking for advice on an approach that I can use to solve this problem:

    I have a Perl script that runs an lcd display over i2c. This runs continuously, reading and displaying new data from a text file. This runs fine and is stable over many months.

    I have a second perl script that reads volts & amps from a device (INA219) that is also connected over i2c. This runs fine when run on a different machine (both Raspberry Pi's).

    But ... when both scripts are run on the same machine, the lcd screen gets garbled and some of the amps/volts data is wrong. It seems that the underlying i2c driver does not handle contention. Both scripts use HiPi::Device::I2C.

    I have overcome the problem by using time slots.

    The amps/volts device can read/write at 0, 5, 10, 15 seconds etc. after each minute and the lcd script can write at (1,2,3) (6,7,8) (11,12,13) seconds etc. after each minute. The data from the amps/volts device is fine and the lcd display is stable using this scheme.

    But this is an ugly fix, so I am looking for alternatives.

    I looked at using a common module with the aim of passing read/write requests from each script to the module, which would then handle them sequentially, so no contention.

    Using two small test scripts and a module I wrote (i2cAccess.pm) I found, perhaps not unexpectedly, that each script loads or uses a separate copy of the module. so there is no common access to the i2c bus. I can post the two scripts and the module if it would be of any use.

    I considered having a lock file that each script accesses, to control access to the i2c bus, but that seems like a lot of overhead, probably worse that my time slot solution.

    I also setup a test using IPC::Shareable

    The aim being to pass the i2c objects, register information etc. to the 'server' program. This failed because, as far as I can tell, Shareable does not support objects. I found a reference to this at https://www.nntp.perl.org/group/perl.poe/2009/12/msg4706.html which says 'Storable can't serialize filehandles'. When adding an i2c object to a shared hash I get 'Can't store GLOB items at ... /IPC/Shareable.pm. Again I can post the test scripts I used if that would be helpful.

    Can the monks suggest an approach to handling this issue.

    Thank you for your consideration.

Perl command line switches
4 direct replies — Read more / Contribute
by Irakro1997
on Mar 26, 2020 at 22:27
    Hi I recently started using Perl scripting again after a very long time so I inherited a scile file that I am playing with to get to work while trying to understand it I just recalled a useful command but I can’t remember the name nor the exact usage so thought I would ask ...if you have inherited a Perl script that you quickly want to understand which arguments to pass on command line to generate out put files there is a command that you can use on the Perl script to get help on how to run that file or which arguments to pass to the script while running .... if that makes sense would someone here know these until commands ? Thanks
Oracle::DBD to 12c server
3 direct replies — Read more / Contribute
by jerryhone
on Mar 26, 2020 at 11:46
    We have an old application running on Solaris using Oracle:DBD that happily connects to an Oracle 11g server! $DBD::Oracle::VERSION = '1.14'; When trying to connect to a new 12c server we're encountering ORA-28040: No matching authentication protocol (DBD ERROR: OCISessionBegin) Google implies that we need to change sqlnet.allowed_logon_version on the server, but our DBA tells us that's not possible on our new dataserver setup! So, question is...will a newer version of Oracle::DBD solve our issues, and if so, what version do we need? If not, any other ideas?
Multidimensional arrays
3 direct replies — Read more / Contribute
by baxy77bax
on Mar 26, 2020 at 11:13
    Hi

    Let say I have a multidimensional array and every time the number of dimensions is different. I don't have apriori information about the number of dimensions thus i need to recursively access each and figure out if there is more depth or not. But I also need to copy this array into another and modify it (let say increment). Curently I am trying to do it like this:

    sub new { my ($class) = @_; my $self->{_mtx_} = []; bless $self, $class; return $self; } sub copycomp_matrix { my ($self, $arg) = @_; $self->_recurse($arg,$self->{_mtx_}); print Dumper($self->{_mtx_}); } sub _recurse { my ($self, $in, $out) = @_; for (my $i = 0;$i<@{$in};$i++){ if (ref $in->[$i] eq 'ARRAY'){ $self->_recurse($in->[$i], $out->[$i]); }else{ if (!$out->[$i]){ $out->[$i] = $in->[$i] ; }else{ $out->[$i] = $in->[$i] +1 ; } } } } ######################################################### use strict; use lib "./"; use Above::Obj; my $z = Above::Obj->new(); my $a = [[1,2],[2,3]]; $z->copycomp_matrix($a);
    but I keep getting  $VAR1 = []; What am i missing ?? Is there a better way to do this ? thnx
Query of multi dimentional array
4 direct replies — Read more / Contribute
by shabird
on Mar 26, 2020 at 05:51

    Hello Monks, hope you all are fine

    I am struggling with multi dimensional array this time so basically the question goes like this, I have a file which has a data like this

    GeneID Tp1 Tp2 Tp3 ALA1 10 12 11 THR8 57 99 12 HUA4 100 177 199 ABA5 2 5 10

    So i have read the file and assigned it to multi dimensional array with the help of this code.

    my @content = (<FH>); close(FH); my $no_of_seq = scalar(@content); my @myArray; foreach my $row (@content) { my @columns = split(/\s+/,$row); push(@myArray,\@columns); # print "@columns"; } for($row = 0; $row < $no_of_seq; $row++){ for($col = 0; $col < $no_of_seq; $col++){ print($myArray[$row][$col], "\t"); } print("\n"); }

    so this code prints every element in the same order but now i want to calculate the sum of numbers in each row and print that sum next to gene name i.e sum of values for ALA1 is 33 and sum of values for THR8 is 168 and so on. Now i have tried something like this.

    for($row = 0; $row < $no_of_seq; $row++){ for($col = 0; $col < $no_of_seq; $col++){ $sum = $myArray[$row][$col] + $myArray[$row]; print($sum, "\t"); }

    But this is printing something like this

    140526414289424 140526414289424 140526414289424 1405264142894 +24 140526414289424 140526414212840 140526414212850 140526414212852 1405264142128 +51 140526414212840 140526414213296 140526414213353 140526414213395 1405264142133 +08 140526414213296 140526414288368 140526414288468 140526414288545 1405264142885 +67 140526414288368 140526414313944 140526414313946 140526414313949 1405264143139 +54 140526414313944

    How can i achieve my desired result? thank you in advance

LDAP Channel Binding
1 direct reply — Read more / Contribute
by chenson00
on Mar 25, 2020 at 14:59
    Hello Perl Wisefolk, my organization has long used a fairly simple LDAP implementation based on Net::LDAP. A key snippet of our code:
    my $netldap = Net::LDAP->new( $server, port => $port ); if ( $netldap ) { $conn = $netldap->bind( $self->{ 'user' }->{ 'ldapdn' }, password => $password ); ( $conn->code() ) ? die('LDAP ' . IBEX::_LOGIN::USER_LOOKUP::LOGIN_FAILED() ) : 1; #in Net::LDAP 0 = good login, true = bad login if ( $netldap ) { $netldap->unbind(); } }
    As you may know, Microsoft has raised concerns about Man-In-The-Middle attacks on insecure LDAP connections. Microsoft's recommendation is for system administrators to harden their LDAP configurations by requiring two things: LDAP Signing and LDAP Channel Binding.
    My understanding of these two concepts are as follows:

    What is LDAP Signing?
    LDAP signing is the process of digitally signing LDAP traffic.

    What is LDAP Channel Binding?
    LDAP channel binding refers to binding the TLS tunnel and the LDAP application layer together to create a unique fingerprint, called Channel Binding Token (CBT).

    For the LDAP Signing concern, I have found lots of helpful Perl documentation on ways to accomplish this using various combos of Net::LDAPS and Authen::SASL, so I think I'm good there.
    But for LDAP Channel Binding I am hitting a brick wall. I don't believe the implementing of LDAP Signing will "magically" also take care of LDAP Channel Binding.. this sounds like a different beast. Nor have I found any information on implementations of LDAP Channel Binding done in Perl. Has anyone dealt with this?
Cool Uses for Perl
Applied MCE : Building a load-testing framework for PSGI apps with Plack::Test and MCE::Queue
No replies — Read more | Post response
by 1nickt
on Mar 30, 2020 at 10:49

    If you develop PSGI apps in Dancer2, Mojolicious, Plack or something else, you probably know that it's fairly easy to test all your routes and confirm that they function as expected. But what about the performance of your app under load? Will there be any bottlenecks? Can the database support the concurrent connections you plan to open? Do you have enough, or too many, workers serving requests? Etc.

    The following script shows how to build a load-testing framework for your PSGI app using Plack::Test for the scaffold and MCE::Flow and MCE::Queue to create the concurrent traffic. (The example includes a Dancer2 app to be exercised, but you can of course use your own app class from outside or inside the script, updating the test flow appropriately.)

    The demonstration simulates an API for managing account records that exposes three endpoints: / POST, / GET, / PUT. The workflow/account lifespan is typical: the caller first creates an account record via a POST call, which returns an id that must be used for future calls. The demonstration simulates the caller encountering the account record still with status 'pending' on the first GET call, and having to call again until it is activated. Finally, the caller deletes the account.

    The script is configured to create 1,000 accounts shared among 10 users. (Note that this is an example and includes no parameter checking, error handling, etc. For demonstration purposes only.) The example app creates an SQLite database on disk.

    How it works
    The script uses MCE::Flow to simulate the workflow, and MCE::Queue to manage the jobs. Two user subroutines are defined, one for the queue "producer" and one for the queue "consumers." The "producer" represents callers making the initial requests to create new accounts; these are placed onto the queue at small intervals. The "consumers" of the queue represent callers making subsequent requests to the app and reacting to the response, sometimes by making a new request. In that case a job for the new request is placed on the queue before the "consumer" finishes the current job.

    To tune the test, change the number of consumers, add more accounts to be created, or reduce/increase the sleeps representing work being performed and caller latency.

    To run the test
    Just copy the code, install the dependencies, and run. You may wish to tail the log file (by default at /tmp/test.log) to see what the app is doing. Afterwards you may wish to examine the populated database (by default at /tmp/test.sqlite), as it will be overwritten on the next run.



    script to load-test/profile a PSGI application


    Some sample output:


    The way forward always starts with a minimal test.
Statistics::Covid : module for fetching and storing covid19-related data for analysis
2 direct replies — Read more / Contribute
by bliako
on Mar 26, 2020 at 15:42

    Fellow Monks,

    I have just submitted to CPAN a very alpha release of a module which collects data from various online providers of Covid19-related statistics (e.g. number of confirmed cases etc.). For example, data provided by Johns Hopkins University (as an arcgis "dashboard") or the data provided by the UK government for data relating to the UK local authorities.

    All the providers I used (so far, John Hopkins University and the UK government) offer an API which provides JSON data. The scraper can be easily configured (that is subclassed) to set the url entry point to the API and how data should be converted to a Perl object. So, it is relatively easy to create more data fetchers which can all store to the same db.

    Fetched data is stored in an SQLite database (support for MySQL exists but remains untested and probably broken - but easily fixed) and there is a high-level interface (thank you DBIx::Class) for saving and retrieving this data. This makes it easy to save data points only if they are more "up-to-date" than what currently exists in database, for the same location and time point (using heuristics). Or, it allows to retrieve all data for a single location over time, or for a single time point/range over all or some locations.

    The CPAN module is Statistics::Covid. It is also hosted on github at https://github.com/hadjiprocopis/statistics-covid which additionally provides the data I have so far collected since a couple of weeks ago.

    If anyone has any comments or suggestions please leave me a message.

    If anyone wishes to contribute, e.g. data analysis or plots generation, under this or any other namespace, please let me know so that I link to that work. I am also starting to write my own analysis which will be under the namespace: Statistics::Covid::Analysis.

    Here is some code from the synopsis as a quick start:

    use Statistics::Covid; use Statistics::Covid::Datum; $covid = Statistics::Covid->new({ 'config-file' => 't/example-config.json', 'providers' => ['UK::BBC', 'UK::GOVUK', 'World::JHU'], 'save-to-file' => 1, 'save-to-db' => 1, 'debug' => 2, }) or die "Statistics::Covid->new() failed"; # fetch all the data available (posibly json), process it, # create Datum objects, store it in DB and return an array # of the Datum objects just fetched (and not what is already in D +B). my $newobjs = $covid->fetch_and_store(); print $_->toString() for (@$newobjs); print "Confirmed cases for ".$_->name() ." on ".$_->date() ." are: ".$_->confirmed() ."\n" for (@$newobjs); my $someObjs = $covid->select_datums_from_db({ 'conditions' => { belongsto=>'UK', name=>'Hackney' } }); print "Confirmed cases for ".$_->name() ." on ".$_->date() ." are: ".$_->confirmed() ."\n" for (@$someObjs); # or for a single place (this sub sorts results wrt publication ti +me) my $timelineObjs = $covid->select_datums_from_db_for_location('Hac +kney'); for my $anobj (@$timelineObjs){ print $anobj->toString()."\n"; } print "datum rows in DB: ".$covid->db_count_datums()."\n"

    Edit: thank yous to marto for advice on githubbing this module and to erix for pointing some errors in this page (John -> Johns)

    BW, bliako

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-04-01 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (186 votes). Check out past polls.

    Notices?