http://www.perlmonks.org?node_id=479

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
Creating a bin or script directory with h2xs
1 direct reply — Read more / Contribute
by rpaskudniak
on Feb 21, 2019 at 21:42
    (Edited about 45 minutes after initial submission)

    Greetings.

    It has been a long time since I had time on my hands to pursue more knowledge. Unfortunately, I have that time now. :-( That aside...

    Some years ago I posted a module to CPAN, along with a command-line utility that would use that module. As per Sam Tregar's sound advice I started with h2xs and created the usual hierarchy. But it did not include a directory for script files that are not part of the module. So here is the directory structure with incriminating names changed.

    drwxr-xr-x+ 1 RPaskudniak None 0 Nov 21 2013 blib drwxr-xr-x+ 1 RPaskudniak None 0 Nov 20 2013 lib -rw-r--r-- 1 RPaskudniak None 24074 Dec 11 2013 Makefile -rwxr-xr-x 1 RPaskudniak None 1297 Mar 3 2011 Makefile.PL -rwxr-xr-x 1 RPaskudniak None 172 Nov 20 2013 MANIFEST -rw-r--r-- 1 RPaskudniak None 568 Nov 20 2013 META.yml -rw-r--r-- 1 RPaskudniak None 500 Dec 11 2013 MYMETA.yml -rw-r--r-- 1 RPaskudniak None 0 Dec 11 2013 pm_to_blib -rwxr-xr-x 1 RPaskudniak None 15000 May 6 2012 README drwxr-xr-x+ 1 RPaskudniak None 0 Feb 5 2014 scripts <<<***>>> drwxr-xr-x+ 1 RPaskudniak None 0 Nov 20 2013 t

    Note that scripts directory: h2xs did not create that; I manually created that. And somehow, when I ran the final make install, the utility program ended up in the right place, /usr/local/bin.

    I have no recollection of how I got that to work. I likely asked someone but I just don't recall what I did right. I don't think I had to mess with the Makefile.

    So my bottom line question is:

    Is there an option to h2xs that makes allowances for a bin or scripts directory whence to place a stub for a Perl program related to the module? Or lacking such an option, is there an alternative to h2xs that does make such an allowance?

    This humble memory challenged peasant begs a memory kick-start to get restarted; I'm planning a module that will need at least 5 utilities that I can think of.

    This humble peasant (with some grand delusions) thanks y'all.

Confused by a couple of idioms
3 direct replies — Read more / Contribute
by nysus
on Feb 21, 2019 at 17:07

    Being Perlmonk's most clueless Vicar, I continually find new idioms that leave me baffled. I'm looking though this code. On line 33, I see:

    "o|c|out=s" => \my $csv

    What is the slash doing there before my?

    And on line 55:

    $csv and $xls = $csv;

    I've stared at this for a good 2 minutes and can't figure it out. I guess that's what keeps Perl so interesting. Thanks to the monks for your patience with me after all these years. :)

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

Using Cypher::CBC to encrypt fields in a file - Need Monk Help
2 direct replies — Read more / Contribute
by perlc1ph3r
on Feb 21, 2019 at 12:06

    I am running up against a wall and hitting it hard. I am needing to encrypt fields and not he entire file for data upload. I then need to use that encrypted field as a key in a system and then when extracted, I need to be able to decypt it. Below are sample lines from a fake text creator to illustrate my quandary."

    customerid registered_date first_name last_name address city state zip age primary_email
    689-08-2317 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 surian0@archive.org
    201-25-0510 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 gimpy1@edublogs.org
    616-54-5114 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 cgunther2@nature.com

    I would need to read in the file, encrypt column 1 & 9, then output the file in the same format but with those values having encrypted values. I may be going down the wrong path here and really need a hash process but I have exhausted my working brain cells at this point and need some help. The end format would look like this below.

    customerid registered_date first_name last_name address city state zip age primary_email
    $#diDse*eES 1518973472 Sascha Urian 85 Sunbrook Road San Antonio Texas 78250 55 weSIDUsae34@#$%%#@@
    eSDE#24DSET 1515436776 Gilbertine Impy 3136 Thierer Trail Mc Keesport Pennsylvania 15134 49 sdies#@SERUSEeset
    OEDUse#@#$2 1518645090 Christabella Gunther 4 Autumn Leaf Junction Chicago Illinois 60619 26 mvxosef@#w32553S

    I have been trying to modify this base code

    #!/usr/bin/perl use strict; use Crypt::CBC; #unless (scalar @ARGV == 3) { # #die "Usage: $0 encrypt|decrypt|en|de \$mysecretkey \$file_to_den +crypt"; #} #my $type = shift @ARGV; #my $key = shift @ARGV; #my $file = shift @ARGV; my $type = "de"; my $key = "12345"; my $file = "C:\\perlinputfiletest\\fakedata.txt.encrypt"; die "The first ARGV should be one of de, en, encrypt, decrypt" if ($ty +pe !~ /^(en|de)(crypt)?$/); die "the file $file is not existence" unless (-f $file); my $DEBUG = 1; print "type is $type, key is $key, file is $file\n" if $DEBUG; my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish' ); local $/; open(FH, $file) or die $!; flock(FH, 2); my $data = <FH>; close(FH); my ($save_data, $save_file); if ($type =~ /^en(crypt)?$/) { $save_data = $cipher->encrypt($data); $save_file = $file . '.encrypt'; } else { $save_data = $cipher->decrypt($data); $save_file = $file . '.decrypt'; } open(FH, '>', $save_file) or die $!; print FH $save_data; close(FH); if (-e $save_file) { print "$type file $file to $save_file OK\n"; } else { print "failed without reason\n"; }

What is L?
5 direct replies — Read more / Contribute
by RichardJActon
on Feb 21, 2019 at 08:31
    I've been reading the File::Temp documentation and there are a number of places in the text where something called 'L' is alluded to, it appears several times in the see also: (https://perldoc.perl.org/5.8.8/File/Temp.html#SEE-ALSO) does anyone know what this mysterious 'L' is, and assuming this is not some kind of bug, where I can find out more about it? as my google-foo is coming up blank with nothing but Perl and the letter L to work with.
    What is true is already so. Owning up to it doesn't make it worse. - Eugene Gendlin
Performing addition on hex value extracted from a string
3 direct replies — Read more / Contribute
by syedasadali95
on Feb 21, 2019 at 05:36

    I am a new monk and struggling with a piece of work. I want to extract data from a string, do addition operation on that data and then substitute the data in the same string. Here is the string I am working on:

    chn:req mon:orig cmd:SDP_CMD_WRSIZEDFULL tag:0x3b9aca01 addr:0xdf7780 qospri:0 len:0xf

    I want to extract the hexadecimal addr:0xdf7780 value, add 0x500000000 to it and then replace the result in the same string. Here is the piece of code I am using. I am able to print the extracted address values but not able to perform addition to it.

    ------------------------------------ #!usr/bin/perl unlink("ccix.sdp_trc"); $num = 0x500000000; open (IN_FILE, "$ARGV[0]") or die "Please provide an input file\n"; open (OUT_FILE, ">>ccix.sdp_trc"); while (my $line1 = <IN_FILE>) { if( ($line1 =~ /addr/) ) { $line1 =~ /addr:([a-z0-9-]+)\s+/; print "%$1\n"; my $one = ($num + $1); $line1 =~ s/addr\:0x.*qospri/addr\:$one qospri/; printf OUT_FILE ("$line1"); } else { printf OUT_FILE ("$line1"); } } ----------------------------------------------------
    Any help is appreciated!
A little golfing challenge: Replacing letters with numbers
4 direct replies — Read more / Contribute
by haukex
on Feb 21, 2019 at 03:48

    In this StackOverflow question, user "Learner" asked how to replace a set of certain letters in the first column of a file with numbers. In other words, given this set of replacements:

    A=>1, B=>5, C=>6, D=>4, E=>7, F=>16, G=>10, H=>11, I=>12, K=>14, L=>13, M=>15, N=>3, P=>17, Q=>8, R=>2, S=>18, T=>19, V=>22, W=>20, Y=>21, Z=>9

    And this input:

    NDDDDTSVCLGTRQCSWFAGCTNRTWNSSA 0 VCLGTRQCSWFAGCTNRTWNSSAVPLIGLP 0 LTWSGNDTCLYSCQNQTKGLLYQLFRNLFC 0 CQNQTKGLLYQLFRNLFCSYGLTEAHGKWR 0 ITNDKGHDGHRTPTWWLTGSNLTLSVNNSG 0 GHRTPTWWLTGSNLTLSVNNSGLFFLCGNG 0 FLCGNGVYKGFPPKWSGRCGLGYLVPSLTR 0 KGFPPKWSGRCGLGYLVPSLTRYLTLNASQ 0 QSVCMECQGHGERISPKDRCKSCNGRKIVR 1

    The expected output is:

    3 4 4 4 4 19 18 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 +18 1 22 6 13 10 19 2 8 6 18 20 16 1 10 6 19 3 2 19 20 3 18 18 1 22 17 13 12 + 10 13 17 13 19 20 18 10 3 4 19 6 13 21 18 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 + 13 16 6 6 8 3 8 19 14 10 13 13 21 8 13 16 2 3 13 16 6 18 21 10 13 19 7 1 11 10 + 14 20 2 12 19 3 4 14 10 11 4 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 +22 3 3 18 10 10 11 2 19 17 19 20 20 13 19 10 18 3 13 19 13 18 22 3 3 18 10 13 16 16 + 13 6 10 3 10 16 13 6 10 3 10 22 21 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 + 17 18 13 19 2 14 10 16 17 17 14 20 18 10 2 6 10 13 10 21 13 22 17 18 13 19 2 21 13 1 +9 13 3 1 18 8 8 18 22 6 15 7 6 8 10 11 10 7 2 12 18 17 14 4 2 6 14 18 6 3 10 2 14 12 + 22 2

    Here are my two solutions:

    $ perl '-M5;%h=map{$_,++$i}split//,"ARNDBCEQZGHILKMFPSTWYV"' -alpe ' ($_=$F[0])=~s/[A-Z]/$h{$&} /g' $ perl -alpe ' ($_=$F[0])=~s/[A-Z]/(index("ARNDBCEQZGHILKMFPSTWYV",$&)+1)." "/ge'

    WebPerl link

    I personally consider the extra space character at the end of the line produced by my solutions acceptable (I think diff -b is probably ok too). Unfortunately the OP didn't specify what would happen in case the input strings contained letters that aren't in the set, so I guess "bonus points" for solutions that only affect [A-IK-NP-TV-WY-Z] instead of [A-Z] like my solution does. Bonus question: Can anyone come up with a short, preferably pure Perl, solution to produce such a regex character set for any given list of letters?

    $ echo "ARNDBCEQZGHILKMFPSTWYV" | perl -MSet::IntSpan -ple ' $_=Set::IntSpan->new([map{ord}split//,$_])->run_list; s/\d+/chr$&/eg;s/,//g;$_="[$_]"'

    Have at it ;-)

Entire Results from R
2 direct replies — Read more / Contribute
by msanchez78
on Feb 20, 2019 at 10:18
    seeking the wisdom of the perl monks ...

    within R i submit this code:

    > data <- c("2016-03-15 13","2016-03-16 23","2016-03-17 06","2016-03-1 +8 15","2016-03-19 08","2016-03-20 21") > datevec <- strptime(data,"%Y-%m-%d %H") > difftime(datevec[-length(datevec)],datevec[-1],units="hours")
    which correctly produces this output:
    Time differences in hours
    1 -34 -7 -33 -17 -37

    notice the two lines. now, in perl i have this code:
    $R->set( 'data', "2016-03-15 13","2016-03-16 23","2016-03-17 06","2016 +-03-18 15","2016-03-19 08","2016-03-20 21" ); $R->run( q`datevec <- strptime(data,"%Y-%m-%d %H")` ); $R->run( q`xx <- difftime(datevec[-length(datevec)],datevec[-1],units= +"hours")` ); @Rres = $R->get('xx'); print Dumper(\@Rres);
    which correctly produces this result:
    $VAR1 = [ [ 'Time', 'difference', 'of', 'hours' ] ];

    the problem is that no matter what i do i cannot get to the second output line from R ... the line 1 -34 -7 -33 -17 -37

    any suggestion on how i can get to the second (and in many cases n-number) of subsequent output lines from R? thanks!
How can I match this }\n} in perl
2 direct replies — Read more / Contribute
by nkg
on Feb 20, 2019 at 05:01

    Hi Perl Monks,

    i need to match "}\n}" (without quotes and newline as in real).description of matching characters is below. How can I do it?

    }

    }

DBI select IN Array
4 direct replies — Read more / Contribute
by Anonymous Monk
on Feb 20, 2019 at 04:11

    Dear monks

    What is wrong in the following statement? (Trying to query a database using IN and a array with my query values)

    my @tagList=('red','black'); my $ID = $dbh->selectall_arrayref("SELECT table FROM tags WHERE ta +g IN (join(', ', ('?') x @tagList))");
perlbug - where is my bugreport
1 direct reply — Read more / Contribute
by Skeeve
on Feb 20, 2019 at 02:14

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.