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

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
inverting keys in a 2-D Hash
3 direct replies — Read more / Contribute
by melmoth
on Aug 30, 2015 at 09:32
    Hi I have a 2D hash of arrays like: $HASH{keyA}{keyB} = 1,2,3,4; and want to turn it into this: $HASH{keyB}{keyA} = 1,2,3,4; and was wondering what the cleanest way to this is. thnaks!
exit value in tar command
4 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 30, 2015 at 03:06
    Hi perl monks,

    In a perl script i am calling a ksh file to copy files. (ie.) inside ksh file i am running the below tar command to copy the files from source to destination.

    My task is, Need to create a 'success.ok' file, if tar command got succeed, else create 'failure.ok'. The issue is, while running ksh file through perl script, by default i am getting the exist value as '0' for both failure and success scenario..

    KIndly please suggest how to solve this.

    (tar cvf - * | cd /var/tmp/dump; tar xcf - ); output=\$\?; if \$output -eq 0; then touch /var/tmp/dump/success.ok; );

how to change this code into perl
2 direct replies — Read more / Contribute
by perlnewbie012215
on Aug 30, 2015 at 02:44

    awk 'NR==FNR{a$1++;next;}{ if (a$1 > 1)print;}' imageoutcome.txt imageoutcome.txt

    this code prints duplicate rows basing on the first column, can you guys help me write in perl

MD5::Digest addfile() w/ tied filehandle
1 direct reply — Read more / Contribute
by jdv
on Aug 29, 2015 at 23:24

    I'm observing an odd interaction of my module with MD5::Digest that I haven't been able to figure out.

    I'm implementing a read/write interface to a gzip variant using tied filehandles, with full seek/read/readline/tell support. I'm in the testing phase and nearly all of the tests I'm throwing at it seem to be working (basically performing exactly the same combinations of seek, read, <>, etc, on both my tied filehandle object (with the compressed file loaded) and a regular Perl filehandle opened on the uncompressed version, and comparing the output).

    The one exception at this point is when I try to provide my tied filehandle to MD5::Digest's addfile() method. This doesn't work:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::MD5->new()->addfile($fh)->hexdigest; print $hex, "\n"; # prints d41d8cd98f00b204e9800998ecf8427e

    The test returns almost immediately and it appears the hash returned is that of an empty string, so clearly the file is not actually being read. However, this works as expected:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $d = Digest::MD5->new(); my $buf = ''; $d->add($buf) while ( read $fh, $buf, 4096 ); my $hex = $d->hexdigest; print $hex, "\n"; # prints the expected sum

    It also works fine if I run the original code but substitute the pure-Perl module (although painfully slowly):

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::Perl::MD5->new()->addfile($fh)->hexdigest; print $hex, "\n"; # prints the expected sum

    I additionally tried it with Digest::SHA, and that works fine too:

    my $fh = B2B::BGZF::Reader->new_filehandle( $fn_bgzf ); my $hex = Digest::SHA->new(1)->addfile($fh)->hexdigest; print $hex, "\n"; # prints the expected sum

    Basically, I have only been able to observe the issue when using my module with the XS implementation of Digest::MD5. Debugging is difficult because I'm not sure what code is actually being called (apparently not the addfile() method of Digest::base or any other actual perl code I can find on my system). I have no problem just using the explicit read()/add() form with Digest::MD5, but if this is an indication of a subtle bug in my code I'd like to work it out - I'm just not sure how to do so.

    Any help with understanding what Digest::MD5::addfile() is actually calling under the hood or what might be going on here would be greatly appreciated.

regex match counter
1 direct reply — Read more / Contribute
by OldChamp
on Aug 29, 2015 at 16:43

    How can I get a match counter and use it as a prefix in the peplace term. For example, I have several times "position" in my text and want to peplace it with "position 1", "position 2" and so on. Many thanks in advance!

Can't close pipe to invalid process
2 direct replies — Read more / Contribute
by cLive ;-)
on Aug 28, 2015 at 17:37
    I was debugging why this test to create a PDF worked in the shell, but not from Jenkins (both environments running as the same user):
    my $fh; ok( open($fh, "|-", "prince - 2>/dev/null $test_file"), "Open +$test_file for writing" ); print $fh "<html><body><h1>Hello, world!</h1></body></html>"; ok( close($fh), "Close $test_file");

    Turns out it was because the PATH didn't exist in the Jenkins environment.

    Problem solved, but now I'm left scratching my head.

    ok 3 - Open /opt/humana/svn/checkouts/rosalind-repo/prod/tmp/testfile. +pdf for writing not ok 4 - Close /opt/humana/svn/checkouts/rosalind-repo/prod/tmp/test +file.pdf

    Why would the open succeed to an unknown executable? Is it because it dosen't fail until you attempt to use it? For some reason that feels a little off to me. What am I missing?

apache 2.4 mod_perl permission denied on standard modules
1 direct reply — Read more / Contribute
by Crackers2
on Aug 28, 2015 at 14:20

    I'm running on SLES12, with apache 2.4.10 and mod_perl 2.0.8

    In a vhost config file, I have a "PerlFixupHandler MyHandler" directive. is in /etc/apache2/script

    If I do a perl -e'use MyHandler' from the commandline, everything works fine. But when it gets used from the server, it fails and is logging this in the error_log:

    [Fri Aug 28 18:15:43.053198 2015] [perl:error] [pid 22133] [client x.x +.x.x:x] failed to resolve handler `MyHandler': Can't locate Digest/SH Permission denied at /etc/apache2/script/ line 7. +\nBEGIN failed--compilation aborted at /etc/apache2/script/MyHandler. +pm line 7.\nCompilation failed in require at (eval 5) line 2.\n, refe +rer: https://x.x.x.x/

    If I comment out the reference to Digest::SHA, it just fails on the next module.

    For debugging, I added a BEGIN block to dump INC and to try and read the file, and that does not appear to cause any errors.

    This configuration used to work fine on apache 2.2 ; I did some minimal updates to the configuration files for the new authentication stuff in 2.4, but nothing related to perl.

    Any idea on what's wrong and how to fix it?

    Edit: Here's the start of the .pm in question:

    package MyHandler; use strict; use warnings; use Apache2::Const qw(:common); use Digest::SHA qw(sha256_hex sha1_hex); use OtherModule; use Apache2::RequestRec; use Apache2::RequestIO; use Apache2::Const; use Apache2::Log; use APR::Table; use CGI::Cookie; use Apache2::URI (); use APR::URI; use APR::Pool ();
Given month last day
4 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 28, 2015 at 14:18
    Hi Monks!
    Can I pass a given month to Time::Piece and get its last day?
    #!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Seconds qw(ONE_DAY ONE_WEEK ONE_MONTH ONE_YEAR); # Create Time::Piece New Object for the dates my $t = Time::Piece->new(); my $year = $t->year; # Pass November, but it can be any month my $month = "11"; my $last_day_month = $t->month_last_day($month); #?? print $last_day_month." days\n"; # 30
Replicating perl instance on AIX 6.1
3 direct replies — Read more / Contribute
by twieckow
on Aug 28, 2015 at 13:59
    Hello , What I would like to do, is create a "duplicate" perl instance from the OS installed system perl . This would be 5.8.8. This is in relation to an early post I had made (#1135687). My perl users have been using the OS install and I now want to move them off of it to their own. So that my OS upgrades do not effect their current perl. I can easily copy/create a new directory structure for them ( /usr/opt/perl5/* -> /segregated). But not sure how best to alter the @INC declaration or any other pieces that may need pointed to this area. In looking at perlbrew, I'm not quite sure if this will do it (not installing different, but replicated). Nor do I know/think autobundle is what I need , or if autobundle is overkill. I know that future module adds would require me to add Prefix= to the "perl Makefile.PL " step.Would appreciate any wisdom/suggestions you may have on this. Thanks very much in advance.
email exchange
1 direct reply — Read more / Contribute
by Sandy
on Aug 28, 2015 at 11:48
    Hello all

    I am trying to access an exchange server (over the web) via Email::Folder::Exchange

    While running the sample code, I run into the following error:

    Can't call method "attr" on an undefined value at Email/Folder/Exchange/ line 123.

    After a little investigation, it comes down to:

    my code

    use Email::Folder::Exchange::EWS; my $folder = Email::Folder::Exchange::EWS->new('my_server', $user,$p +ass);
    Looking at the file, line 123 is:
    $self->folder_id( $som->dataof('//FolderId')->attr->{Id} );
    I inspected the $som object, and looked at some of the stuff in the code, and I think I can see where there might be an error, (and it is not with my credentials) but I am hesitant to start debugging all of this, when the point is to have a package that just works.

    So, the question is: Is there an easy way to access an exchange server, via the web, with a perl program. What would be the best package?



    Making a bit of progress... but can't seem to find the right credentials to log in.

    Have established that the server is NOT

    as that is for the web-client not the soap client.

    Am able to login (via a browser) to https::// with my standard username password, but does not work when I am using this from a perl script.

    Still stymied.

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!
  • 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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (8)
    As of 2015-08-30 15:05 GMT
    Find Nodes?
      Voting Booth?

      The oldest computer book still on my shelves (or on my digital media) is ...

      Results (349 votes), past polls