Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

The Monastery Gates

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

Donations gladly accepted

  • (Sep 10, 2018 at 22:53 UTC) Welcome new users!
If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Truly Isolated Perl
4 direct replies — Read more / Contribute
by mikkoi
on Sep 19, 2018 at 06:57

    For using on a website, I need to create a completely isolated Perl. Not just local::lib isolated module space. My first thought was using plenv install but it installs new Perl executable only under ${PLENV_ROOT}. What I need is to install to ~/public_html (or similar), e.g. ~/public_html/prog/env/bin/perl. And this new Perl executable would use ~/public_html/prog/env/lib as its @INC, and only that!

    Currently this is not possible with plenv install. But could we make it so? Or is there an existing easy way? Instead of actually manually configuring and building Perl.

Blowing smoke tests
1 direct reply — Read more / Contribute
by nysus
on Sep 18, 2018 at 20:16

    Got a module that is failing many of its smoke tests. Most of the failed tests report they can't find the Getopt::Args module. I must have some kind of misconfig or something in my dist:zilla. Here's my Makefile.PL:

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

ithreads, locks, shared data: is that OK?
1 direct reply — Read more / Contribute
by bliako
on Sep 18, 2018 at 19:11

    I want some advice for when using Perl's ithreads to do parallel processing and return back the results.

    For this particular situation I have one or more input words and a huge dictionary. I search the input word for similar words (given some metric) and return back these similar words. I repeat the process with these words as input and so on until I go into some depth and be able to create a Graph of a few hundred nodes.

    Parallelisation: each thread checks for its input word via a Thread::Queue and when it gets one it calls a function ref which will know where the dictionary is and what to do to get the similar words out. I have included a mock function here which just gets random words as the other one is too long and convoluted.

    Additional to the work-queue, I have three more Thread::Queue objs: to save words-currently-being-processed (so as not to re-process them), for words-done-i.e.-results, and for failures-of-the-distance-function.

    I also have a couple of shared scalars : an integer to contain the total number of items processed and a flag to know if processing has to abort.

    Finally, I have the huge dictionary which is absolutely readonly and I do not know whether I have a choice in just distributing a ref to each thread rather than duplicating it to each.

    The test program works OK. (one needs a dictionary file which linux has at specified path or get 1MB worth from https://gist.githubusercontent.com/wchargin/8927565/raw/d9783627c731268fb2935a731a618aa8e95cf465/words) but I am not really sure I am doing the right things sometimes. For example where I am locking a Thread::Queue or a shared variable. And about passing references to scalars to the threads to keep track of the total number of items each thread processes. It's lovely in C but is it ok here?

    Most importantly, is there a way to avoid duplicating that huge dictionary (without explicitly share()ing each and every entry of the array or hash.

    I have another question, less important: I tried to use my own SIGINT handler to call stop() on Ctrl-C but it does not propagate all the way to the threads when threads start. I would say it is being completely overriden (by what?).

    Here is the program as a test preamble and the package in one file, many thanks bliako:

(solved) combining perlbrew and cpanm
1 direct reply — Read more / Contribute
by LanX
on Sep 18, 2018 at 12:58
    Hi

    I'm using an Ubuntu VM to test some stuff in different perlversions plus module-versions and already installed perlbrew succesfully.

    Unfortunately it seems that I installed and (sudo) used cpanminus before.

    which cpanm shows /usr/bin/cpanm and I already renamed and recreated .cpanm because it belonged to root.

    Using this instance from cpanm has strange side effects, looks like the old path from the system Perl where hardcoded.

    So whats the best way to resolve this?

    • apt-get uninstall cpanminus and reinstall it via cpan?
    • cpan install cpanm and hope that perlbrew gets the paths right?
    Yeah I know, probably I shouldn't use cpanm with System Perl in the future... but ... I didn't expect this VM to be used that long.

    So what's the canonical way to combine cpanminus with perlbrew?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    oops ... ((shame-oticon))

    Found:

    apparently I'm too colorblind to read https://perlbrew.pl/Perlbrew-and-Friends.html properly, already the second phrase says:

    > Also when you read the perlbrew usage documentation, there is a command install-cpanm that installs a standalone executable cpanm to to the same bin directory.

    I'll try later, but i think the issue is resolved now ...

    update

    works as described! ++

SOLVED: Predictable repeatable hash order in CORE
3 direct replies — Read more / Contribute
by VinsWorldcom
on Sep 18, 2018 at 09:55

    UPDATE

    I was so focused on an ordered hash, I didn't read the JSON::PP perldoc to see canonical() gives me what I want and due to the names of my hash keys, alphabetical works just fine. Thank you choroba for the quick response in Re: Predictable repeatable hash order in CORE.

    Original node follows:


    I see Tie::IxHash is not in core, but Tie::Hash is - which is the base class Tie::IxHash uses to create ordered hashes. Is there a way to get ordered hashes (predictable, repeatable order when printed for example) in CORE - other than rolling my own Tie::Hash extension (cause if I'm going that route, I might as well just install Tie::IxHash)?

    Background (in case you're interested):

    I've tested my code with Tie::IxHash and it works fine, so I may have to bite the bullet and teach a "even you can install Perl modules" course for my co-workers.

Perl alternation regex looks ok to me?
3 direct replies — Read more / Contribute
by misterperl
on Sep 18, 2018 at 09:51
    I've been a Perl programmer since about 1997 and I know a lot about regexes. Not a guru perhaps, but at least a master :) But this I don't get.

    I had a string like XT3USI , and I want to test to see if it began with (any char), followed by T, followed by 2 or S. So I used /^.T2|S/

    which looked perfect. Reading from L to R, "begins with a char, then a T, then a (2 or an S)". But instead it acted like I had used: /^.T(2|.*S)/

    2|S, in my mind, should have alternated chars 2 or S, not 2 or (0-many other chars) followed by S.

    I replaced it with /^.(T2|TS)/ which bugs me. But works..

    Advice is, as always, appreciated. I use it all the time, but maybe all these years I misunderstood alternation.
Renaming files sequentially into directory
1 direct reply — Read more / Contribute
by Magnolia25
on Sep 18, 2018 at 09:39

    I got some files in directory as below:

    000001.OrderID.html 000002.OrderID.cgi 000003.OrderID.pl 000004.OrderID.js 000005.OrderID.txt 000006.OrderID.cgi 000007.OrderID.minus.html OrderID.master

    Based on some condition certain files are copied into other directory.

    (000003.OrderID.pl & 000007.OrderID.minus.html are not copied)

    000001.OrderID.html 000002.OrderID.cgi 000004.OrderID.js 000005.OrderID.txt 000006.OrderID.cgi OrderID.master

    What I need is to correct the sequence number of files after they are copied into other directory

    000001.OrderID.html ** remain as is 000001.OrderID.html - Should be +ignored for renaming 000002.OrderID.cgi ** remain as is 000002.OrderID.cgi - Should be +ignored for renaming 000004.OrderID.js => 000004.OrderID.js Rename to => 000003.OrderI +D.js 000005.OrderID.txt => 000005.OrderID.txt Rename to => 000004.OrderI +D.txt 000006.OrderID.cgi => 000006.OrderID.cgi Rename to => 000005.OrderI +D.cgi OrderID.master ** OrderID.master Should be ignored for renamin +g

    Apparently I am stuck here. Please help.

convert C# to perl script
1 direct reply — Read more / Contribute
by Metilda
on Sep 18, 2018 at 03:56

    Please help to convert this C# code to perl

    var client = new RestClient("url"); var request = new RestRequest(Method.PUT); // Create SSL certificate // Certificate CN name is used to authenticate with Secure Gateway string certificateFile = "<SSL_Certificate>.pfx"; var certificate = new X509Certificate(certificateFile); client.ClientCertificates = new X509CertificateCollection(); client.ClientCertificates.Add(certificate);
Login form
4 direct replies — Read more / Contribute
by bachoA4o
on Sep 18, 2018 at 01:15
    Hello I'm making a a form that create a user with username and password and then crypts the data and store it in database ; password crypting is working fine i think but stores the name as CGI::Simple=HASH(0x55bb333591e0). Here is the code for creating the user:
    #!/usr/bin/perl use strict; use warnings; use DBI; use CGI::Simple; use Crypt::Argon2 qw/argon2id_pass argon2id_verify/; sub user_crypt;sub createUser; sub user_crypt { my ($cryUser , $cryPass)=@_; my $salt = rand(16); my $encode = argon2id_pass($cryPass,$salt,3,'32M',1,16); createUser($cryUser,$encode); } sub createUser{ my ($user , $hashPass)=@_; my $statement="INSERT INTO LOGIN_DATA(USER_NAME,USER_HASH) VALUES(?,?) +"; my $driver="mysql"; my $database="USERS"; my $dsn="DBI:$driver:database=$database"; my $databaseUser="root"; my $databasePass=""; my $dbcon= DBI->connect($dsn,$databaseUser,$databasePass) or die $!; my $preState = $dbcon->prepare($statement) ; $preState->execute($user , $hashPass) or die $!; } my $username=CGI::Simple->new; my $pass=CGI::Simple->new; $username->param('textfield'); $pass->param('pass'); user_crypt($username,$pass); print "Content-type: text/html\n\n"; print "<html><body>\n"; print "<h1>Random heading</h1>\n"; print "$username , $pass"; print "</body></html>\n";
    and this is how is stored in the database
    CGI::Simple=HASH(0x55bb333591e0) | $argon2id$v=19$m=32768,t=3,p=1$MTMu +ODI1NjU0MDY4Mjc3OQ$V83Q4GNnwnY1187C/whS/Q
    and after the program that verifies but of course it's not working because don`t know how to fetch the username .
    #!/usr/bin/perl use strict; use warnings; use Crypt::Argon2 qw/argon2id_pass argon2id_verify/; use DBI; sub get_data; sub chec_pass; sub get_data{ my ($user , $hash_pass); my $statement = "SELECT * FROM LOGIN_DATA"; my $driver = "mysql"; my $database = "USERS"; my $dsn = "DBI:$driver:database=$database"; my $dataUsr = "root"; my $dataPass = ""; my $dbcon = DBI->connect($dsn,$dataUsr,$dataPass) or die $!; my $preState = $dbcon->prepare($statement); $preState->execute(); my @row; while (@row = $preState->fetchrow_array()) { } } sub chec_pass { my ($user, $password) = @_; my $encoded = fetch_encoded($user); return argon2id_verify($encoded , $password); }
assigning arrays as values to keys of hash
5 direct replies — Read more / Contribute
by pearllearner315
on Sep 17, 2018 at 23:52
    Hello monks, I have a file formatted like this:


    bird beak
    bird beak
    bird claw
    bird wings
    bird feathers
    snake fangs
    snake scales
    snake fangs
    snake tail

    I want to loop through this file and get the bird and snake as keys, and the word next to them as the values in an array. I only want unique elements tho for each array so for example, bird will be the key in the hash, and its corresponding value will be an array that contains (beak, claw, wings, feathers). I have the current code that works but it doesn't get rid of the duplicates:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $file = 'file.txt'; open( FILE, '<', $file ) or die $!; my %hash; while ( <FILE> ) { chomp; my $lines = $_; my $key = (split(/" "/, $lines))[0]; my $value = (split(/" "/, $lines))[1]; push @{ $hash{$key} }, $value; } print Dumper(\%hash);
    How would I get rid of the duplicates in the arrays?
How to bypass File::Slurp when it is required by another module?
2 direct replies — Read more / Contribute
by vitoco
on Sep 17, 2018 at 02:02

    Hi! This seems to be quite repetitive but I still cannot figure out how to solve this...

    I'm using Email::MIME::CreateHTML to build an email that contents an HTML message with a PNG image that I want to include in the email encoded as BASE64. Everything seems to work OK except for the fact that in the email, when displayed by the recipient's email client, the image is broken. I've decoded the BASE64 part of the multipart body and found that the encoded file had 1 byte less that the original image file. So I binary compared the original and decoded files and found that the missing byte was because read_file in File::Slurp always does a CR/LF into NL substitution with the asumption that it is a text file just because I'm running in a Windows machine ($buf =~ s/\015\012/\n/g if $is_win32 ;), and this binary image just happens to have that sequence of bytes in it's header.

    Is there a way to replace the read_file method, without editing the modules or rewriting a whole resolver, or to force it to use binmode? Well, Slurp accepts some options like binmode, but this obvously needs CreateHTML to send that option to it, and I'm not sure if that works with small binary files.

    Thanks!

assigning value of a key in a hash to a variable while dereferencing
5 direct replies — Read more / Contribute
by pearllearner315
on Sep 17, 2018 at 01:00
    Hi guys, I'm trying to assign the value of a key in a hash to a variable but want to use a hash reference. Without dereferencing the hash before on a separate line, how could I assign the value of the key to the variable while dereferencing at the same time? Here is some code:
    #!/usr/bin/perl use warnings; use strict; # defines country => language hash my %langs = ( England => 'English', France => 'French', Spain => 'Spanish', China => 'Chinese', Germany => 'German'); my $countryid = 'England'; my $langsref = \%langs; # get language of England my $englishlang = %{ $langsref }{$countryid}; # <---- this doesn't wo +rk. I wanna do the assignment all in this one line but can't figure o +ut assigning while dereferencing. print $englishlang;
    Any help would be great. Thanks so much!
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 studying the Monastery: (8)
As of 2018-09-19 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Eventually, "covfefe" will come to mean:













    Results (166 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!