Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Seekers of Perl Wisdom

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

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
Net::SFTP::Foreign Password Authentication Hangs
3 direct replies — Read more / Contribute
by dano63
on Oct 18, 2017 at 16:42
    Greetings Monks,

    I'm using Net::SFTP::Foreign (which I've used elsewhere for years) as part of a new on-prem file movement service and am running into a problem where it's simply hanging when it gets to password authentication. Doing a manual sftp works so this is something else. I have even stripped down my code to only the relevant lines and it's doing the same thing.

    My OS is Fedora 25, the perl is "(v5.26.1) built for x86_64-linux-thread-multi" and Net::SFTP::Foreign is 1.87 and everything is patched and up to date. Thanks for any help you can provide!


    $Net::SFTP::Foreign::debug = 1; my $sftp = Net::SFTP::Foreign->new( $Server, user => $User, password => $Pass, more => '-vvv' );
    With the results:
    debug1: Authentications that can continue: password,keyboard-interacti +ve,publickey debug3: start over, passed a different list password,keyboard-interact +ive,publickey debug3: preferred keyboard-interactive,password debug3: authmethod_lookup keyboard-interactive debug3: remaining preferred: password debug3: authmethod_is_enabled keyboard-interactive debug1: Next authentication method: keyboard-interactive debug2: userauth_kbdint debug3: send packet: type 50 debug2: we sent a keyboard-interactive packet, wait for reply debug3: receive packet: type 60 debug2: input_userauth_info_req Password authentication debug2: input_userauth_info_req: num_prompts 1 # queueing msg len: 5, code:1, id:3 ... [1] # waiting for message... [1] debug3: send packet: type 61 debug3: receive packet: type 51 debug1: Authentications that can continue: password,keyboard-interacti +ve,publickey debug2: we did not send a packet, disable method debug3: authmethod_lookup password debug3: remaining preferred: debug3: authmethod_is_enabled password debug1: Next authentication method: password
    The hang is after the last line above. Perhaps not helpful but I also did an strace and got (at the end of much more output):
    Password authentication debug2: input_userauth_info_req: num_prompts 1 ) = 1 (in [3], left {tv_sec=0, tv_usec=780618}) read(3, "Password: ", 4096) = 10 write(3, "AintTellin\n", 10) = 10 wait4(21851, 0x7ffe1ea64784, WNOHANG, NULL) = 0 select(8, [3], NULL, NULL, {tv_sec=1, tv_usec=0}) = 1 (in [3], left {t +v_sec=0, tv_usec=999989}) read(3, "\r\n", 4096) = 2 close(4) = 0 close(3) = 0 fcntl(5, F_GETFL) = 0 (flags O_RDONLY) fcntl(5, F_SETFL, O_RDONLY|O_NONBLOCK) = 0 fcntl(8, F_GETFL) = 0x1 (flags O_WRONLY) fcntl(8, F_SETFL, O_WRONLY|O_NONBLOCK) = 0 write(2, "# queueing msg len: 5, code:1, "..., 45# queueing msg len: + 5, code:1, id:3 ... [1] ) = 45 write(2, "# waiting for message... [1]\n", 30# waiting for message.. +. [1] ) = 30 rt_sigaction(SIGPIPE, NULL, {sa_handler=SIG_DFL, sa_mask=[], sa_flags= +0}, 8) = 0 rt_sigprocmask(SIG_BLOCK, [PIPE], [], 8) = 0 rt_sigaction(SIGPIPE, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RES +TORER, sa_restorer=0x7f688564a5d0}, {sa_handler=SIG_DFL, sa_mask=[], +sa_flags=0}, 8) = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 select(16, [5], [8], NULL, NULL) = 1 (out [8]) write(8, "\0\0\0\5\1\0\0\0\3", 9) = 9 select(8, [5], NULL, NULL, NULLdebug3: send packet: type 61 debug3: receive packet: type 51 debug1: Authentications that can continue: password,keyboard-interacti +ve,publickey debug2: we did not send a packet, disable method debug3: authmethod_lookup password debug3: remaining preferred: debug3: authmethod_is_enabled password debug1: Next authentication method: password
    Again with the hang at the last line.
Why doesn't this die with "Can't use an undefined value as an ARRAY reference"?"
1 direct reply — Read more / Contribute
by kikuchiyo
on Oct 18, 2017 at 13:49

    Consider the following script:

    #!/usr/bin/perl use strict; use warnings; use Test::More; use Data::Dumper; my $hash = { '50' => [ 1 ] }; print Dumper $hash; is(keys %{$hash}, 1, q/keys %{$hash} is 1/); is(scalar @{$hash->{'50'}}, 1, q/$hash->{'50'} is 1/); is(scalar @{$hash->{'100'}}, 0, q/$hash->{'100'} is 0/); print Dumper $hash; done_testing();

    With Perl 5.24.3 it runs to the end and all tests pass, even though I would expect that it dies with an "Can't use an undefined value as an ARRAY reference" error when it tries to dereference $hash->{'100'} which indeed does not exist.

    Compare with

    #!/usr/bin/perl use strict; use warnings; my $hash = { '50' => [ 1 ] }; print scalar @{$hash->{'100'}};

    which dies with the expected error.

    Under Perl 5.16 the first program also dies with the expected error. (This is how we initially noticed the problem: a program that was developed on 5.22+ needed to be ported to Centos 7 which has 5.16, and the tests began to fail there.)

    What is going on here?

    (Errata: Now I've ran with more Perl versions (perversions), and it doesn't die under perl 5.22 and above, but dies as expected under perl 5.20 and below)

Storing output of a subroutine into an hash and then printing hash
3 direct replies — Read more / Contribute
by Maire
on Oct 18, 2017 at 10:09

    Hi Monks!

    I have a subroutine that essentially stores all of the text from files held in a specific folder into a hash (%corpus). The idea is that you specify the folder when you call the subroutine. This subroutine has been written and used by someone with a lot more coding experience than me, so I can be sure that it is not the problem.

    What I want to do is 1) call the subroutine (getCorpus); 2) specify the folder that I want it to work on; 3) instruct it to store its output into a new hash (%mycorpus), and then I want to print out the contents of this hash.

    Iíve been browsing the PerlMonk archives for any similar situations to see if I could solve the problem myself, and Iíve managed to cobble together the code below from various places (notably Re: easiest way to print the content of a hash?), but I am quite obviously making some big mistakes that I don't have the skills, as of yet, to spot (as evidenced by the various error messages it returns (reproduced below the code)

    I haven't uploaded the original subroutine here because I'm not sure about the etiquette of making someone else's code available without their consent. But essentially the subroutine comes first and then I write:

    %mycorpus = getCorpus("C:\Users\li\test") #line 1 foreach (sort keys %mycorpus) { print "$_ : $mycorpus{$_}\n"; }

    In line 1, I am trying to call the subroutine (named getCorpus), tell getCorpus what folder I want it to operate on, and store the output of the subroutine into a new hash named %mycorpus. Then in lines 2 and 3, I attempt to print the contents of %mycorpus. The error messages returned for this particular attempt are as follows:

    Operator or semicolon missing before %mycorpus at line + 51. Ambiguous use of % resolved as operator % at line 51. Can't modify modulus (%) in scalar assignment at line 52 +, near ") foreach " syntax error at line 52, near ") {" Execution of aborted due to compilation errors.
    Any guidance on this would be greatly appreciated.
Double check for positions
5 direct replies — Read more / Contribute
by bisimen
on Oct 18, 2017 at 08:30

    This sub works, but say this is my DNA: AGCTTCTTGCGCTTCTT and this is what i'm looking for: GCTTCTTGC

    It will return 2. Which it correct, but since the next match blends into the first one, it dosen't detect it.

    So it should return: 2 9

    I guess I need to change the regular expression somehow...

    sub match_positions { my ($regexp, $sequence) = @_; use strict; my @positions = ( ); while ( $sequence =~ m/$regexp/ig ) { push ( @positions, pos($sequence) - length($&) + 1 ); } return "@positions "; }

    Thanks for any answers!

Issues with pp and Tk
6 direct replies — Read more / Contribute
by Chuma
on Oct 18, 2017 at 06:40


    I'm trying to teach myself Tk, by making a little game. Now I'd like to send it to my sister, so my little nephews can play it, but she doesn't have Perl installed, so I'd like to compile it to an executable (we both have macs). I use the pp tool, and it seems to work just fine on little example programs, but for this one I get errors. I worked out most of the problems, but the last error has me at a loss.

    >>> pp -c hopgame SYSTEM ERROR in compiling hopgame: 11 at /System/Library/Perl/Extras/5 +.18/Module/ line 1351. >>> pp hopgame /usr/bin/pp5.18: Failed to extract a parl from 'PAR::StrippedPARL::Sta +tic' to file '/var/folders/y8/p9hddj4d1l7fryf24cz0cd3m0000gn/T/parlpo +XAzmu' at /System/Library/Perl/Extras/5.18/PAR/ line 1152, < +DATA> line 1.

    I guess it's something to do with importing the Tk modules? I don't know what else to do about it. Of course, if there's an easier way to compile to an executable, I'd welcome that too. I have ActivePerl 5.20, Mac OS 10.12.6.

A question a Tree::Trie
1 direct reply — Read more / Contribute
by jhuijsing
on Oct 18, 2017 at 01:08

    I have Tree:Trie created when I do a prefix search I am not getting the response I expect

    here is the trie

    0 Tree::Trie=HASH(0x600d98df8) '_DEEPSEARCH' => 3 '_END' => '' '_FREEZE_END' => 0 '_MAINHASHREF' => HASH(0x600d99050) '+' => HASH(0x600d9cfa8) 6 => HASH(0x600d9d0c8) 1 => HASH(0x600d9d008) '' => 'Term' 1 => HASH(0x600d9d2d8) 4 => HASH(0x600d9d188) 1 => HASH(0x600d9cea0) 1 => HASH(0x600d9d200) '' => 'Outleg_1' 0 => HASH(0x600d9d170) '' => 'Outleg_2' 5 => HASH(0x600d9d458) '' => 'Term_Ref' 'd' => HASH(0x600d9d0b0) 'e' => HASH(0x600d9d260) 'f' => HASH(0x600d98f78) 'a' => HASH(0x600d9d578) 'u' => HASH(0x600d983d8) 'l' => HASH(0x600d9d5a8) 't' => HASH(0x600d9d5d8) '' => 'Unknown'
  • do a search for +6114110
  • x $trie->lookup( "+6114110" )

    0 '+6114110'

  • do a search for +61141102
  • x $trie->lookup( "+61141102" )

    empty array

    I was expecting to get the same result, As it is the best prefix that matches

    Or I have got it wrong?

Best way to validate a string as a color?
5 direct replies — Read more / Contribute
by pra
on Oct 17, 2017 at 11:37

    What is the best way to find out if a given string is a valid color? I am writing a Perl/TK program that will read in a file which contains color names. Users might not be aware of which names are allowed, or could mis-type something.

    It seems to me that I could compare with names from the list which on my Linux machine is in /usr/share/X11/rgb.txt (and add a check for colors of the form #rrggbb, no reason to exclude those). Can I expect that list to be standard on all systems (Linux, Windows, Macs)? Even if it is standard, is that the best way to do it?

    Certainly, if I use an incorrect color name, I get an error message. Could I make use of the verification system being used there, and if so, how?

Issues Fetching URL with a variable token
4 direct replies — Read more / Contribute
by Petroza
on Oct 17, 2017 at 11:34
    Hey there, I'm trying out a script in perl language using Selenium Chrome Driver as engine. It's a fairly simple code but I got to a part that kept blocking my process for 2-3 days now and I can't seem to get around it. The script consists into logging into an online game account and opening the main page (it's pretty simple as I said, after all it's just exercises for me before I get the hand of it).
    use feature ':5.10'; use strict; use warnings; use Selenium::Chrome; use Selenium::Remote::WebElement; my $driver = Selenium::Chrome->new; my $a = 'email'; #write email here my $b = 'password'; #write password here my $login_url = "link/login/"; #link to the website (link is a substi +tute to the actual URL which was considered spam so i just wrote "lin +k") $driver->get( $login_url ); #navigate to the website my $name = $driver->find_element("//input[@name='mailAddress']"); $name->send_keys( $a ); #find and input mail address my $password = $driver->find_element("//input[@name='password']"); $password->send_keys( $b ); #find and input password my $Login_button = $driver->find_element("//input[@value='Login']"); $Login_button->click(); #find and click login button

    So far the code is perfect. Now, after reaching this part the struggles started. After logging in an image appears with a ''My Page'' button. This button leads to the my page menu (duh) where I want my script to end at. The source code for the button is below:

    <a id="enterBtn" class="push-motion0 js-onceLink" href="link/mypage?t=1pSX7e.3KXy.asX.2k42h.1klg0.280f08.H" style="user-select: none; touch-action: pan-y; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></a>

    I've tried several things to get past this but I just can't get to navigate to My page and/or ''click'' the button. The thing is the http URL has a base part


    and a token part


    The token part changes EVERY time you reopen the page so I CANNOT (or I don't know how to) assign the url to a variable like I did above since it gives me an error upon entering the expired link.

    Instead I tried to SEARCH for elements containing the partial link text:

    my $my_page = $driver->find_element("//a[@href='link/mypage?t=]"); my $href = $my_page->get_attribute("href"); $driver->get($href); <>;

    Also tried 'click' instead of 'get' with same result.

    The script stops at the current page without navigating to "My page" menu. The error bellow appears in the .bat that I use to execute the command.

    "Use of uninitialized value in substitution iterator at C:/Perl64/site/lib/Selenium/Remote/ line 437."

    I just can't seem to get past this element and finally open "my page" menu. It has something to do with XPath but I think I'm missing something (simple yet essential).

Puzzle Regex: Letter Frequency Arithmetic Sequence
6 direct replies — Read more / Contribute
by QM
on Oct 17, 2017 at 11:03
    I ran across a blog post about an interesting word characteristic, and wondered if a regex can be written to match this (e.g., with the plan to search a dictionary file). I suspect the answer is no, without invoking the magic "code in a regex".

    Unfortunately, I don't have time now to try my hand at it, but I thought I'd post it here for everyone to have a go.

    Update: Possible puzzles:

    1) Find the longest words where each letter used has a different frequency.
    2) Find the longest words where letter frequencies are sequential (e.g., 3,4,5,6).
    3) Find the longest words where letter frequencies are sequential starting from 1.

    Use any dictionary you like. If you have a mahvelous dictionary, drop a link here.

    Quantum Mechanics: The dreams stuff is made of

copying file from one server to other
3 direct replies — Read more / Contribute
by vickey110
on Oct 17, 2017 at 03:21

    I have a directory named systems which contains two more directories system1 and system2 and each of these directory contains bunch of files. Out of those I need to export only .csv files to my local machine as I am working on Unix in Putty. Using the scp command as

     scp /user/vickey/systems/system1/file1.csv vickeys@localhost:C:/

    but copying each file one by one is taking too long. Could anyone suggest how I can do the same using a Perl script without editing the file name each time.

Add your question
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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (6)
    As of 2017-10-21 03:07 GMT
    Find Nodes?
      Voting Booth?
      My fridge is mostly full of:

      Results (269 votes). Check out past polls.