Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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
Calculating holidays
1 direct reply — Read more / Contribute
by htmanning
on Jul 25, 2017 at 18:56
    Monks, I'm trying to calculate whether a given date is a major holiday, namely Thanksgiving. The string is in date format like this 2017-11-20. Using DateTime I can do things like tell the day of the week of the server, but how do I parse a date submitted in a string to figure out if it is the 4th Thursday of the month? I'm confused. I believe Thanksgiving is between the 22nd and 28th and the 4th Thursday of the month, but I don't know how to calculate that from a date. I can do something like this:
    $thanks = "2017-11-24"; my ($thyear, $thmonth, $thday) = split(/-/,$thanks);
    but I don't know how to figure out which Thursday it is.
"sketch" directory not being added with 'make manifest'
1 direct reply — Read more / Contribute
by stevieb
on Jul 25, 2017 at 18:48

    Another day, another request for advice.

    In RPi:WiringPi, I have a docs/ directory that includes subdirs breadboard/, fritzing/, schematic/ and sketch/. As you can see by browsing my docs dir on CPAN, sketch/ doesn't show up, but it's there.

    When I do a make manifest it doesn't get added, but all other directories do just fine. Can anyone spot something I'm missing in my MANIFEST.SKIP file here, or provide an answer as to why this directory refuses to be included?

    # MANIFEST.SKIP ^images/ .bs$ .c$ .o$ .sh$ ~$ ^blib/ ^pm_to_blib/ .old$ .orig$ .tar.gz$ .bak$ .swp$ .hg/ .hgignore$ ^_build/ ^Build$ ^MYMETA\.yml$ ^MYMETA\.json$ ^README.bak$ ^Makefile$ .metadata/ .idea/ pm_to_blib$ .git/ .debug$ .gitignore$ .ignore.txt$ .travis.yml$ .iml$ build/ ^\w+.list$ .bblog$ .base$ main$

    It's a pretty old skip file which needs to be cleaned up, but it's what I'm running with that shows the problem so I'm posting it as is in case I'm overlooking a regex or something.

    Here's the current MANIFEST as of a minute ago, after running make manifest.

Perl in programming contests and problem solving
2 direct replies — Read more / Contribute
by rsFalse
on Jul 25, 2017 at 16:49

    There are many programming contests and websites for problem solving. Do you like to solve problems and when do you use Perl? Can you suggest programming contests or websites with string problems or something at which Perl excels?

    Usually in programming contests there are algorithmic problems. You are given some task, which contain: 1. problems description, 2. input constraints and format, 3. output format, 4. time and memory limits. Then you write and submit some code, and testing system tests your code using some test cases which you do not know, except you know constraints. After testing, system tells verdict: accepted, wrong-answer, time-limit-exceeded or something else.
    Some examples of online contests are: codeforces (many languages available, Perl including), topcoder; some examples of online judges are: SPOJ, ProjectEuler, Timus, Rosalind. Some online judges like Project Euler accepts only answers, not code. Majority of people write solutions for these problems in C++ language. Less people use Java, C, Python, Pascal. That is because C++ is fast and short to express and have macros. Many problems are composed in such way that only very fast algorithm can solve all edge cases in a given time limit. For example if author's solution solve the problem in 1 s, then time limit can be set around 2-5 s. If author solved with C++, then it is often impossible to achieve such speed with higher level languages, like Perl or Python. So, Perl was used mostly not in usual competitive programming, but in golf contests :D

    I am using Perl in contests for fun and am solving easiest tasks, when tasks which are more difficult require better understanding of algorithms and data structures.

    If you want to try Perl in solving some string problems, I can suggest -> problemset and choose tag "strings". You can see pieces of test cases and solutions of other users to comparison code and comparison time program consumed on each test case. If you have a nice solution for a problem or you are stuck in it you can start discussion here(?). Websites have their own forums/blogs for discussing problems, but usually these discussions are not language specific. That discussions can be about which Perl structures to use to gain better speed or similar. While using Perl I've discovered that many my solutions on simple tasks are about the same speed as Python users. And do you know if any of more popular online judge systems are written in Perl?
WWW-Authenticate with [WWW::Mechanize::Firefox]
No replies — Read more | Post response
by ptizoom
on Jul 25, 2017 at 15:32

    I am trying to automate data scraping of a well know router which has not SNMP services. It needs to answer a basic authentication challenge which starts with this request:

      WWW-Authenticate: basic realm="'Default: admin/1234"
    but with WWW::Mechanize::Firefox, we do not seem to build this WWW-Authenticate response...

    following the examples in... I use this code:
    use WWW::Mechanize::Firefox; use File::Temp qw/ :seekable /; my $url = ''; my $formSubmitURL; my $httprealm = 'Default: admin/1234'; my $username = 'admin'; my $password = '1234'; my $res; my $hostname = $url; (my $u = "$url") =~ s!/$!!; my $mech = eval { WWW::Mechanize::Firefox->new( autodie => 0 , launch => '/usr/bin/firefox' || system(qq(which firefox) +) , autoclose => 0 , activate => 1 # bring the tab to the foreground , log => [qw[debug]] ) }; my $nsILoginManager = $mech->repl->expr(<<'JS'); Components.classes[";1"] .getService(Components.interfaces.nsILoginManager) JS my $logins = $nsILoginManager->findLogins({}, $url, $formSubmitURL, $h +ttprealm); my $count = $logins->{length}; my $nsILoginInfo; unless($count) { $nsILoginInfo = $mech->repl->expr(<<"JS"); var nsILI = new Components.Constructor( "\;1" , Components.interfaces.nsILoginInfo , "init" ); new nsILI("$hostname", null, "$httprealm", "$username", "$password +", "", "") JS # in resource://gre/components/nsLoginManager.js $res = $nsILoginManager->addLogin($nsILoginInfo); } (my $tpl_f = $url) =~ s/.*\/\///g; $tpl_f =~ s/\/.*//g; $tpl_f .= '_XXXXXX'; my $temp_p = File::Temp->newdir(TEMPLATE => $tpl_f, OPEN => 0); my %attr = (':content_file' => $temp_p->dirname . '.html', no_cache => + 1); $res = $mech->get($url, %attr);

    but I always get this response ...

    $mech->content '<html><head></head><body>WWW::Mechanize::Firefox</body></html>'
    which is the first page.

    Whereas remarkably, the file... "" $temp_p->dirname . '.html' "", contains the routers starting page:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://ww"> <html><head><meta http-equiv="Content-Type" content="text/html; chars +et=utf-8"> <title>Some Technology</title></head>(.v.)<noframes><body></body></no +frames></html>
  • what is the way to automatically build this basic authentication challenge response with WWW::Mechanize::Firefox ?
  • thx.
how to execute 'tmboot -y'
5 direct replies — Read more / Contribute
by ytjPerl
on Jul 25, 2017 at 14:24

    Hi guys, I need to execute 'tmboot -y' to startup application, and before that I need to run setenv.cmd to setup the environment. I have my code as below, but it was running with failure, stating 'tmboot' is not recognized as internal or external command. usually when I dont run setenv.cmd before 'tmboot -y', I got that error. but I already ran that in my script. I do not what is wrong with my script, thanks

    use warnings; use strict; chdir "/server/setup"; my $env = system('setenv.cmd'); my $output = system('tmboot -y'); print $output;
Proc::ProcessTable won't install
1 direct reply — Read more / Contribute
by roperl
on Jul 25, 2017 at 14:18
    I'm trying to use Proc::ProcessTable on Solaris 10
    I downloaded and compiled Perl 5.26.0 and installed to /opt/perl-5.26.0, which I symlink to /opt/perl
    I'm trying to install Proc::Process 0.53 to /opt/perl
    #/opt/perl/bin/perl Makefile.PL Checking if your kit is complete... Looks good Writing MYMETA.yml and MYMETA.json Generating a Unix-style Makefile Writing Makefile for Proc::ProcessTable Writing MYMETA.yml and MYMETA.json #make cp blib/lib/Proc/ cp blib/lib/Proc/ cp blib/lib/Proc/ cp blib/lib/Proc/ cp ../blib/lib/Proc/ProcessTable/ AutoSplitting ../blib/lib/Proc/ProcessTable/ (../blib/lib/au +to/Proc/ProcessTable/Process) Manifying 1 pod document Running Mkbootstrap for ProcessTable () chmod 644 "" "/opt/perl-5.26.0/bin/perl5.26.0" -MExtUtils::Command::MM -e 'cp_nonem +pty' -- blib/arch/auto/Proc/ProcessTable/ProcessTable 644 "/opt/perl-5.26.0/bin/perl5.26.0" "/opt/perl-5.26.0/lib/5.26.0/ExtUtil +s/xsubpp" -typemap '/opt/perl-5.26.0/lib/5.26.0/ExtUtils/typemap' P +rocessTable.xs > ProcessTable.xsc mv ProcessTable.xsc ProcessTable.c /opt/solarisstudio12.3/bin/cc -c -D_REENTRANT -I/usr/local/include + -DPERL_USE_SAFE_PUTENV -O -DVERSION=\"0.53\" -DXS_VERSION=\"0.53 +\" -KPIC "-I/opt/perl-5.26.0/lib/5.26.0/sun4-solaris-thread-multi/COR +E" -DPROC_FS -D_POSIX_PTHREAD_SEMANTICS ProcessTable.c /opt/solarisstudio12.3/bin/cc -c -D_REENTRANT -I/usr/local/include + -DPERL_USE_SAFE_PUTENV -O -DVERSION=\"0.53\" -DXS_VERSION=\"0.53 +\" -KPIC "-I/opt/perl-5.26.0/lib/5.26.0/sun4-solaris-thread-multi/COR +E" -DPROC_FS -D_POSIX_PTHREAD_SEMANTICS OS.c "OS.c", line 121: warning: implicit function declaration: bless_into_p +roc rm -f blib/arch/auto/Proc/ProcessTable/ /opt/solarisstudio12.3/bin/cc -G -L/usr/lib -L/usr/ccs/lib -L/opt/sol +arisstudio12.3/prod/lib/sparc -L/opt/solarisstudio12.3/prod/lib -L/li +b -L/usr/local/lib OS.o ProcessTable.o -o blib/arch/auto/Proc/Proce +ssTable/ \ \ chmod 755 blib/arch/auto/Proc/ProcessTable/ Manifying 3 pod documents # make test "/opt/perl-5.26.0/bin/perl5.26.0" -MExtUtils::Command::MM -e 'cp_nonem +pty' -- blib/arch/auto/Proc/ProcessTable/ProcessTable 644 Manifying 1 pod document No tests defined for Proc::ProcessTable::Process extension. PERL_DL_NONLAZY=1 "/opt/perl-5.26.0/bin/perl5.26.0" "-MExtUtils::Comma +nd::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_ +harness(0, 'blib/lib', 'blib/arch')" t/*.t t/process.t .. ProcessTable.c: loadable library and perl binaries are +mismatched (got handshake key 7f80080, needed 8180080) t/process.t .. Dubious, test returned 1 (wstat 256, 0x100) Failed 3/3 subtests Test Summary Report ------------------- t/process.t (Wstat: 256 Tests: 0 Failed: 0) Non-zero exit status: 1 Parse errors: Bad plan. You planned 3 tests but ran 0. Files=1, Tests=0, 0 wallclock secs ( 0.04 usr 0.01 sys + 0.05 cusr + 0.01 csys = 0.11 CPU) Result: FAIL Failed 1/1 test programs. 0/0 subtests failed. *** Error code 1 make: Fatal error: Command failed for target `test_dynamic'
    So look like somewhere is picking up the already installed perl at /bin/perl
    ProcessTable.c: loadable library and perl binaries are mismatched (got handshake key 7f80080, needed 8180080)

    How can I fix this?
how to put Chart::Gnuplot::DataSet->new and Chart::Gnuplot->new into a subroutine?
2 direct replies — Read more / Contribute
by buchi2
on Jul 25, 2017 at 10:52

    I have several datasets with different y-values, sometime with different x-values, which I want to give to Chart::Gnuplot::DataSet->new.

    In the moment I copy the codeblock 10times and give another ydata/xdata.

    More nicefull it will be, if I can do it with a subroutine, if it is possible?

    in the moment e.g.:
    my $tempmin = Chart::Gnuplot::DataSet->new( xdata => \@yfix, ydata => \@minit, style => "lines", color => "dark-gray", linetype => "dash", width => 1, ); my $tempmax = Chart::Gnuplot::DataSet->new( xdata => \@yfix, ydata => \@maxit, style => "lines", color => "dark-gray", linetype => "dash", width => 1, );

    But I have no idea, how a subroutine have to look, where I pass e.g. @x, @y and other plotparameters to?

    And as next step a subroitine to which I can give some plotsets and some other parameters, like filename, for creating a picture. And call it again with other parameters for the next picture?

    Regards, Buchi

how to access array of hash with arrays?
2 direct replies — Read more / Contribute
by buchi2
on Jul 25, 2017 at 10:05

    In a hash I have some arrays. This I get work. But now I want to build several of this hashes an array. I did it with push. But how I can get out now a value?

    #!/usr/bin/perl -w my %hierhash; my @feld; my @hashfeld; my $wert; @hashfeld = (); %hierhash = &dofn(0,5,1); print "1 $hierhash{'var2'}[3] \n"; push(@hashfeld,%hierhash); %hierhash = &dofn(0,5,2); print "2 $hierhash{'var2'}[3] \n"; push(@hashfeld,%hierhash); $wert = $hashfeld[0]->{'var2'}[3]; print "$wert \n"; sub dofn () { my ($i, $ivon, $ibis, $imal); my %hashfn; $ivon = $_[0]; $ibis = $_[1]; $imal = $_[2]; for ($i = $ivon; $i < $ibis; $i++) { push(@{$hashfn{'var1'}}, $i*$imal); push(@{$hashfn{'var2'}}, $i*$imal*10.); push(@{$hashfn{'var3'}}, $i*$imal*20.); } return %hashfn; }
    Regards, buchi
SSL Apgrade failed
2 direct replies — Read more / Contribute
by trahulprajapati
on Jul 25, 2017 at 08:52
    Greetings All

    I'm trying to load a page which is using HTTPS protocol and it is giving me an error

    SSL upgrade failed: SSL connect attempt failed because of handshake problems

    I have installed LWP::PROTOCOL::https. This is my code

    use strict; use warnings; use WWW::Scripter; use Net::SSL; my $w= WWW::Scripter->new( cookie_jar => { autosave => 1} ); $w->use_plugin('JavaScript'); # packaged separately $ENV{HTTPS_VERSION} = '3'; $w->get(''); my $home_title=$w->title(); print $home_title;

    Please suggest

    Thanks in advance!

Need to sort comma delimited, double quoted file
5 direct replies — Read more / Contribute
by CSharma
on Jul 25, 2017 at 05:22
    Hi PMs! I've CSV file, comma delimited, embedded (, & ") in double quotes; as you can see in row given below.
    6450458,6011,"Urine - Culture & Sensitivity",1658,"Colony Count:","10^ +3 cfu/ml",,2016-10-26 09:55:34,0,"", ,"",,2016-10-26 09:55:34,SS00002 +03,6,"All Tests Done and Verified",,SCIN72669,2016-10-24 12:04:58,21, +"Max Smart Super, Speciality Hospital "O"",3445,"Bansidhar Tarai ",0, +"SAVITRI DEVI",False,"SAVITRI ","DEVI",SKCT,334,20905,"Anjani Kuma +r Agrawal",1957-01-01 00:00:00,0,NULL,"7838457000","INFO@MAXHEALTHCA +RE.COM",OP,NO,Verified,2016-10-24 12:04:58,2016-10-24 12:04:58,Lab,24 +3981,0,"",F 6444885,21732,"Blood - Culture & Sensitivity",3147,"Method BacT/ALERT3 +D & Vitek 2","SubHead",,2016-10-26 09:00:11,1,"min", ,"",0,2016-10-26 + 09:00:11,PM0004746,6,"All Tests Done and Verified",,PMIN4335,2016-10 +-21 19:07:36,25,"PMC",3445,"Bansidhar Tarai ",0,"SUSHILA DEVI 94907", +False,"SUSHILA DEVI","94907",PMCL,1861,69142,"Parkash Gera",1961-01 +-01 00:00:00,0,NULL,"0143000000","INFO@MAXHEALTHCARE.COM",OP,NO,Verif +ied,2016-10-21 19:07:36,2016-10-21 19:07:36,Lab,781642,0,"",F 6444891,21732,"Blood - Culture & Sensitivity",3147,"Method BacT/ALERT3 +D & Vitek 2","SubHead",,2016-10-26 09:00:36,1,"min", ,"",0,2016-10-26 + 09:00:36,PM0004748,6,"All Tests Done and Verified",,PMIN4337,2016-10 +-21 19:11:24,25,"PMC",3445,"Bansidhar Tarai ",0,"TUSHAR BHATIA 94916" +,False,"TUSHAR BHATIA","94916",PMCL,1876,69142,"Parkash Gera",1985- +01-01 00:00:00,0,NULL,"0143000000","INFO@MAXHEALTHCARE.COM",OP,NO,Ver +ified,2016-10-21 19:11:24,2016-10-21 19:11:24,Lab,773211,0,"",M

    Requirement: Sort file based on 3 columns in file i.e. primary: 31, secondary: 1, tertiary: 2 i.e. sorted as below, 1st -> 2nd -> 3rd

    1,2,3 1,2,4 1,3,4 2,1,5

    What I'm doing: i.) converting file to pipe(|) separated from comma(,) -> using Text::CSV module ii.) Sorting using File::Sort Here is the code snippet:

    &commaToPipeDelimiter($maxFile, $pipeMaxFile); sort_file({t => '|', k => ['31n', '1n', '2n'], I => $pipeMaxFile, o => + $sortedMaxFile}); sub commaToPipeDelimiter{ my ($file, $pfile) = @_; my $csv = Text::CSV->new({binary => 1, decode_utf8 => 1, auto_diag + => 1, allow_loose_quotes => 1}); open(my $data, '<:encoding(utf8)', $file) or die "Could not open ' +$file' $!\n"; open(W, ">".$pfile) || die "Could not open $pfile $!\n"; while(my $line = <$data>){ chomp $line; if($csv->parse($line)){ my @fields = $csv->fields(); print W join("|",@fields),"\n"; } else{ warn "Line could not be parsed: $!\n"; } }

    Is there some other efficient way someone can suggest? Rather than converting file into pipe separated file then sort since files could be much larger. FYI -> Embedded commas need to be taken care of.


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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2017-07-26 01:29 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (383 votes). Check out past polls.