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
Regex \. help
2 direct replies — Read more / Contribute
by codz67
on Nov 21, 2014 at 18:57
    Oh great and wise monks, I beseech your help. I have this regex: ^regex*\.0-9 It matches regex.1 It must also match regex, but not regex.abc. . It seems as though the ? doesnt make the \. optional. Please help.
Matching Values in an Array
6 direct replies — Read more / Contribute
by Digs27
on Nov 21, 2014 at 13:15

    Hi Monks!

    I am a new programmer and need help with a project I am working on.

    I have two data files formatted as follows:


    Station,Code 1,10 2,11 3,12 4,13 5,14



    Station,S_10,S_11,S_12,S_13,S_14,S_15,T_10,T_11,T_12,T_13,T_14,T_15 1,31,29,29,31,29,29,15,14,23,15,14,23 2,33,28,23,33,28,23,17,15,23,17,15,23 3,23,27,33,23,27,33,18,16,23,18,16,23 4,25,26,28,25,26,28,23,14,15,23,14,15 5,26,26,27,26,26,27,23,18,17,23,18,17 6,27,33,31,27,33,31,14,17,18,14,17,18 7,33,29,29,33,29,29,12,18,23,12,18,23

    Both of the CSV files listed above are dummy datasets, where the actual files i'm trying to use are 1,900(Station) and 48,000(Parameters) lines long.

    My objective is to match station from station.csv to station in Parameters.csv and then use Code from station.csv to match the correct temperature and salinity column from Parameters.csv and return those values to station.csv. For example, in line 1 of station.csv I need it to match station 1 in Parameters.CSV and return values for S_10 and T_10 in that row. Does this make sense? Is this possible in Perl?

    This is the script I have written up to this point. I am at a complete loss on how to continue.

    my @Station = qw(); my @Code = qw(); open(TEXT,"<Station.csv") or die "\n\n\nFile Not Found!\n\n\n"; my @SCAL = <TEXT>; close(TEXT); foreach my $line (@SCAL) { my @dataA = split(',', $line); push (@Code, $dataA[1]."\n"); push (@Station, $dataA[0]."\n"); } open(FILE,"<Parameters.csv") or die "\n\n\nFile Not Found!\n\n\n"; my @FVCO = <FILE>; close (FILE); foreach my $lineB(@FVCO) { my @dataB = split(',', $lineB); if ( $dataB[0] == $Station[0 .. 5]) { #match correct column for salinity and salinity based off the +value for "code" } }

    Please help me if you can!

Cpanm error message - not in range
1 direct reply — Read more / Contribute
by combraxis
on Nov 21, 2014 at 12:38

    I am trying to get to grips with installing modules from Cpan. I have installed Cpan minus - it works well.

    But trying to install Email::Sender,when it attempts to install the dependency MakeMaker it reports an error

    installing the dependencies failed: Installed version(6.55_02)of ExtUtils::MakeMaker is not in range '6.59'
    and it bails out. What can I do about this? Thanks.
PDF Letter format
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 21, 2014 at 11:33
    Hi there Monks!
    I am looking for suggestions on how more efficient this code could be. It creates a standard letter. It works,
    the problem I am having is how to add extra new lines to the main text of the main body on the letter, like between “Sincerely” as an example.
    I am using "\n" new lines to set where to end the lines, it might not
    be the right way of doing it. Anyways, any suggestions!
    Here is the code I am using:
    #!/usr/bin/perl use strict; use warnings; use PDF::API2; my $font_size=10; my $pdf = PDF::API2->new(-file => "test.pdf"); $pdf->mediabox('A4'); my $page = $pdf->page; my $fnt = $pdf->corefont('Helvetica',-encode => 'latin1'); my $boldfont=$pdf->corefont('Helvetica-Bold',-encode => 'latin1'); my $txt = $page->text(); my $gfx=$page->gfx; $txt->textstart; $txt->font($boldfont,$font_size); # 1X moves right and left - #1Y more moves up and down $txt->translate(380,740); $txt->text("November 21, 2014"); my $image=$pdf->image_jpeg('logo.jpg'); $gfx->image( $image, 20, 770 ); $txt->font($fnt,9); $txt->translate(20,740); $txt->text("I am here"); $txt->font($fnt,9); $txt->translate(20,728); $txt->text("P.O.Box 1234"); $txt->translate(20,717); $txt->text("Earth, ET. 1234-1234"); $txt->translate(20,705); $txt->text("(000) 000-0000"); $txt->font($boldfont,$font_size); $txt->translate(20,650); $txt->text("John The III"); $txt->translate(20,636); $txt->text("A Company Somewhere"); $txt->translate(20,621); $txt->text("12345 Main St. Ste 001"); $txt->translate(20,606); $txt->text("Saturn, ET. 00000"); $txt->font($fnt,10); $txt->translate(20,550); $txt->text("Dear Mr. ET III"); # Main content settings $txt->font($fnt, $font_size); $txt->translate(100,800); $txt->font($fnt, $font_size); # Main text content position on the page my $y=523; my $summary = " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac + libero dui. Sed massa quam, ultricies in faucibus nec, volutpat sed velit. Donec aliquet, elit si +t amet hendrerit eleifend, augue augue volutpat enim, quis posuere neque arcu eget nisi. Vestibu +lum ex erat, semper vitae blandit ut, pulvinar mattis lorem. Sed urnaest, ullamcorper quis feug +iat sed, venenatis in lorem. Donec facilisis dolor nibh, sit amet lacinia est viverra vel. Praesent in leo id tellus sagittis luctus. Maecenas quis ante mollis, viverra eros nec, luctus ante. Duis eget sa +pien rutrum, euismod velit et, mattis massa. Aeneanegestas et diam et venenatis. Praesent erat neque +, lacinia vulputate ornare quis, accumsan nec metus. Fusce sollicitudinid risus vulputate aliquam. Cum + sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Interdumet malesuada f +ames ac ante ipsum primis in faucibus. Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +, Suspendisse ultricies, nisi ac lobortis posuere, lacus odio porta eros +. Sincerely, Mr. Joe Doe Manager Services Manager ET "; my @all_lines = split/\n+/, $summary; foreach my $rows (@all_lines){ #my $string=join(" ",$rows)."\n"; $txt->translate(20,$y); $txt->text("$rows"); $y-=20; } # Bottom Data $txt->font($boldfont,10); $txt->translate(20,70); $txt->text("Code Number:00007"); $txt->textend; $pdf->save; $pdf->end( );
    Thanks for looking!
sshpass perl
3 direct replies — Read more / Contribute
by nik786
on Nov 21, 2014 at 08:40

    can u convert above mentioned shell script in to perl script??
Getopt::Long defaults for options with multiple values
3 direct replies — Read more / Contribute
by PetaMem
on Nov 21, 2014 at 07:23

    Reviewing some middle-aged code, I stumbled across this topic...

    Unfortunately, the G:L documentation is silent about default values for options with multiple values: Getopt::Long#Options-with-multiple-values. Even more unfortunate seems an inconsistency compared to defaults for options with single values and maybe a semantic inconsistency at all. If you have a single value option, you may define a default like:

    my $tag = 'foo'; # option variable with default value GetOptions ('tag=s' => \$tag);

    This works as expected. Good. You can - of course - do a similar thing for options that take multiple values:

    my $listref = ['a','b','c']; # option variable with default valu +es GetOptions ('list=s{,}' => $listref);

    If you omit the -list option, the program will have the default value, which is good. If you, however, will give a list option, G:L seems to push that option to the list already given in default, which may have its applications, but is not that great as default behavior. If you want to actually replace the default given, you would have to define defaults the ugly, backward and programmatically DIY way:

    my $listref = []; # no default GetOptions ('list=s{,}' => $listref); my $listref = @{$listref} ? $listref : ['a', 'b', 'c']; # DIY default

    Which is actually code I see right now. Yuck! That can't be right - can it?

        All Perl:   MT, NLP, NLU

How to find received mail id in outlook
1 direct reply — Read more / Contribute
by karthik.raju
on Nov 21, 2014 at 07:02
    Hi i'm using the following to read the mail subject and receiver mail id, my $text = $message->Subject(); my $from_User = $message->From(); my $from_User1 = $message->SenderName(); for subject i'm not getting any issue, but for From() i'm not getting mail id(xxxxx@sss.com) i'm getting only username. Please help on this. Thanks Karthik
Code review: function to trim whitespace from all items in a list
5 direct replies — Read more / Contribute
by eyepopslikeamosquito
on Nov 21, 2014 at 04:30

    At work we have a simple rule for all our Perl code: it must pass our automated in-house static analysis check -- which includes a Perl::Critic check. No ifs, no buts, it must pass before you are allowed to check it in.

    To allow for special cases, and Perl::Critic bugs, we do allow you to switch off specific Perl::Critic warnings, but only in the smallest scope possible.

    Though this simple blanket rule has worked very well IMHO over the past few years, today a workmate and good friend of mine, Misha, was agitated because his lovingly-crafted function was rejected by our automated Perl::Critic check.

    After I showed him how to silence the Perl::Critic check (as shown below) and get his code checked in, I wondered what the Perl Monks think of his coding style. Do you -- like Perl::Critic -- object to Misha's modification of the list items in the grep block below? How would you write the trim_nonempties function below?

    use strict; use warnings; use Data::Dumper; # Returns non-empty strings with leading and trailing spaces removed. sub trim_nonempties { ## no critic (ProhibitMutatingListFunctions) grep { !/^\s*$/ && s/^\s*|\s*$//g } @_; ## use critic } my @x = ( ' hello ', "\thello again\t", '', " ", " \t", 'jock', "\t ab +c", "def \t " ); print Dumper(\@x); my @y = trim_nonempties(@x); print Dumper(\@y);
    To clarify the intent of this little function, the output produced by running the above program is shown below:
    $VAR1 = [ ' hello ', ' hello again ', '', ' ', ' ', 'jock', ' abc', 'def ' ]; $VAR1 = [ 'hello', 'hello again', 'jock', 'abc', 'def' ];
    As you can see, it has trimmed leading and trailing whitepace from non-empty items in the list, while removing any white-space-only items.

Net::SMTP::SSL connect failure
1 direct reply — Read more / Contribute
by linxdev
on Nov 20, 2014 at 17:52

    After and upgrade of perl sending mail via my mail relay is failing. I suspect this has to do with an invalid certification because my test program works fine for sending to gmail.

    Here is my strace output:

    fcntl64(3, F_SETFD, FD_CLOEXEC) = 0 connect(3, {sa_family=AF_INET, sin_port=htons(465), sin_addr=inet_addr +("")}, 16) = 0 fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR) fcntl64(3, F_SETFL, O_RDWR|O_NONBLOCK) = 0 time(NULL) = 1416523433 time(NULL) = 1416523433 time(NULL) = 1416523433 open("/dev/urandom", O_RDONLY|O_NOCTTY|O_NONBLOCK) = 4 fstat64(4, {st_mode=S_IFCHR|0444, st_rdev=makedev(1, 9), ...}) = 0 poll([{fd=4, events=POLLIN}], 1, 10) = 1 ([{fd=4, revents=POLLIN}]) read(4, "u6\323\253L`x\256\317\234\240\243\36\325c\227\r\240\336DD\216 +{D@j\302c\372\rv\2"..., 48) = 48 close(4) = 0 getuid32() = 500 time(NULL) = 1416523433 write(3, "\26\3\1\0;\1\0\0007\3\1Tnn\251\274\252V\253\212\314\26:\306Q +0*\327\214\213\210\367"..., 64) = 64 read(3, 0x8edbc30, 7) = -1 EAGAIN (Resource temporar +ily unavailable) select(8, [3], NULL, NULL, {120, 0}) = 1 (in [3], left {119, 912250 +}) time(NULL) = 1416523433 time(NULL) = 1416523433 read(3, "\26\3\1\0J\2\0", 7) = 7 time(NULL) = 1416523433 time(NULL) = 1416523433 read(3, "\0F\3\1Tnn\246\350wT#\224\230\314\324\320\333^BP\17\314\225\3 +40\315D\304\346\352O\375"..., 72) = 72 read(3, "\26\3\1\3\341", 5) = 5 read(3, "\v\0\3\335\0\3\332\0\3\3270\202\3\3230\202\3<\240\3\2\1\2\2\1 +\0000\r\6\t*\206"..., 993) = 993 write(3, "\25\3\1\0\2\0020", 7) = 7 fcntl64(3, F_GETFL) = 0x802 (flags O_RDWR|O_NONBLO +CK) rt_sigaction(SIGPIPE, NULL, {SIG_DFL, [], SA_RESTORER|SA_INTERRUPT|SA_ +NODEFER|0x34302d0, 0x8ad36e000000000}, 8) = 0 rt_sigprocmask(SIG_BLOCK, [PIPE], [], 8) = 0 rt_sigaction(SIGPIPE, {0x1, [], SA_RESTORER|SA_RESTART|SA_INTERRUPT|SA +_NODEFER|SA_RESETHAND|SA_SIGINFO|0x3726080, (nil)}, {SIG_DFL, ~[HUP Q +UIT ILL TRAP FPE SEGV ALRM TERM STKFLT CONT STOP TSTP TTIN XFSZ VTALR +M PROF IO PWR RTMIN RT_1 RT_2 RT_4 RT_6 RT_10 RT_11 RT_12 RT_14 RT_16 + RT_17 RT_21 RT_25 RT_26 RT_27 RT_29 RT_30], SA_RESTORER|SA_INTERRUPT +|SA_NODEFER|0x34302d0, 0x100000000}, 8) = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 rt_sigprocmask(SIG_BLOCK, [PIPE], [], 8) = 0 rt_sigaction(SIGPIPE, {SIG_DFL, [], SA_STACK|0xac5c70}, {0x1, [RT_4 RT +_5 RT_6 RT_7 RT_8 RT_9 RT_11 RT_12 RT_14 RT_16 RT_18 RT_19 RT_23 RT_2 +4 RT_25 RT_26 RT_27 RT_28 RT_29 RT_30 RT_31], SA_RESTORER|SA_RESTART| +SA_INTERRUPT|SA_NODEFER|SA_RESETHAND|SA_SIGINFO|0x3726080, (nil)}, 8) + = 0 rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0 close(3) = 0 write(2, "ERROR: Died at ./invite.pl line "..., 37ERROR: Died at ./inv +ite.pl line 10.

    I am using:

    my $s = Net::SMTP::SSL->new('', Port => 465, Debug=>1) or + die "$!";

    I never get far enough to see a failure so I had to use strace.

    Is this a certificate issue?

HoA, grep or exists?
3 direct replies — Read more / Contribute
by teamassociated
on Nov 20, 2014 at 15:19
    How do I determine for a certain key I have certain values in the anonymous array? I was to determine if vhost5 contains l02duapofqa and not something else. Or I want to determine is l02duapofqa is owned by vhost5. Data structure is:
    __DATA__ 'vhost5' => [ 'l02duapofqa_r1', 'l02duapofqa1_d1' ] __CODE__ use strict; use warnings; open (LSMAP, "-|", "/usr/ios/cli/ioscli lsmap -all") or die $!; while ($lsm = (<LSMAP>)) { if ($lsm =~ /^Backing device\s+(\w+)\.\w+$/i) { push @{$vhosts{$host}}, $1; next; } my ($p1, $p2, $p3) = split /\s+/, $lsm; $host = $p1 if defined $p3 && $p1 =~ /^(vhost.*)$/i; } $a="vhost11"; $b="l02duapofqa2_d1"; #use Data::Dumper; print Dumper \%vhosts; OUTER: for my $vhname (sort keys %vhosts) { for my $luname ( @{$vhosts{$a}} ) { grep /$b.*/, @{$vhosts{$luname}}; print "\n$vhname -> $luname\n"; if ( !grep /$a.*/, @{$vhosts{$b}} ) { print "\nincorrect vhost\n"; die; } else { print @{$vhosts{$a}},"\ncorrect\n"; last OUTER; } } }
    thank you!

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
  • Outside of code tags, you may need to use entities for some characters:
            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.