Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Apache causing R to crash
1 direct reply — Read more / Contribute
by davies
on Jan 29, 2020 at 09:12

    I have a SSCCE, DebugR.cgi:

    #!c:/Strawberry/Perl/bin/perl use strict; use warnings; use Statistics::R; use CGI qw(:standard escapeHTML -utf8); print header(-type=>'text/html', -charset=>'utf-8'); my $R = Statistics::R->new(); print "R created\n"; $R->run('options(scipen = 999)'); # disabling scientific notation i +n R. print "R initialised\n";

    Running this from the command line works as expected:

    C:\Information\mgroot\cgi-bin>perl DebugR.cgi Content-Type: text/html; charset=utf-8 R created R initialised

    But pointing a browser to it via an Apache web server results in only the "R created" line and the following errors in the log:

    [Wed Jan 29 13:58:44 2020] [error] [client] ack Invalid +argument: write( 5, 'cmd<-c("write(ngettext(1, \\"Error: \\", \\"\\", + domain=\\"R\\"), stdout())"); write("\\1",stdout()) [Wed Jan 29 13:58:44 2020] [error] [client] ' ) at C:/St +rawberry/perl/vendor/lib/IPC/Run/ line 549. [Wed Jan 29 13:58:44 2020] [error] [client] Warning: una +ble to close filehandle GEN10 properly: Bad file descriptor during gl +obal destruction. [Wed Jan 29 13:58:44 2020] [error] [client] Warning: una +ble to close filehandle GEN5 properly: Bad file descriptor during glo +bal destruction.

    It is not obvious to me why there should be a difference or where to start looking next. Any clues would be appreciated.


    John Davies

Side effects of an /e modifier
3 direct replies — Read more / Contribute
by timpoiko
on Jan 29, 2020 at 09:01
    Dear monks If I have a function foo(),
    sub foo {my $c = shift; $c*=5; return $c;}
    All those substitutions works as expected:
    $bar =~ s/(\d{2})/joo/msg; $bar =~ s#(\d{2})#jee#msg; $bar =~ s#(\d{2})#foo($1)#mesg; $bar =~ s#(\d{2})#<td>ha ha ha</td>#msg;
    But if want to use all things: a /e modifier, html tags and # instead of / (because \-escaped slashes are not very readable)
    $bar =~ s#(\d{2})#<td>foo($1)</td>#mesg;
    I only get
    Bareword found where operator expected at line 18, near "<td>foo" (Missing operator before foo?) syntax error at line 18, near "<td>foo" Search pattern not terminated at line 18.
    man perlop is not very helpful.
    Any non-whitespace delimiter may replace the slashes. Add space after the "s" when using a character allowed in identifiers. If single quotes are used, no interpretation is done on the replacement string (the "/e" modifier overrides this, however). Note that Perl treats backticks as normal delimiters; the replacement text is not evaluated as a command. If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own pair of quotes, which may or may not be bracketing quotes, for example, s(foo)(bar) or s<foo>/bar/. A /e will cause the replacement portion to be treated as a full-fledged Perl expression and evaluated right then and there. It is, however, syntax checked at compile-time. A second "e" modifier will cause the replacement portion to be "eval"ed before being run as a Perl expression.
    Tested in Debian Jessie (5.20) and in Debian Buster (5.29).
How to combine the values from hash using 'cname', along with total and balance.
3 direct replies — Read more / Contribute
by Sami_R
on Jan 29, 2020 at 08:08

    Hi Monks

    I have hash with three different values for 'cname' 1. 'Smart Parking' 2. 'Highview Parking' and 3. 'ParkingEye', based on the 'cname' need to group it with number count and 'balance' values added.

    Please give me directions to achieve this. Thanks

    #!/usr/bin/perl -w use strict; use warnings; my $aref = [ { 'cname' => 'Smart Parking', 'balance' => 10.12, }, { 'cname' => 'Smart Parking', 'balance' => 10.22, }, { 'cname' => 'Smart Parking', 'balance' => 10.32, }, { 'cname' => 'Highview Parking', 'balance' => 20.12, }, { 'cname' => 'Highview Parking', 'balance' => 20.22, }, { 'cname' => 'Highview Parking', 'balance' => 20.32, }, { 'cname' => 'Highview Parking', 'balance' => 20.42, }, { 'cname' => 'ParkingEye', 'balance' => 30.12, }, { 'cname' => 'ParkingEye', 'balance' => 30.22, }, ];

    # Expected output

    $VAR1 = [ { 'cname' => 'Smart Parking', 'balance' => '30.66', 'total' => '3', }, { 'cname' => 'Highview Parking', 'balance' => '81.08', 'total' => '4', }, { 'cname' => 'ParkingEye', 'balance' => '60.34', 'total' => '2', } ];

    Thank you so much

Get total number of characters in a hash (not memory usage)
5 direct replies — Read more / Contribute
by TieUpYourCamel
on Jan 28, 2020 at 15:45
    I would like to get the total number of characters in a hash, keys plus values. For instance:
    { 'key' => 'value' } #should be 7
    The only thing I can find that does anything close to this is Devel::Size, but it gives the total amount of memory used by the hash, which is interesting, but not what I want:
    use strict; use warnings; use Devel::Size('total_size'); use feature 'say'; my $testme = { 'key' => 'value' }; say total_size($testme); # Output is "223"
    Is there a package that will do what I want?
Dump filtered subtree?
2 direct replies — Read more / Contribute
by LanX
on Jan 27, 2020 at 18:31

    Dear monastery

    In continuation of Proper Way to Reference a Hash Value

    Let's say you have a deeply nested data structure, but you want only to display the subtrees where an item (hash key or hash value or array) in between passes a test (eq or regex or whatever)

    Data::Printer has filters but they are normally used to format the current item.

    I have an idea how to implement it nevertheless, but am quite busy right now. And next week I will certainly forget it.

    Extra challenge: print the correct Perl path to access this element.

    Anyone interested?

    Alternative approaches?


    I was asked what "subtree" means. Those nested items which are not predecessor or successor are not displayed.


    This would be the subtree for finding the "field" key in the linked thread.

    \ { content { errors [ [0] { field "merge_fields", } ], }, }

    the path would be ->{content}{errors}[0]{field}

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

svg line graphs
2 direct replies — Read more / Contribute
by BernieC
on Jan 27, 2020 at 16:03
    From what I can tell, svg::tt:graph::xy can only plot a single data set. Other than that it looks great :o). Is it possible to get it to do multiple datasets {each in a different color}?

    I was thinking of writing my own program to do that. Doesn't seem too hard, but a bit complicated. I can see the parameters: x-range, y-range, y-title, x-title, y hash marks, x hash marks. And with that superstructure I think it'd be relatively easy to just do SVG lines from data-point to data-point but lots of messy stuff. I hit metacpan guessing that someone had done that and tt:graph:xy seems to only handle one set of data.

    Am I crazy/reinventinganoldwheel to start doing something like this just using the SVG package? Is there an existing package that I missed that could handle that?

Perl FCGI Error: mod_fcgid: error reading data from FastCGI server
3 direct replies — Read more / Contribute
by knox
on Jan 27, 2020 at 15:40

    Hello Monks,

    Your wisdom once again is appreciated. I've been testing some FCGI scripts, I've managed to get CGI::Fast working, but I'd like to get FCGI working. Unfortunately, I haven't be able to find good working examples. I'd like to see an example of FCGI return $ENV vars, similar to the CGI::Fast used in the Apache example: The code posted below is just me trying to get something to work, it is giving an internal server error. I've posted this is from my error log:

    [Mon Jan 27 19:44:02.915926 2020] [fcgid:warn] [pid 20681] (104)Connec +tion reset by peer: [client] mod_fcgid: error re +ading data from FastCGI server [Mon Jan 27 19:44:02.915973 2020] [core:error] [pid 20681] [client xx.] End of script output before headers: test.fcgi <code> <code> #!/usr/bin/perl use FCGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use Encode; #use CGI::Fast; #the CGI:Fast example from +fcgid.html is working; my FCGI example is not. my $count = 0; my $request = FCGI::Request(); while($request->Accept() >= 0) { $body = "Content-type:text/html; charset=utf-8\r\n\r\n"; $body .= '<html><head><meta http-equiv="Content-Type" content="te +xt/html; charset=utf-8" /></head><body>'; $body .= '<h1>Woohoooo!</h1>': $body .= '</body></html>'; $body .= '</body></html>'; $body = encode_utf8($body); print $body; }

    Aside from that, I have a very basic question with FastCGI - as it keeps a persistent connection, whats the best way to temporarily test it? I am new to FastCGI, it is compelling, but I don't know the vernacular to describe it well (yet). Let me know if the above doesn't make sense. Secondly, is there a way to get more descriptive error messages than the ones that come out of apache.log?

    And yes! I will look into frameworks like Plack/Dancer - but before I do, I'd like to get grasp of how FCGI works before I put a layer on top of it.

Mail::Chimp3 Add Member
2 direct replies — Read more / Contribute
by JEWebDes
on Jan 27, 2020 at 13:47

    I'm far from being a true Perl monk, and here's proof. I've been trying to add a member to a MailChimp list, but keep getting a 400 Bad Request - 'This value should of type object' error. The thing is I don't know how to build the Perl object required. Here's what I've tried:

    my $mailchimp = Mail::Chimp3->new( api_key => 'mykey' ); my %merge_fields = ( 'FNAME' => 'My', 'LNAME' => 'Name', 'PHONE' => '760-720-0000' ); my $response = $mailchimp->add_member ( list_id => '49ef108521', email_address => '', status => 'subscribed', merge_fields => %merge_fields );

    Obviously I haven't built the merge_field Perl object correctly. How should it be coded?

    Thanks for the help!

Hash (not) returned by subroutine
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 27, 2020 at 04:49
    I did not expect that $result in the code below is set, since it is not returned by the called subroutine. Why it happens?

    $ cat use strict; use lib ('.'); use Test qw (get_data); use Data::Dumper; my $result = get_data(); print Dumper $result; $cat package Test; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(); our @EXPORT_OK = qw( get_data ); #------------- sub get_data { my $data = {a=>'b'}; } 1; $ perl $VAR1 = { 'a' => 'b' };
Outputting JSON with sorted names/keys
5 direct replies — Read more / Contribute
by pryrt
on Jan 25, 2020 at 19:15

    I'm trying to create a simple JSON file. I wanted to try to get the names/keys output in a specific (non-alphanumeric) order (to make it easier on the human users of the JSON -- I know it's irrelevant to any automated parsers). Using the JSON module, I can get unsorted or alphanumerically sorted. Manually rolling it, I can get in whatever order I specify. Is there an option I'm not seeing in the JSON docs for altering the sort-order? or another module that has user-supplied sort order?

    (It's not overly important; this is mostly as a learning opportunity; my manual_ordered_json could have finished this one-off job for me already, but I was hoping to expand my toolbase knowledge.)


missing second of time
8 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 25, 2020 at 04:47
    I was calculating orbital elements once per second with Perl and noticed a glitch that occasionally doubled one of the derivatives for a single second.

    I was using the core function "time" and noticed that the unixtime stamp would skip a second just before the event would occur.

    Then I tried to see more with Time::HiRes and saw that a second would be skipped when the decimal turned over.

    I ran these two lines of code concurrently in bash shells, and saw the strange results:

    perl -le 'while () { print time; sleep 1 }'
    1579941924 <- WTF?
    perl -MTime::HiRes=time -le 'while () { print time; sleep 1 }'
    1579941924.00213 <- WTF!
    A third terminal running at the same time printed the missing second, but, it skipped one when it eventually turned over.
    1579941923.52163 <- OK
    The interval does not seem significant:
    1579941924.00213 <- WTF!
    I tried a lot of things like disabling NTP and using usleep but it always skips and causes my program to flip out for a second. What is this sorcery? Thanx
How do I Pull data out of Array inside of Hash
3 direct replies — Read more / Contribute
by nachtmsk
on Jan 24, 2020 at 19:33

    I am trying to pull JSON data from an online source.

    I was able to get the data and store it into $datahash.

    I dereferenced that into %hashdata

    Then ran through the keys. Output below.

    The key named "value" is an array. I need to run through that data in that array and work with it. It is the first value in the data section I printed out below the code.

    My question is how can I get the data out of that ARRAY that is in the hash %hashdata.

    Thanks very much for any help.

    use LWP::Simple 'get'; use Data::Dumper; use Mojo::JSON qw(decode_json encode_json); use constant URL => ' +t/test?access_token=xxxxxxxxxxxxxx'; my $json = get URL or die "Unable to get JSON data"; my $datahash = decode_json $json; %hashdata = %$datahash; #print Dumper(%hashdata); #print $hashdata{'value'}; ## Array that holds the data foreach $key (keys %hashdata) { $value = $hashdata{$key}; print "Key $key Value $value\n\n\n"; }
    Data returned =============

    Key value Value ARRAY(0x5612912d40c8) ## NEED to get into this Array and get data out.

    Key @odata.context Value

    Key @odata.count Value 10006

    Key @odata.nextLink Value

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 exploiting the Monastery: (8)
As of 2020-01-29 17:49 GMT
Find Nodes?
    Voting Booth?