Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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
To each() their own
2 direct replies — Read more / Contribute
by Ancient.Wizard
on Apr 30, 2017 at 11:13

    I am attempting to understand why Perl may be incapable of *always* printing the file and line number like the following example. In this simple example and in every Perl .pm or .pl I write seems to work as expected by burping a warning when the condition is detected.

    Use of each() on hash after insertion without resetting hash iterator results in undefined behavior, Perl interpreter: 0xed8cd8 at ./ line 21.

    However, an application I'm coding at work currently has no usage of each(); however while the application runs I randomly received the message above *BUT* without a filename or line number. Figuring Perl wont burp the message unless its real I've combed through the source and found no usage of each(), which is as expected, as I'm not a fond user of it. What I get looks like this:

    Use of each() on hash after insertion without resetting hash iterator results in undefined behavior, Perl interpreter: 0x<hex-blah>.

    A little back ground: The application runs on Linux, specifically it runs on RH6.7 and uses the Perl RH provides, perl-5.10.1 with various patches RH added via RPM. This version of Perl does not complain about this issue. I've been forced to try and use a newer Perl because the Perl 5.10.1 contains bugs that causes it to received a SIGSEGV (11) because given enough time it will corrupt its heap or something. I attempted to look under Perl's hood by using RH's debug info packages etc and examining core files. It was not useful and the location it pooped out was always random.

    I wont bother making a full listing but I've built the latest of every 5.<even>.<latest> release from 5.14.4 and ran regression tests against the application. All of these versions I believe complain about the usage of each(). I wanted to use 5.24.1, thinking I could get support for any issues found however it has a serious memory leak in it. BTW 5.25.<latest> does not the large memory leak. Well I get off topic. Prior to 5.24 version seem to be memory leak free.

    I guess the real question is how to determine where this each() warning is coming from? Without a file and line number I'm suspecting this each() is embedded into some XS code that is also compiled Perl. Yes that's rather a huge guess. I'm assuming that if the each() was in my code which is all pure Perl OR in any number of the CORE/CPAN modules being used would include the filename and the line number if it was loaded and compiled at run time. Is this possible and how can I discover where this is happening? It seems I might need to start digging under the hood again and have a Perl with debugging enabled. That sounds real ugly.

    The following is not useful except for causing Perl to burp a lot

    #!/usr/bin/env perl use strict; use warnings; EXAMPLE: { my $happy_camper = { Rambler => 'The Yellow Rose of Texas', Wagon => 'There may be flies on them there guys but there ar +e no flies on us', Caskit => 'The Rain in Spain goes mainly down the drain', Skate => 'The Quick Brown Fox Jumped Over The Lazy Dogs Back +', Lemon => 'Love the smell of napalm in the morning' }; my $newkeys = 'KEYME000'; foreach ( 1 .. 900000 ) { while ( my ( $_key , $_val ) = each % $happy_camper ) { if ( int(rand 300000) == 1 ) { # printf STDERR "# Adding new item\n"; $happy_camper->{ ++$newkeys } = 'Shame on you!'; #( keys % $happy_camper )[ int(rand scalar keys % $happy_cam +per ) ]; } } # if (( int rand 2000 ) == 0 ) # { # foreach my $_key ( sort keys % $happy_camper ) # { # printf "# %-12s ->> %s\n", $_key, $happy_camper->{ $_key }; # } # } } } exit 0; ## END
Using Net::SMTP to send email attachments
2 direct replies — Read more / Contribute
by astrobal
on Apr 30, 2017 at 02:44

    I crave the wisdom of the Perl Monks

    I am trying write a cgi program which sends an email to a customer with the invoice (in pdf format) attached. My previous experiences in sending emails has reduced me to using Net::SMTP as the only module that works - but I am open to enlightenment!

    The script below has been cribbed from previous questions on this topic, and the attachment of a binary file came from and example answer posted back in 2008. And it works - sort of. That is, the text part of the email comes through fine, but the ascii encoded binary file is just tagged on to the text part of the email rather than appearing as an attachment with it paperclip, as it should. I give the whole script below and then what is received as an email:

    Thanks for your consideration.

    #!/usr/bin/perl print "Content-type: text/html\n\n"; use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use warnings; use Net::SMTP; use MIME::Base64; my ($buf, $picture); my $company = ''; my $path = "/home/sites/$company/public_html"; my $attachBinaryFile = "image.jpg"; my $boundary = 'frontier'; my $passwd = "password"; my $contact = "name"; my $email = "info\@$company"; $smtp = Net::SMTP->new("mail.$company", Timeout => 30,Debug => 0,); $smtp->datasend("AUTH LOGIN\n"); $smtp->response(); $smtp->datasend(encode_base64("$contact\@$company") ); $smtp->response(); $smtp->datasend(encode_base64("$passwd") ); $smtp->response(); $smtp->mail("$contact\@$company"); $smtp->to($email); $smtp->cc(); $smtp->data(); $smtp->datasend("From: $contact\@$company\n"); $smtp->datasend("To: $email\n"); $smtp->datasend("Cc: info\@$company\n"); $smtp->datasend("Subject: To see if this will come through\n"); $smtp->datasend("\n"); $smtp->datasend(" This is some text. "); $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: image/jpeg; name=\"$attachBinaryFile\"\ +n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$attachBi +naryFile\"\n"); $smtp->datasend("\n"); open(DAT, "$path/$attachBinaryFile") || die("Could not open binary fil +e!"); binmode(DAT); local $/=undef; while (read(DAT, $picture, 4096)) { $buf = &encode_base64( $picture ); $smtp->datasend($buf); } close(DAT); $smtp->datasend("\n"); $smtp->datasend("--$boundary\n"); $smtp->dataend(); $smtp->quit; print "Mail sent\n"; exit; print "</body></html>";

    And here is what I get at the other end.

    This is some text. --frontier Content-Type: image/jpeg; name="image.jpg" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="image.jpg" AAABAAEAICAAAAEAIACoEAAAFgAAACgAAAAgAAAAQAAAAAEAIAAAAAAAABAAABILAAASCw +AAAAAA AAAAAAD4//L/9v7+//j7///6////+v/v////8////v7///39//L/9P/l/+7/6P/3//r+// +///P// ///5///69f/7//////b////3//////n/7//r/+j/+P/k////7/z////6////+f////P/// +/1////
Is ActivePerl repositories go free again?
2 direct replies — Read more / Contribute
by exilepanda
on Apr 30, 2017 at 00:39
    Dear monks,

    Story part 1: Days ago, I was looking for where to download PDK9.5(doesn't matter), and while I stepped in some page under ActiveState(from search result), I saw something alike mentioning ActivePerl will go free in the future. Because I was in a rush, I only glanced the page for few second (and can't find them again as my browser clear history per close)

    Story part 2: Yesterday, I was trying my best luck to install some modules in my very old machine using Perl 5.16 in WinXP, and when I hit on ppm.. I saw the repositories are all updated. I can tell there was something like redirected, 403 sort of before, seem I need a business license to access those repositories, but I could update them again yesterday.

    Can somebody tell if the repositories open for free again (for any perl version)?? Since I involved many machines, so I don't sure maybe I saw those redirect 403 from other machine.

Are there any modules using source filter to capture code?
1 direct reply — Read more / Contribute
by LanX
on Apr 29, 2017 at 17:47
Get url
4 direct replies — Read more / Contribute
by kepler
on Apr 29, 2017 at 17:25


    I'm having some troubles regarding a simple get for an url,

    Even using this script, I get a null result (in several hosts except one, bluehost):
    #!/usr/bin/perl use LWP::Simple; use warnings; print "Content-type: text/html\n\n"; my $url = " +ry/2.5_week.atom"; my $data = get $url; print "$url\n$data";

    Can someone help me out?

    Thanks, Kepler
Videos from LPW 2016?
No replies — Read more | Post response
by LanX
on Apr 29, 2017 at 15:59
using Skype
2 direct replies — Read more / Contribute
by louhevly
on Apr 29, 2017 at 13:19

    Greetings oh wise ones,

    My mother is in her 90s and lives alone in her own apartment in a retirement home in the States. I live in Europe and would like to write a Perl script that calls her landline phone every morning at 9am (this could be on a cronjob). If she answers, the program should report success and exit; if no answer, then call 10 minutes later. If after 30 minutes there is no answer, then call a different number requesting that someone check her room.

    My Skype account allows me to call Skype-to-landline for pennies, which is what I'm doing now manually. I searched for Skype on CPAN and found quite a number of modules described, but I thought it might save time asking here if someone knew just which module might be appropriate for what I'm trying to do.

    Thanks in advance, Lou

HASH value error
2 direct replies — Read more / Contribute
by pdahal
on Apr 29, 2017 at 01:58
    I am a beginner in perl. I need to parse an XML file and store the results into CSV file. The problem I am facing is I am getting the output like "HASH(0x3de3350)" instead of PubMed IDs like "16466327" in CSV file. How can i handle this?
Convert weekday to numerical date
3 direct replies — Read more / Contribute
by james28909
on Apr 28, 2017 at 23:23

    Hello again wise monks :)

    I have a list of weekdays:

    my @weekdays = qw( Today Yesterday Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday );

    I need to format these weekdays into an absolute numerical date based off of scalar localtime. eg:

    my ($local_weekday, $local_month, $local_numerical_date) = split (/\s+ +/, scalar localtime), 3; __END__ Fri Apr 28 'Yesterday' would be '27'

    I need to use $local_weekday and $local_numerical_date (eg. 'fri 28') to format @weekdays from the list. eg. 'wednesday' to eg. '26'.

    Also, i never need to check what the numerical date in the future is, it is always current date or earlier. I am pretty sure there is a way to loop while subtracting but I cant figure it out lol.

    If I can be any more clear on anything please let me know. I have tried to explain it the best i can :)

    EDIT: changed 'wednesday 27' to just '27' and added 'Yesterday' would be '27' to __END__

Array of variables
7 direct replies — Read more / Contribute
by Michael W
on Apr 27, 2017 at 11:38
    my @Variables = ( $Map_Request_Date,$Map_Due_Date,$Map_Cutover_Date,$ +Map_Complete_Date,$Map_Approved_Date); foreach $Date_Ref (@Variables) { print $Date_Ref; $Date_Ref =~ s/ +/ /; #When day is a single digit it creates two wh +ite spaces ($Month,$Day,$Year,$Time)= split / /, $Date_Ref,4; my %Months = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' + => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' + => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' +=> '12' ); if (length($Day) == 1) {$Day = "0$Day";}#Add 0 to the front of sing +le digit days @Variable[$X]=$Year."-".$Months{$Month} ."-" . $Day ; $X=$X+1; }

    Working on a date issue from sql to html format

    previous only used an array of variables to read from

    this time I want to place the value back into the array of variables

    this line: @Variable$X=$Year."-".$Months{$Month} ."-" . $Day ;

    all the code works until I try to write the new variable back in

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 monks are getting baked in the sun...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (5)
    As of 2017-04-30 18:43 GMT
    Find Nodes?
      Voting Booth?
      I'm a fool:

      Results (542 votes). Check out past polls.