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
Using Crypt::OpenPGP with large files
5 direct replies — Read more / Contribute
by roperl
on Jul 24, 2017 at 16:48

    I need to decrypt large pgp/gpg files

    I can do this with GnuPG, but it calls an external gpg program binary ie "/usr/local/bin/gpg"

    The Crytp:OpenPGP module does this with a pure perl implementation
    However, the decrypt function returns the decrypted file into a scalar variable

    open( my $fh, '>', 'pt.txt' ); my $pt = $pgp->decrypt(Filename => $file, Passphrase => $pp); $fh = $pt; close $fh;
    This works fine with small files, but a large encrypted file (100MB) can return a huge plain text file (1.5 GB) This then will cause Out of Memory! error.
    Is there any way to handle this using Crypt::OpenPGP?
Converting XLS to HTML
2 direct replies — Read more / Contribute
by dirtdog
on Jul 24, 2017 at 15:39

    Hello Monks

    I'm creating an XLS spreadsheet on a Linux server using Spreadsheet::WriteExcel. It has 12 worksheets and each sheet has different color combinations and URL hyperlinks.

    Now I need to convert it to HTML to display it on a webpage. I can do this manually without issue, but when I tried to use Spreadsheet::ParseExcel to automate the process (borrowed from an old monk post) I ended up with an extremely basic display

    my $excel_obj = Spreadsheet::ParseExcel->new(); my $workbook = $excel_obj->Parse($outfile); die "Workbook did not return worksheets!\n" unless ref $workbook->{Worksheet} eq 'ARRAY'; for my $worksheet ( @{$workbook->{Worksheet}} ) { print OUT_HTML_FILE "<table>\n"; for my $row ( 0 .. $worksheet->{MaxRow} ) { print OUT_HTML_FILE "<tr>\n"; for my $col ( 0 .. $worksheet->{MaxCol} ) { my $cell = $worksheet->{Cells}[$row][$col]; print OUT_HTML_FILE "<td>"; print OUT_HTML_FILE ref $cell ? $cell->Value : ''; print OUT_HTML_FILE "</td>\n"; } print OUT_HTML_FILE "</tr>\n"; # record ends } print OUT_HTML_FILE "</table>\n"; # worksheet ends }

    When I look at the files that Excel produces when I use the "Save As" to an HTML file. It produces a main .htm file plus a link to 12 separate htm files for each worksheet. Within each sheet the HTML seems to be somewhat complex. Can you please let me know if Spreadsheet::ParseExcel would be a good option to try and produce the same .htm files that are produced from the "File Save As" option within Excel?

    Thank you very much for your time

POE child autoflush stdout
1 direct reply — Read more / Contribute
by sub_soNic
on Jul 24, 2017 at 12:17

    Hi guys!

    I have been playing around with POE for a while, I would like to create a little script that forks subprocesses while there are tasks to process and also log the result of those tasks. It works fine except for the "logging" part

    I replaced the while(1) with sleep for making the problem more obvious. The problem is that the stdout/stderr is only displayed after the parent is done with the sleep wherever I put it, the rest of the code is executed tho just fine. So the child processes might not autoflush with POE?.. If so, could you give me an advice please how to force it or some workaround.

    use POE; use POE::Component::Client::SMTP; use POE qw( Wheel::Run ); use strict; use warnings; $|++; POE::Session->create( inline_states => { _start => \&on_start, got_child_stdout => \&on_child_stdout, got_child_stderr => \&on_child_stderr, got_child_close => \&on_child_close, got_child_signal => \&on_child_signal, get_next_logic => \&on_get_next_logic, } ); POE::Kernel->run(); exit 0; sub on_start { print "on_start\n"; $_[KERNEL]->yield("get_next_logic"); sleep(10); } sub get_next_file { print "get_next_file\n"; } sub on_get_next_logic { my $child; print("get_next_logic\n"); $child = POE::Wheel::Run->new( Program => sub { get_next_file() }, StdoutEvent => "got_child_stdout", StderrEvent => "got_child_stderr", CloseEvent => "got_child_close", #Conduit => "pty" ); $_[KERNEL]->sig_child($child->PID, "got_child_signal"); # Wheel events include the wheel's ID. $_[HEAP]{children_by_wid}{$child->ID} = $child; # Signal events include the process ID. $_[HEAP]{children_by_pid}{$child->PID} = $child; print( "Child pid ", $child->PID, " started as wheel ", $child->ID, ".\n" ); sleep(10); } ####### signal handling stuff i haven't touched ##### # Wheel event, including the wheel's ID. sub on_child_stdout { my ($stdout_line, $wheel_id) = @_[ARG0, ARG1]; my $child = $_[HEAP]{children_by_wid}{$wheel_id}; print "pid ", $child->PID, " STDOUT: $stdout_line\n"; } # Wheel event, including the wheel's ID. sub on_child_stderr { my ($stderr_line, $wheel_id) = @_[ARG0, ARG1]; my $child = $_[HEAP]{children_by_wid}{$wheel_id}; print "pid ", $child->PID, " STDERR: $stderr_line\n"; } # Wheel event, including the wheel's ID. sub on_child_close { my $wheel_id = $_[ARG0]; my $child = delete $_[HEAP]{children_by_wid}{$wheel_id}; # May have been reaped by on_child_signal(). unless (defined $child) { print "wid $wheel_id closed all pipes.\n"; return; } print "pid ", $child->PID, " closed all pipes.\n"; delete $_[HEAP]{children_by_pid}{$child->PID}; } sub on_child_signal { print "pid $_[ARG1] exited with status $_[ARG2].\n"; my $child = delete $_[HEAP]{children_by_pid}{$_[ARG1]}; # May have been reaped by on_child_close(). return unless defined $child; delete $_[HEAP]{children_by_wid}{$child->ID}; }
    Thank you very much!
crypt function has string length limit?
3 direct replies — Read more / Contribute
by jamroll
on Jul 24, 2017 at 11:18
    i'm using perl's 'crypt' function.

    does it have a limit on the size of the string being passed in?

    #!/usr/bin/perl # must have's! use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use DBI; use URI::Escape; use lib "/var/www/html/Pm"; my $pw = "Aardvark"; # 8 characters my $salt = "ab"; my $pwCrypted = crypt($pw, $salt); print "content-type: text/plain\n\n"; if (crypt("Aardvarks", $salt) eq $pwCrypted) { print "yup" } else { pr +int "nope"; } exit 1;
    the above prints "yup", not "nope" as you might expect....seems crypt does have a limit, although the doc i reference doesn't state the existence of such a limit. is there an equivalent which operates exactly the same?
printing complex data structures
4 direct replies — Read more / Contribute
by ic23oluk
on Jul 24, 2017 at 10:50

    Hello Monks,

    I have trouble with complex data structures and I'm currently wondering why the following code doesn't work:

    use strict; use warnings; sub base_code (\%); my %aas = ( 'serine' => ['TCA', 'TCC', 'TCG', 'TCT'], 'proline' => ['CCA', 'CCC', 'CCG', 'CCT'] ); my %codes; $codes{'earth'} = \%aas; $codes{'mars'} = { 'serine' => ['QWZ', 'QWX', 'QWW'], 'proline' => ['ZXZ', 'ZXX', 'ZXQ', 'ZXW'] }; base_code (%codes); ## line 17 sub base_code (\%) { my $h_ref = $_[0]; foreach my $planet (keys %$h_ref){ foreach my $aa (keys %$planet){ + ## line 22 foreach my $codon (@{ $planet->{$aa} }){ print $codon, "\n"; } } } }

    the error message is: Can't use string ("earth") as a HASH ref while "strict refs" in use at line 22.

    Can anyone help? Thanks in advance :)

    for extra credits: Why does it complain if I write base_code (\%codes) in line 17 ? I thought it must be a reference..

LWP / HTPP Post not working
No replies — Read more | Post response
by amitsq
on Jul 24, 2017 at 10:43
    I am currently using Perl to write a program which can automatically submit information to some websites with LWP, getting results, which i print in a table on my own website. For most of the pages, it works well, but for one it doesn't: After submit, I get a redirection to a captcha (no need of any complex solving her, after extracting the captcha, i want the user to solve it and enter the code). However once i want to post the reply to reach to the result page, the session get reloaded and I can not fetch the result page. Whats going wrong?
    #extracted captcha picture before, and catched user's reply in $captch +a #trying to submit captcha code: $param="numIg=&numDgc=&numIgc=&numIp=&pageSize=3&strWhere='.$mySearch. +'&numDg=&numIpc=&numUgc=&numUgd=&strLicenseCode=&numIgd=&showType=1&n +umSortMethod=&strSources=&numUg=&pageNow=1&vct='.$captcha.'&Submit=%E +7%BB%A7%E7%BB%AD"; $Search_url ="one website"; $request = HTTP::Request->new('POST', $Search_url); $request->header('Accept' => 'text/html,application/xhtml+xml,applicat +ion/xml;q=0.9,*/*;q=0.8'); #$request->header('Accept-Language' => 'de,en-US;q=0.7,en;q=0.3'); $request->header( 'Referer' => "one webiste"); $request->header('Connection' => 'keep-alive'); $request->content($param); $response = $ua->request($request); $page = $response->decoded_content(); print $page;
    If i visit the html page, i can enter the captcha code and get to the result page directly. To compare, the request observed by the firefox plugin looks as following:
    Request Header Host: website User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/2010010 +1 Firefox/52.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0. +8 Accept-Language: de,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate Referer: website Cookie: _gscu_1718069323=94938064vupi3q75; _gscu_7281245=960585782o5hw +t18; fp_ver=4.2.3; BSFIT_CLICK_ARRAY=; _va_id=6af9c5bacce82b34.149725 +6276.9.1500561808.1500561808.; _va_ref=%5B%22%22%2C%22%22%2C150056180; BS +FIT_EXPIRATION=1499900863032; BSFIT_OkLJUJ=FCPai-v72cPAKxWwXtmw7Q5V23 +KeTUXF; BSFIT_DEVICEID=DJtgNB-72bmDzc9ryT1Cgn-fF8rxZOW3kAgDU2FyxVGjzC +7zxS49h52Qp1ACtmLH80ib8FDb4CmAPGjJLNEZapA7Wp83mJtoq2ZD4U1hXHNjGYkHEss +9Rd21Z7bHaoRKU4o5N6CCTPsB36cPMQwXSxGrZuTY7XRd; br_access_code=ZrbCHtq +r/h8AAAAATpJHWQAAAABmB0vG; JSESSIONID=7F350E89175BADCBEEDEA44B6F12F4E +A; WEB=20111116; _gscbrs_7281245=1; _gscs_7281245=t009042585zmvlf21|p +v:5 Connection: keep-alive Upgrade-Insecure-Requests: 1
    Request Body ontent-Type: application/x-www-form-urlencoded Content-Length: 267 numIg=&numDgc=&numIgc=&numIp=&pageSize=20&strWhere=%27.%E6%97%A0%E5%88 +%B7%E7%94%B5%E6%9C%BA.%27&numDg=&numIpc=&numUgc=&numUgd=&strLicenseCo +de=&numIgd=&showType=1&numSortMethod=&Submit=%E7%BB%A7%E7%BB%AD&strSo +urces=&numUg=&pageNow=1&vct=y23m4&Submit=%E7%BB%A7%E7%BB%AD
Why am I getting "syntax error at line 18, near "$barney return" ?
2 direct replies — Read more / Contribute
by prospect
on Jul 24, 2017 at 04:36

    So I am messing around with subroutines, I got this piece of code running last week but now when I return to it, it's giving me syntax error on line 18. I compared the sources I got it from and the syntax used in those cases and I just can't see it. I wanted to make a little exercise where the user inputs two values, 1 for $fred and 1 for $barney and it would say which value is bigger. But when I run it, it says there is a syntax error on line 18 and I cannot find it :/

    sub combo { my $sum = $fred * $barney return $sum; if ($fred > $barney) { $fred; #also why do I have to put another $fred here? It doesn +#'t seem to be part of "if" nor of "else" } else { $barney; } } print "Enter value:"; $fred = <STDIN>; # chomp $fred; print "Enter value:"; $barney = <STDIN>; # chomp $barney;
Failure to cpanm PP on portable Strawberry Perl 24
2 direct replies — Read more / Contribute
by aplonis
on Jul 23, 2017 at 12:56

    Seeking a workaround for the failure of cpanm to install Tk::TableMatrix in Starwberry Perl v26, I thought to maybe try it out in a portable installation of Strawberrry Perl 24, and that worked. So, finding success, I next went to install also the module PP, which promply failed.

    The failure occurs here ...

    A:\PP>cpanm pp --> Working on pp Fetching +tar.gz ... OK Configuring PAR-Packer-1.037 ... OK ==> Found dependencies: Module::ScanDeps, Getopt::ArgvFile --> Working on Module::ScanDeps Fetching +.24.tar.gz ... OK Configuring Module-ScanDeps-1.24 ... OK Building and testing Module-ScanDeps-1.24 ... OK Successfully installed Module-ScanDeps-1.24 --> Working on Getopt::ArgvFile Fetching +1.11.tar.gz ... OK Configuring Getopt-ArgvFile-1.11 ... OK Building and testing Getopt-ArgvFile-1.11 ... OK Successfully installed Getopt-ArgvFile-1.11 Building and testing PAR-Packer-1.037 ...

    It fails with the pop-up herewith included. The pop-up occures six times in a row, like that over and over.

    The pop-up says...

    The procedure entry point _ZdlPvy could not be located in the dynamic link library C:\Strawberry\perl\bin\perl526.dll

    Just to be sure there was no confusion with cpanm from my installed Strawberry on the c: drive, I tried it again like below, (first insuring that my prior installs had really taken)...

    ---------------------------------------------- Welcome to Strawberry Perl Portable Edition! * URL - * see README.TXT for more info ---------------------------------------------- Perl executable: A:\PP\perl\bin\perl.exe Perl version : 5.24.2 / MSWin32-x64-multi-thread A:\PP>A:\PP\perl\bin\perl.exe -e "use Tk"; A:\PP>A:\PP\perl\bin\perl.exe -e "use Tk::TableMatrix"; A:\PP>A:\PP\perl\bin\cpanm.exe PP 'A:\PP\perl\bin\cpanm.exe' is not recognized as an internal or externa +l command, operable program or batch file. A:\PP>A:\PP\perl\bin\cpanm PP ! Finding PP on cpanmetadb failed. --> Working on PP Fetching +r.gz ... OK Configuring PDL-PP-0.010 ... N/A ! Configure failed for PDL-PP-0.010. See A:\PP\data\.cpanm\work\150082 +8689.4884\build.log for details. A:\PP>A:\PP\perl\bin\cpanm PAR::Packer --> Working on PAR::Packer Fetching +tar.gz ... OK Configuring PAR-Packer-1.037 ... OK Building and testing PAR-Packer-1.037 ...

    ...but results were as before.

Understanding endianness of a number
3 direct replies — Read more / Contribute
by stevieb
on Jul 23, 2017 at 11:07

    Hey all,

    I ran into a situation recently where I was reading from an I2C device, and the byte ordering was in the opposite endianness that I was expecting. Although I fixed the problem by reversing the bytes before returning them, I went off to research exactly what endian was. I've spent numerous hours testing, reading and more testing, but I still can't grasp it fully. I'm hoping just one or two more examples will have it 'click'.

    So, I'll start off with a couple of examples here to see if I have the basics down. Please feel free to elaborate with other examples or comments etc.

    Set up our number, and two byte scalars (full version copy/pastable at the bottom of the post):

    use warnings; use strict; use feature 'say'; my $num = 1023; # 0x03ff my ($b1, $b2);

    Now, if I do the following bit shifting, the printf() is printing the bytes in big endian format, correct?

    $b1 = ($num & 0xff00) >> 8; $b2 = $num & 0xff; printf("%x, %x\n", $b1, $b2); # 3, ff

    Likewise, if I reverse the operations/bytes, this one will print in little endian format, right?

    $b1 = $num & 0xff; $b2 = ($num & 0xff00) >> 8; printf("%x, %x\n", $b1, $b2); # ff, 3

    Full code:

    use warnings; use strict; use feature 'say'; my $num = 1023; my ($b1, $b2); $b1 = ($num & 0xff00) >> 8; $b2 = $num & 0xff; printf("%x, %x\n", $b1, $b2); $b1 = $num & 0xff; $b2 = ($num & 0xff00) >> 8; printf("%x, %x\n", $b1, $b2);
sub variables
4 direct replies — Read more / Contribute
by Todd Chester
on Jul 22, 2017 at 19:33
    Dear Perl Monks,

    In Perl 5, is there a way to name my variables in the () of the sub declaration, as I do in Perl 6?
    sub DoSomething( $DataStr ) {;}

    Many thanks, -T

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 one hand claps...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2017-07-25 22:04 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (382 votes). Check out past polls.