Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
Count of Repeated Characters
1 direct reply — Read more / Contribute
by santu4frnds
on Apr 26, 2015 at 23:57

    Hi Monks, I have a string like 'dftaatzaaadfaa'.I need a count of 'aa' in a string.

Perl.js: porting Perl to the browser?
1 direct reply — Read more / Contribute
by Dumu
on Apr 26, 2015 at 20:44

    Dear fellow Monks,

    Has anyone tried using Perl.js (or anything similar) to write client-side web applications in Perl?

    Could this be the end of the traditional Perl on the server / JS in the browser split? While Node.js is bringing JS onto the server, have the Perl.js developers pluckily occupied the browserspace with Perly tanks* while the Node legions weren't looking?

    (*or possibly more monastically appropriate huts, gardens & allotments!)

    To return to the 'tank' analogy, perhaps the engines could do with a little tinkering yet.

Passing digits in perl
4 direct replies — Read more / Contribute
by peacefuldragon
on Apr 26, 2015 at 18:03
    Hello monks, perl novice here. I've been using perl for under a week, our class has decided to do a 2-week crash course into the wonderful world of perl to complete our year. As such we were given a simple task (or so it seemed). Ask the user to enter 16 digits, then print those 16 digits out in 4 groups of 4 (IE: 1234 1234 1234 1234). So far I have managed to do this:
    use strict; use warnings; print 'Please enter your card number' chomp($inp = <>); $inp =~ s/(\d{4})(\d{4})(\d{4})(\d{4})/$1 $2 $3 $4/; print $inp,"\n";
    It didn't work and I want to see what I've done wrong and what I need to fix in order to reach my end goal. Thank You
AnyEvent code blocking Twiggy event loop
2 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 26, 2015 at 17:44
    Anyone have any idea why my AnyEvent timer is blocking Twiggy from handling requests? The initial response is served immediately, but any subsequent requests aren't handled until the timer is done running/deleted. Any ideas?
    use strict; use Plack; use AnyEvent; our $timers = {}; my $app = sub { my $time = time(); $$timers{$time} = AE::timer 0, 0, sub { sleep 60; delete $$timers{$time} }; return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello World' ] +]; };
Having difficulties with "split"
4 direct replies — Read more / Contribute
by andybshaker
on Apr 26, 2015 at 17:08

    Hi everyone, I have an array called @Coordinates where each element looks like 1111111..2222222, 3333333..4444444, 5555555..6666666, etc. They are not all the same number, obviously--that is just an example. The point is that in each element, there are 7 or 8 numbers separated by ".." . What I need to do is make an array with elements 1111111, 2222222, 3333333, 4444444, 5555555, 6666666, etc. I tried using a foreach loop with

    foreach my $C (@Coordinates){ push (@NewCoordinates, split("..",$C)};

    to no avail. Does anyone have suggestions? Also, I just want to say how helpful this website is. I don't know where I'd be without it.

Naming ordinals (directions/sides/faces) in the presence of 3D rotation?
3 direct replies — Read more / Contribute
by BrowserUk
on Apr 26, 2015 at 15:57

    With apologies, this is essentially the same question as I attempted to frame in Refactoring technique?, but I finally think I've got enough of a handle on the problem to ask the right question; but not yet the solution.

    (Please excuse (or ignore) the presence of JavaScript code in the following; the question pertains to all languages, and I'm bored with mocking up Perl substitutes.)

    In the code I'm refactoring I've (mechanically) abstracted out three recurrent conditions, without (previously) understanding what it was they really tested:

    var condX = ( ( this.Parent.Fi < 90 ) || ( this.Parent.Fi >= 270 ) + ); var condY = ( this.Parent.Fi >= 180 ); var condZ = ( this.Parent.Th <= 0 );

    Obviously, condX, condY & condZ are not great names. I've finally worked out what it is that they control. (For those coming to this fresh, the code constructs 3 reference grids around a graph (chart) that is rotatable in all 3 dimensions.)

    1. condX determines whether the front or back face of the bounding box is drawn depending upon the current rotation.
    2. condY determines whether the left or right face of the bounding box is drawn depending upon the current rotation.
    3. condZ determines whether the top or bottom face of the bounding box is drawn depending upon the current rotation.

    First off; those names were my "best guess" before I'd tied them down, and are not right.

    But they demonstrate the real problem: All our words for ordinals/directions/etc. are tied to a point of view and our expectations

    • Up is Top is Above is North is MaxHieght is ...
    • Left is East is MinWidth is ...
    • Back is MaxDepth is Away is ...

    And ditto for terms like X-axis/horizontal and Y-axis/vertical, etc.

    But when things can be rotated in all dimensions, left can become right; up, down; back, front, min, max; so naming things in code that can be called with stuff in any state of rotation becomes a nightmare.

    Often, you can get around this problem by naming variables in terms of the model view: that is, pretending you are standing on the 'floor' of the bounding box and rotate with it (without gravity messing things up) and name them in terms relative to that viewpoint.

    But equally often, and as is the case of the conditionals above, the ordinates being represented are relative to the viewer's perspective; ie. world coordinates. That is, the decision of what faces to draw is determined in terms of which faces are 'behind' the model, thus not obscuring the model (graph) from the viewer.

    And that I think is the key. Whatever the orientation, the faces to be draw are the three of the six that are 'furthest' from the viewer; but even with that insight, I'm still stuck for how to name them. And as each of those conditions is reused 4 times each, I think they definitely do need to be named.

    So to the question: what would you name them?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
LWP and SHA2
1 direct reply — Read more / Contribute
by dorko
on Apr 26, 2015 at 14:46

    I've got several Perl programs that talk to a learning management system (LMS) used by about 50,000 users. The LMS has three environments, TEST, QA and PRODUCTION. From my desktop, I can talk to all three environments just fine. From the server that handles the management of the LMS, I can talk to PRODUCTION ok. The server can't talk to the TEST or QA environments.

    I'm using LWP for all the communications. My desktop and server are at the same version of the LWP module (6.04 -- the current of LWP is 6.13).

    This is the error I'm getting:

    ( '500 Can\'t connect to', \bless( { '_content' => '', '_uri' => bless( do{\(my $o = 'https://QA.server.exa}, 'URI::https' ), '_headers' => bless( { 'user-agent' => 'Management P +rogram', 'authorization' => 'Bearer SU +PERPOWERFULLPASSWORDGOESHERE' }, 'HTTP::Headers' ), '_method' => 'GET' }, 'HTTP::Request' ), 'Can\'t connect to LWP::Protocol::https::Socket: SSL connect attempt failed with unknown +error error:0D0C50A1:asn1 encoding routines:ASN1_item_verify:unknown +message digest algorithm at /opt/perl/lib/site_perl/5.16.1/LWP/Protoc +ol/ line 51. ', \bless( { '_content' => 'Can\'t connect to QA.server.example.c +om:443 LWP::Protocol::https::Socket: SSL connect attempt failed with unknown +error error:0D0C50A1:asn1 encoding routines:ASN1_item_verify:unknown +message digest algorithm at /opt/perl/lib/site_perl/5.16.1/LWP/Protoc +ol/ line 51. ', '_rc' => 500, '_headers' => bless( { 'client-warning' => 'Internal + response', 'client-date' => 'Sun, 26 Apr + 2015 18:18:56 GMT', 'content-type' => 'text/plain +', '::std_case' => { 'client-war +ning' => 'Client-Warning', 'client-dat +e' => 'Client-Date' } }, 'HTTP::Headers' ), '_msg' => 'Can\'t connect to +443', '_request' => ${$![1]} }, 'HTTP::Response' ) );

    I pretty sure I know what's going on: The TEST and QA servers are using SHA2 / SHA256 SSL certificates. PRODUCTION is using SHA1. The OpenSSL on the server is 0.9.8y. Everything I've read says OpenSSL greater than 0.9.8o can handle SHA2.

    My question is how do I fix this? Will upgrading LWP to 6.13 fix things? (I'm assuming the version of LWP I'm using was installed using an earlier version of OpensSSL than is currently installed.) How can I get LWP to work with SHA2? How can I get LWP to use the more recent version of OpensSSL?



    -- Yup, I'm a Delt.
Is CGI::Session compatible with CGI::Lite::Request ?
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 26, 2015 at 14:34

    Hello monks, today I replaed with CGI::Lite::Request for my website
    Since then login functionality became not working.
    Documentation of CGI::Session says it is compatible with any like modules which have param() method.
    But CGI::Lite::Request do have param() method.
    Especially the problem comes from here:

    $session = CGI::Session->load(undef, $request, {Directory => "$LOCAL_PATH/admin/session"}, {name => 'sid'}); #returns undef

    Other things such as setting cookie, creating session worked fine, but that method didn't work.
    so.. is it possible to use CGI::Lite::Request with CGI::Session? if not, is there any alternative to CGI::Session for CGI::Lite::Request?

random web page breakages (Dancer2)
2 direct replies — Read more / Contribute
by nikosv
on Apr 26, 2015 at 13:50

    (cross-posted at the mail list

    I'm building a site and it's all fine and well except when randomly getting part of the page (without any css,html) essentially like a semi-half blank page and ,sometimes again, getting a "Reload the page to get source for:" from the GET request using Firebug. Firebug's Net panels shows only the initial GET request but not the subsequent ones to the other resources, like the CSS file, images,Javascripts etc

    When that happens I get the following in the DBI trace log :

    <-- dbd_st_finish 1 <- fetch= ( undef ) [1 items] row1 at /home/starman/perl5/perlbrew +/perls/perl-5.18.1/lib/site_perl/5.18.1/x86_64-linux/DBD/ lin +e 867 via at /home/starman/Foo/bin/../lib/MySite/ line 219 <- fetchrow_hashref= ( undef ) [1 items] row1 at /home/starman/Foo +/bin/../lib/MySite/ line 219 via at /home/starman/Foo/bin/.. +/lib/ line 34 <> DESTROY(DBI::st=HASH(0x3fcfc60)) ignored for outer handle (inne +r DBI::st=HASH(0x3fcfac8) has ref cnt 1) -> DESTROY for DBD::mysql::st (DBI::st=HASH(0x3fcfac8)~INNER) <- DESTROY= ( undef ) [1 items] at /home/starman/Foo/bin/../lib/Fo line 34 via at /home/starman/Foo/bin/../lib/ line 34 DESTROY (dbih_clearcom) (sth 0x3fcfac8, com 0x41e4d70, imp DBD::my +sql::st): FLAGS 0x180191: COMSET Warn RaiseError PrintError PrintWarn PARENT DBI::db=HASH(0x3d23dc0) KIDS 0 (0 Active) NUM_OF_FIELDS 38 NUM_OF_PARAMS 0 dbih_clearcom 0x3fcfac8 (com 0x41e4d70, type 3) done. <> DESTROY(DBI::db=HASH(0x3d18b00)) ignored for outer handle (inne +r DBI::db=HASH(0x3d23dc0) has ref cnt 1) -> DESTROY for DBD::mysql::db (DBI::db=HASH(0x3d23dc0)~INNER) imp_dbh->pmysql: 42d7440 <- DESTROY= ( undef ) [1 items] at /home/starman/Foo/bin/../lib/Fo line 34 via at /home/starman/Foo/bin/../lib/ line 34 DESTROY (dbih_clearcom) (dbh 0x3d23dc0, com 0x418e4e0, imp DBD::my +sql::db): FLAGS 0x180391: COMSET Warn RaiseError PrintError PrintWarn Aut +oCommit PARENT DBI::dr=HASH(0x3c85940) KIDS 0 (0 Active) IMP_DATA HASH(0x3d23478) dbih_clearcom 0x3d23dc0 (com 0x418e4e0, type 2) done.

    I should explain more:
    there are two queries each one in its own sub, the first one being called through a straightforward [% PROCESS %] which populates some elements like drop down select menus,sliders etc

    When the user makes a choice the second query is fired through an Ajax call:

    $.ajax({ url: '/query2', type: "POST", data: AllData(1)}).success(function(data){ $("#contentx").append(data); });
    which populates a #contentx div with the results of the query.

    Now,when in the same session, the error does not occur no matter how much I stress it. When opening another 4-5 sessions,or doing a couple of reloads of the same session, the DBI error comes up and I get the effect of the first query being run as the elements are still populated, but then there is nothing else:no css,scripts,and the rest of the html code .

    Is that indicating a global DBI handle that is reused and cleaned up at some time,having to do with Starman's preforking? The thing is that in my Model I initialize the database and statement handles within the subs scope and not globally.
    Any help deeply appreciated,thanks!

    The setup is Dancer,Starman,Mysql on Digital Ocean
    DBD:mysql 4.031
    DBI 1.633
    Dancer2 0.158000
    Ubuntu 14.04 x86_64 x86_64 x86_64 GNU/Linux

perl module help
3 direct replies — Read more / Contribute
by janasec
on Apr 26, 2015 at 13:40

    hi I am learning to write automation for running some tests,to begin with I have written the following code.I need to know how I can create a perl module so I can make calls to check if a host is alive

    #!/usr/bin/perl use strict; use warnings; use lib '/home/suse/junk/automation1/emulex'; use Config::Simple; use Net::Ping::External qw(ping); use 5.010; #myconf.cfg is a file with all hosts my $cfg = new Config::Simple('/home/suse/junk/automation1/emulex/mycon +f.cfg'); #accessing values my $host = $cfg->param("host1"); print "checking $host is reachable or not\n"; my $alive = ping(hostname => "$host", count => 5, size => 1024, timeou +t => 3); print "$host is alive!\n" if $alive or die"Could not ping host '$host' + ";

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.
  • 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 having an uproarious good time at the Monastery: (3)
    As of 2015-04-27 07:52 GMT
    Find Nodes?
      Voting Booth?

      Who makes your decisions?

      Results (497 votes), past polls