If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
Filtering array of strings and numbers
3 direct replies — Read more / Contribute
by nysus
on Apr 29, 2016 at 11:28

    I'm stuck on what I think should be a simple problem. I want to filter out elements from an array. I don't know if they array contains strings or numbers or both. And I don't know if the value I want to filter out is a string or number:

    my $string_or_number; my @filtered = grep { $string_or_number ne $_ } @strings_or_numbers;

    What's the best way to accomplishing this without throwing an error if the wrong equality operator is used?

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

Detached forking in a CGI script
4 direct replies — Read more / Contribute
by xylose
on Apr 29, 2016 at 11:06

    I'm having a problem satisfying a number of constraints in a CGI script I'm writing.

    I need the script to fork a child process and to get hold of the pid of this child, but to then not make the parent (the CGI process) wait for the child, and allow it to exit immediately.

    One extra complication is that in the child I exec a new program (an R script), but capture STDOUT and STDERR into log files, so I can't close those filehandles before doing the exec.

    I've tried a number of different approaches, but anything which gives me the pid and starts the child process correctly always makes the parent hang until the child is done.

    My current (not working) best guess is:

    $SIG{CHLD} = 'IGNORE'; my $pid = fork(); if ($pid) { # We're the child and we need to start the analysis # First we'll write out pid into a file in the run # folder so that the results tracker can tell if we've # died. open (PID,'>','pid.txt') or die "Failed to write to pid file : $!" +; print PID $pid; close PID or die "Failed to write to pid file: $!"; exec("Rscript analysis.r > log.txt 2>errors.txt"); exit(0); } print $q->redirect("script.cgi?job_id=$job_id");

    In this version the child runs, but the parent doesn't exit until the child has completed.

    Any ideas how I can have my cake and eat it?

    Thanks.

How to upload a file in a test through Open Windows using Selenium::Remote::Driver Perl package
2 direct replies — Read more / Contribute
by Chaoui05
on Apr 29, 2016 at 08:28

    Below are the additional bullets : I can get my "button" with following code :

    $elem = $driver->find_element('//*[@id="file_uploader"]/div/div/div/d +iv[1]/div[1]/div/span'); $driver->mouse_move_to_location(element => $elem); # xoffset => x +, yoffset => y $driver->click_ok('LEFT'); $driver->pause(3000);

    But after that, I can't get anything in Opened Window which appears to upload. How is it possible to upload a file also?

    Thanks in advance !
Another concurrent HTTP requests script - code review request
No replies — Read more | Post response
by alanek
on Apr 29, 2016 at 06:02
    Hi PerlMonks! After years of using Parallel::ForkManager to speed up making thousands of HTTP request, I was adviced by Ikegami at Stackoverflow.com to give Net::Curl::Multi a try. And I have to say its awesome! Now, becouse its my first time using it, and becouse final version of the code will have to deal with external, 3rd party API, I would really want to be sure its working fine and doesnt have any bugs. Below You will find my test script based on Ikegami's advice. It works, I dont have any problems with it, but maybe some more experienced Perl users would notice something that I didnt, or tell me if I shouldn't do something like I did, etc. My main concern is the LINE loop and way that I add URL to N::C::M queue, fill the queue with more URLs, wait for one of them to finish, and go to the next URL and add it. Some notes:
    • Im using external txt file with URLs becouse there might be A LOT of them (hundret of thousands even) so I dont want to load everything to array.
    • As I don't fully understand how Net::Curl::Multi's concurrency works, please tell me whether I should expect any problems with putting MySQL UPDATE command (via DBI) inside RESPONSE loop (besides higher server load obviously - I expect final script to run with at least 50 concurrent N::C::M workers)
    Thanks!
    #!/usr/bin/perl use Net::Curl::Easy qw( :constants ); use Net::Curl::Multi qw( ); sub make_request { my ( $url ) = @_; my $easy = Net::Curl::Easy->new(); $easy->{url} = $url; $easy->setopt( CURLOPT_URL, $url ); $easy->setopt( CURLOPT_HEADERDATA, \$easy->{head} ); $easy->setopt( CURLOPT_FILE, \$easy->{body} ); return $easy; } my $maxWorkers = 10; my $multi = Net::Curl::Multi->new(); my $workers = 0; my $i = 1; open my $fh, "<", "urls.txt"; LINE: while ( my $url = <$fh> ) { chomp( $url ); $url .= "?$i"; print "($i) $url\n"; my $easy = make_request( $url ); $multi->add_handle( $easy ); $workers++; my $running = 0; do { my ($r, $w, $e) = $multi->fdset(); my $timeout = $multi->timeout(); select $r, $w, $e, $timeout / 1000 if $timeout > 0; $running = $multi->perform(); RESPONSE: while ( my ( $msg, $easy, $result ) = $multi->info_r +ead() ) { $multi->remove_handle( $easy ); $workers--; printf( "%s getting %s\n", $easy->getinfo( CURLINFO_RESPON +SE_CODE ), $easy->{url} ); } # dont max CPU while waiting select( undef, undef, undef, 0.01 ); } while ( $workers == $maxWorkers || ( eof && $running ) ); $i++; } close $fh;
Execute a command line and save in memory its output
2 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 29, 2016 at 04:28

    Hello,

    I'm reading a netCDF file to extract a timeseries using a command line named ncks.

    I dump its output in a temporary file and then I read the temporary file to extract the values and push them into an array.

    Is there a way to store in memory the extracted records (@tmplines in the code below) without writing them on a temporary file?

    Since I have to call the subroutine thousands of times I imagine that it would speed up things.

    Thanks for any suggestion and best regards.

    use strict; use IPC::Run qw( run ); use Tie::File; use Fcntl 'O_RDONLY'; # ... cut ... my @ts = get_time_series(File=>$ncfile,Variable=>$ncvar,Id=>$nc_id); # ... cut ... sub get_time_series { my %args = @_; my $file = $args{File}; my $variable = $args{Variable}; my $id = $args{Id}; my $tmpfile = "$id.tmp"; # Example: # ncks -v vmro3 -d station,1,1 myfile.nc | grep "station\[" # my $command = qq(ncks -v $variable -d station,$id,$id $file | grep + \"station\\[\" > $tmpfile); run $command; # This will write in file the following records: #station[3385]=3386 #time[0]=3287.02083333 station[3385]=3386 vmro3[3385]=1.53524e-08 mole + mole-1 #time[1]=3287.0625 station[3385]=3386 vmro3[7513]=1.55109e-08 mole mol +e-1 #time[2]=3287.10416667 station[3385]=3386 vmro3[11641]=1.56481e-08 mol +e mole-1 my @ts; tie my @tmplines, 'Tie::File', $tmpfile, mode => O_RDONLY; foreach my $ii (1..$#tmplines) { my $line = $tmplines[$ii]; my @pa = anytrim(split(/\s/,$line)); my ($dummy,$value) = split(/=|\s/,$pa[2]); push @ts, $value; } unlink $tmpfile or die "$tmpfile: $!"; return @ts; }
Any module for saving data as perl data?
3 direct replies — Read more / Contribute
by PerlBroker
on Apr 29, 2016 at 03:25
    Is there any module or way, to simply save data as Perl, for example if I have $a = "hello", that I can save it in exact that form in a file: $a = "hello"; including the same for hashes and arrays?
count the occurrence of second line of a paragraph in a file
2 direct replies — Read more / Contribute
by umaykulsum
on Apr 29, 2016 at 01:02
    I have multiple files like data.txt:
    @NS500278:42:HC7M3AFXX:1:11101:16723:1045 1:N:0:AACGTGAT AGATCNGAAGAGCACACGTCTGAACTCCAGTCACAACGTGATATCTCGTATGCCGTCTTCTGCTTGAAAA +AAAAAAGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG +GGGGGGGGGGGG + AAAAA#EEEEEEEEEEEEEEEE6EEEEEAEEEAE/AEEEEEEEAE<EEEEA<A/AE<EE/EEAEEAEEAE +EEEA///EEEEEEEEEAEEEEEEEEEEEEEEEEEEEE/EEEAEEEAEEEEEEEEEAEAEEEEEEEEEEE +EAEEEEEAEEAA @NS500278:42:HC7M3AFXX:1:11101:20279:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEAEEEEEEEEEEEEEEEEEEEEAEEEEEEEE/EEEAE6AE<EAEEAEAAEEAEEEEE +EEAE/EEAEEAEEE6EEEEEAE6A/E<EEEEEEEEAE<EEEEEA/AEEAAEEEEEE//AEE/<<<EEAE +<66/</AE<<A6 @NS500278:42:HC7M3AFXX:1:11101:18609:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEAEEEEEEEEAEEEAEEEEAEEAEEEE +AEEEA//EEAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE +EEAAAEAEEEEA
    I want to print the whole paragraph (four lines) and the count of second line in the file. The output for above file should be:
    @NS500278:42:HC7M3AFXX:1:11101:16723:1045 1:N:0:AACGTGAT AGATCNGAAGAGCACACGTCTGAACTCCAGTCACAACGTGATATCTCGTATGCCGTCTTCTGCTTGAAAA +AAAAAAGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG +GGGGGGGGGGGG + AAAAA#EEEEEEEEEEEEEEEE6EEEEEAEEEAE/AEEEEEEEAE<EEEEA<A/AE<EE/EEAEEAEEAE +EEEA///EEEEEEEEEAEEEEEEEEEEEEEEEEEEEE/EEEAEEEAEEEEEEEEEAEAEEEEEEEEEEE +EAEEEEEAEEAA 1 @NS500278:42:HC7M3AFXX:1:11101:20279:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEAEEEEEEEEEEEEEEEEEEEEAEEEEEEEE/EEEAE6AE<EAEEAEAAEEAEEEEE +EEAE/EEAEEAEEE6EEEEEAE6A/E<EEEEEEEEAE<EEEEEA/AEEAAEEEEEE//AEE/<<<EEAE +<66/</AE<<A6 2
    The code which I have written is:
    use strict; use warnings; my @files=('data.txt'); for my $input_file (@files) { my $output_file = $input_file.".out"; process_file($input_file, $output_file); } sub process_file { my($input_file, $output_file) = @_; my %count; my $file = shift or die "Usage: $0 FILE\n"; open my $fh, '<', $file or die "Could not open '$file' $!"; open my $fa, '>', $output_file; $/=""; while (my $line = <$fh>) { foreach my $str ($line) { chomp $line; $count{$str}++; } } foreach my $str (sort keys %count) { printf $fa "%-s %s", $str."\t", $count{$str}; print $fa ":".$input_file."\n"; } }
    This gives the count of whole paragraph instead of the second line. It matches the whole paragraph but I want to print the whole paragraph and count of only the second line.
Manipulating tab delimited file
2 direct replies — Read more / Contribute
by andyBio
on Apr 28, 2016 at 21:54

    Hello, wise ones! I am trying to read in a file with content like this:

    TATTATGAGAATAGTGTGCATTTT 3 ATAGAGCAAAAGGGCAAATGCTGA 6 TACGAGTAGGATATCGATCTGGTGG 2 ATCCCCGGCATCTCCGCCA 1 TGAGAATAGTGTGCATTT 52 CGCATTACATTTGGAGCC 1 ACTCCAGGCAGCGTAGAGTT 1 ATCAACGTTGCTGCATCGG 1
    It is a tab demilimited file. I want to manipulate the file to have it look like this:
    >dme0_count=3 TATTATGAGAATAGTGTGCATTTT >dme1_count=6 ATAGAGCAAAAGGGCAAATGCTGA >dme2_count=52 TGAGAATAGTGTGCATTT
    (i). Sequences(column 0) with length less than 15 or greater than 30 are ommitted (ii). Sequences with counts (Column 1) < 2 are ommitted (iii). the print statement takes uses an argument and the count for the header line. Here is my code:

    #!/usr/bin/perl -w use strict; use warnings; my $species=$ARGV[0]; my $input=$ARGV[1]; my @fields; my $n = 0; open my $tabdata, '<', "$input" or die ("Can't open $input\n"); while (my $line = <$tabdata>) { foreach my $line ($tabdata){ my @fields = split("\t",$line); if(($fields[1] > 2) && (length($fields[0]) > 14 && length($fields[ +0]) < 31)) {print ">$species" . $n++ . "_count=$fields[1]\n$fields[0]\n"} +; } } close ($tabdata);
    Here is the error: a> line 1. Use of uninitialized value $fields[1] in numeric gt (>) at tab.pl line + 19, <$tabdata> line 2. Use of uninitialized value $fields[1] in numeric gt (>) at tab.pl line + 19, <$tabdata> line 3. . . . Use of uninitialized value $fields[1] in numeric gt (>) at tab.pl line + 19, <$tabdata> line 21.

    I'd appreciate some assistance on how to get this to work! Thank you.

Backup Script Pb
1 direct reply — Read more / Contribute
by WTem
on Apr 28, 2016 at 20:30

    Hello PerlMonks !! Here i am seeking for your Wisdom again ^^

    My goal is to auto save the configuration parameters of a router Netcomm 140w.

    The procedure is to connect to the router with ssh and use the following commands to create the backup conf file:

    # cd /tmp # dbcfg_export -o /tmp/filename.cfg -p "password" # tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn-keys ssh-hostkeys | o +penssl des3 -salt -k "password" | dd of=/tmp/vpn.des3 # tar -zcvf /opt/cdcs/upload/filename.cfg.tar.gz filename.cfg vpn.des3

    The second command will create the file "/tmp/filename.cfg". In the end the backup conf file created is "/opt/cdcs/upload/filename.cfg.tar.gz" wich I will next recover via SCP

    Below an exemple with filename = test and with no password. Don't bother with the missing files with the 1st tar command warning;

    root:/# cd /tmp root:/tmp# dbcfg_export -o /tmp/test.cfg -p "" root:/tmp# tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn-keys ssh-hos +tkeys | openssl des3 -salt -k "" | dd of=/tmp/vpn.des3 ipsec.d/ ipsec.d/crls/ ipsec.d/aacerts/ ipsec.d/policies/ ipsec.d/cacerts/ ipsec.d/certs/ ipsec.d/rsakey/ ipsec.d/private/ ipsec.d/ocspcerts/ tar: openvpn-keys: Cannot stat: No such file or directory ssh-hostkeys/ ssh-hostkeys/ssh_host_dsa_key ssh-hostkeys/ssh_host_rsa_key.pub ssh-hostkeys/ssh_host_ecdsa_key.pub ssh-hostkeys/ssh_host_key ssh-hostkeys/ssh_host_rsa_key ssh-hostkeys/ssh_host_dsa_key.pub ssh-hostkeys/ssh_host_key.pub ssh-hostkeys/ssh_host_ecdsa_key WARNING: can't open config file: /lib/ssl/openssl.cnf tar: Exiting with failure status due to previous errors 9+1 records in 9+1 records out 4792 bytes (4.7KB) copied, 0.081756 seconds, 57.2KB/s root:/tmp# tar -zcvf /opt/cdcs/upload/test.cfg.tar.gz test.cfg vpn.des +3 test.cfg vpn.des3

    Now here my perl code, filename = test2 with no password

    #!/usr/bin/perl -w ###################################### Perl Script ################### +############################### ## + ## ## + ## ## Auteur: WILLIAMS Temoe + ## ## Date: 21/04/16 + ## ## Description: The script will create a backup conf fi +le of a router Netcomm 140W used for business clients + ## ## ## ## + ## ## + ## ###################################################################### +############################### use strict; use warnings; #### Déclaration Bibliothèques use Net::SSH2; use Time::Piece; #### Déclaration Variables globales my $date = Time::Piece->new->strftime('%d-%m-%Y'); my $user='root'; my $password='W1m4x21h'; my $portssh=22; my $pwdfile=""; ################# Netcomm140w Backup process via SSH connexion ####### +################## ################# Use router @IP and client name as variables ######## +################# sub sauvegarde_netcomm { #### Variable declaration my($ip, $filename) = @_; #### Initialize SSH connexion my $ssh = Net::SSH2->new(); $ssh->connect($ip, $portssh) or die; if ($ssh->auth_password($user,$password)) { ## Command lines for the backup process my $chan = $ssh->channel(); $chan->shell(); $chan->blocking(0); print $chan "cd /tmp\n"; print $chan "dbcfg_export -o /tmp/$filename.cfg -p '$pwdfile +'\n"; print $chan "tar -C /usr/local/cdcs -zcvf - ipsec.d openvpn- +keys ssh-hostkeys | openssl des3 -salt -k '$pwdfile' | dd of=/tmp/vpn +.des3\n"; print $chan "tar -zcvf /opt/cdcs/upload/$filename.cfg.tar.gz + $filename.cfg vpn.des3\n"; #print $chan "rm -f /tmp/$filename.cfg\n"; #### Close SSH connexion $chan->close(); sleep (1); }else { #### Error message when ssh connexion fail warn "auth failed.\n"; } } ################# Main Program ################################ +######################## ################# Netcomm140w Backup ################################# +####################### sauvegarde_netcomm ('192.168.200.62' , 'test2');

    Now we can see the problem by comparing the size of the created files

    root:/tmp# ll /tmp/t* -rw-r--r-- 1 root root 31331 Apr 28 13:55 /tmp/test.cfg -rw-r--r-- 1 root root 31331 Apr 28 13:58 /tmp/test2.cf +g root:/tmp# ll /opt/cdcs/upload/ -rw-r--r-- 1 root root 12548 Apr 28 13:55 test.cfg.tar. +gz -rw-r--r-- 1 root root 20 Apr 28 13:58 test2.cfg.tar +.gz

    I suppose that the problem comes from the 3rd command but I can't explain why

    Can you please help me with this ? Best regards. Temoe

Pronouncible TLA's?
6 direct replies — Read more / Contribute
by BrowserUk
on Apr 28, 2016 at 17:36

    Is there a metric by which the pronounceability of of a combination of letters can be judged? And is there anything that Perl can do to measure it?

    Pronounceable by whom is a variable, xkz might be pronounceable in Czech or Polish; but since I am chronically monoligual; let's stick to 'by native English speakers'.

    That still leaves plenty of scope for sounds made by the native English speakers from some parts of the world that are totally unreproducible by those from others; but its a place to start.

    To be useful, the pronunciations would need to be distinct; and changing the pronunciation of known word, to enable a less common spelling to (re)use that sound defeats the purpose.

    Ie. Deciding to pronounce 'are':ah-rey, so that 'aar' can be pronounced as ah doesn't work.

    Also, I think that the Dutch could intone a difference between 'six' and 'syx' and hear the difference; but I'm pretty sure I can't do either.

    For the 'Perl content' and 'what have you got' criteria, this produces all 17,576 3-letter combinations. Can you add code to eliminate the unpronounceable?:

    use Algorithm::Combinatorics qw[ variations_with_repetition ];; $i = variations_with_repetition( [ 'a'..'z' ], 3 );; $" = ''; print "@$_" while $_ = $i->next;; aaa aab aac aad ...

    I thought perhaps that any combination that contained a vowel or y might work:

    $" = ''; $_ = join( '', @$_ ), m[[aeiouy]] and print while $_ = $i->ne +xt;;

    But I don't think any of these would qualify: xxa xxe xxi xxo xxu xxy xza xze xzi xzo xzu xzy.

    And there is a distinguish-ability problem with things like gga gja jga & jja. Again the Dutch might get something like kh-yah & ye-gah for the middle two, but I probably wouldn't be able to pronounce either to their satisfaction.

    The application, albeit a light-hearted speculation rather than yet a serious pursuit, is the Huffman encoded (I really just mean 'short' here) naming of a few (3-7?) thousand items (variables; values) in a phonetically pronounceable way, for clarity of verbal communication, and memorability.

    And why not just use numbers? The hope is that at least some of the more common items could be named in some vaguely mnemonic way; to aid in the second of the above goals in a way that "Item 237' never will.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.