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

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
Search::Elasticsearch date range
1 direct reply — Read more / Contribute
by bfdi533
on Apr 24, 2018 at 18:04

    I have a query I need to run against ElasticSearch and am using Search::Elasticsearch module. The issue is getting the data range correctly in the query on the PERL side as I can make this work from curl without any issues. After much tinkering with format, the following no longer throws errors when executing the code but the date range does not change the number of results returned no matter what I set the date to. This leads me to believe that there is something wrong with the code.

    For what it is worth, I have noticed that if I change the 'gte' to a 'lte' then I get a count of -1 showing no results are being returned. So, something is "working" if I do that ...

    Any hints, changes or suggestions would be much appreciated!

    #!/usr/bin/env perl # global settings use warnings; use strict; $|++; # libraries use Data::Dumper; use Search::Elasticsearch; use Try::Tiny; # global variables my $elk_host1 = '10.0.10.61:9200'; my $elk_host2 = '10.0.10.51:9200'; my $elk_host3 = '10.0.10.52:9200'; my $elk_host4 = '10.0.10.53:9200'; my $elk_user = 'user'; my $elk_pass = 'pass'; my $dt = `date +%F_%T`; chomp $dt; my $latest_dt; print "Starting run at: ".`date`; my $e = Search::Elasticsearch->new( nodes => [ "http://$elk_user:$elk_pass\@$elk_host1" , "http://$elk_user:$elk_pass\@$elk_host2" , "http://$elk_user:$elk_pass\@$elk_host3" , "http://$elk_user:$elk_pass\@$elk_host4" , ] , max_requests => 10000, ); my $results = $e->search ( size => 10000, index => 'api-*', body => { query => { bool => { must => { term => { '_type' => "alarm", }, }, filter => { range => { '@timestamp' => { gte => "2018-04-23 00:00:00", format => "YYYY-MM-DD HH:mm:ss" } } } } } } ); print "Count: ".$#{ $results->{hits}->{hits} }."\n";
Tk main window protocole maximize
2 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 24, 2018 at 14:47

    Is there a protocol to catch when the user maximize the main window? I use the following to catch when the user closes the application, but I can't find any counterpart to catch the maximization (or minimization) of the main window:

    $mw->protocol('WM_DELETE_WINDOW', \&CloseApp);

    Thank you in advance for any suggestion.

Create JSON file in specific format
2 direct replies — Read more / Contribute
by ovedpo15
on Apr 24, 2018 at 11:23
    Hi, I'm a beginner in Perl.
    I would like to use the JSON module in order to create one. In other words, I'm trying to build a function which creates a JSON file:

    Consider the following structure of a JSON file:

    { "time": 123456 "id": 56789 "data": [{"key": "first" , "value": "1" , "format": "1.5.6" , "ver +sion": "5.4"}, {"key": "two" , "value": "2" , "format": "1.4.6" , "versi +on": "5.4}, {"key": "five" , "value": "5" , "format": "1.5.9" , "vers +ion": "5.1"}] }
    This is the structure I would like the JSON file to be. Also, consider that the data is in a file that each one if its lines look like this:
    first,1,1.5.6,5.4 two,2,1.4.6,5.4 five,5,1.5.9,5.1

    So the array of objection that JSON file should contain is represented as each line of the previous file. I know how to split each line and push into a hash. So I would like to ask the two following questions:

    1. Is there a better way to than splitting and pushing into a hash?
    2. Main question: After I get a hash, how can I convert it to be of JSON file type while not forgetting to add the primitive values of "time" & "id"?
    I saw some examples of how to convert JSON to hash in Perl but didn't find an example of how to use the other way.

    Thank you and have a great day.
How to remove HTTP Keep-Alive 300 header from LWP::UserAgent request
1 direct reply — Read more / Contribute
by Veltro
on Apr 24, 2018 at 09:23
    Hello, I'd like to remove the Keep-Alive 300 header from the request but I can't figure out how.

    I am instantiating the LWP::UserAgent with the intention to use HTTP 1.1 and keeping the connection alive as follows:

    my $ua = LWP::UserAgent->new(keep_alive => 1, send_te => 0) ;

    send_te => 0 removes the TE header and the header information that I want to send contains tags like:
    my @ns_headers = ( 'ACCEPT' => '..', 'ACCEPT_ENCODING' => '...', 'ACCEPT-LANGUAGE => '..', 'UPGRADE_INSECURE_REQUESTS' => '1', 'USER-AGENT' => '...', ) ;
    I have tried several methods creating the request but each and one of them sends the Keep-Alive 300 header.

    my $response = $ua->get( $link, @ns_headers ) ;

    my $response = $ua->request(GET $link) ; # This does not use @ns_headers
    my $getReq = HTTP::Request->new( GET => $link, HTTP::Headers->new( @ns +_headers ) ) ; my $response = $ua->request( $getReq ) ;
    Tryig to get rid of it by specifying this in the @ns_headers does not work:

    'KEEP-ALIVE' => undef,

    Anyone knows how to do this? Thanks
TK param passing
2 direct replies — Read more / Contribute
by jsteng
on Apr 23, 2018 at 21:42
    I would see codes written like this:
    $submit->bind('<1>' => sub { Submit($p1, $p2, $p3, $p4, $p5); } );
    The above codes I understand, knowing how many parameters and how to intercept the params in a subroutine.



    However I dont understand this code:
    $yscroll=$mw->Scrollbar()->pack(-fill=>'y', -side=>'right',); $yscroll->configure( -command => [ \&ScrollAll, $yscroll, [$p1, $p2, $ +p3, $p4, $p5]]); sub ScrollAll { my ($sb, $wigs, @args) = @_; foreach my $w (@$wigs) { $w->yview(@args); } }


    Can anyone explain how the params are passed when it is constructed in this manner:
    -command => [ \&ScrollAll, $yscroll, [$p1, $p2, $p3, $p4, $p5]]
    In the above sub's code my ($sb, $wigs, @args) = @_;
    What is assigned to $sb?
    What is assigned to $wigs?
    Is there any other way of passing params when used with -command?
    And can that like be written like:
    -command => sub { subname ($p1, $p2, $p3, $p4, $p5); }
    thanks
paren formatter for function calls?
4 direct replies — Read more / Contribute
by iaw4
on Apr 23, 2018 at 21:36
    has someone already written a bracketed paren formatter for function calls? I am thinking increasing spaces for outer-more calls. non-function call spacing should be ignored. think
    ab + (fun1(fun2( 3 +. fun3( 4+(5+6) )))
    would become something linke
    `ab + (fun1(..fun2(.3 . + . fun3(4+(5+6)).)..)
    where I have replaced spaces with dots for clarity.
Problem installing Inline::Python
2 direct replies — Read more / Contribute
by paulorfmmb
on Apr 23, 2018 at 11:02
    Hi, I'm a beginner in Perl. I'm trying to install Inline::Python, but I get this error
    Running install for module 'Inline::Python' NINE/Inline-Python-0.56.tar.gz Has already been unwrapped into directory C:\strawberry\cpan\build\I +nline-Python-0.56-FjgGjd NINE/Inline-Python-0.56.tar.gz Has already been prepared Running make for N/NI/NINE/Inline-Python-0.56.tar.gz ---- Unsatisfied dependencies detected during ---- ---- NINE/Inline-Python-0.56.tar.gz ---- Proc::ProcessTable [build_requires] Running install for module 'Proc::ProcessTable' JWB/Proc-ProcessTable-0.55.tar.gz Has already been unwrapped into directory C:\strawberry\cpan\build\P +roc-ProcessTable-0.55-D7PRce JWB/Proc-ProcessTable-0.55.tar.gz Has already been prepared Running make for J/JW/JWB/Proc-ProcessTable-0.55.tar.gz dlltool --def ProcessTable.def --output-exp dll.exp g++ -o blib\arch\auto\Proc\ProcessTable\ProcessTable.dll -Wl,--base-fi +le -Wl,dll.base -mdll -s -L"C:\strawberry\perl\lib\CORE" -L"C:\strawb +erry\c\lib" ProcessTable.o C:\strawberry\perl\lib\CORE\libperl518.a + -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvap +i32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lw +inmm -lversion -lodbc32 -lodbccp32 -lcomctl32 dll.exp ProcessTable.o:ProcessTable.c:(.text+0x3f): undefined reference to `OS +_initialize' ProcessTable.o:ProcessTable.c:(.text+0x3d2): undefined reference to `O +S_get_table' c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.7.3/../../../../i686 +-w64-mingw32/bin/ld.exe: ProcessTable.o: bad reloc address 0x124 in s +ection `.rdata' c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.7.3/../../../../i686 +-w64-mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status dmake.exe: Error code 129, while making 'blib\arch\auto\Proc\ProcessT +able\ProcessTable.dll' JWB/Proc-ProcessTable-0.55.tar.gz C:\strawberry\c\bin\dmake.exe -- NOT OK Stopping: 'install' failed for 'Proc::ProcessTable'. Failed during this command: JWB/Proc-ProcessTable-0.55.tar.gz : make NO
    I tried installing just Proc::ProcessTable but it comes up with this error
    Running install for module 'Proc::ProcessTable' JWB/Proc-ProcessTable-0.55.tar.gz Has already been unwrapped into directory C:\strawberry\cpan\build\P +roc-ProcessTable-0.55-D7PRce JWB/Proc-ProcessTable-0.55.tar.gz Has already been prepared Running make for J/JW/JWB/Proc-ProcessTable-0.55.tar.gz dlltool --def ProcessTable.def --output-exp dll.exp g++ -o blib\arch\auto\Proc\ProcessTable\ProcessTable.dll -Wl,--base-fi +le -Wl,dll.base -mdll -s -L"C:\strawberry\perl\lib\CORE" -L"C:\strawb +erry\c\lib" ProcessTable.o C:\strawberry\perl\lib\CORE\libperl518.a + -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvap +i32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lw +inmm -lversion -lodbc32 -lodbccp32 -lcomctl32 dll.exp ProcessTable.o:ProcessTable.c:(.text+0x3f): undefined reference to `OS +_initialize' ProcessTable.o:ProcessTable.c:(.text+0x3d2): undefined reference to `O +S_get_table' c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.7.3/../../../../i686 +-w64-mingw32/bin/ld.exe: ProcessTable.o: bad reloc address 0x124 in s +ection `.rdata' c:/strawberry/c/bin/../lib/gcc/i686-w64-mingw32/4.7.3/../../../../i686 +-w64-mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status dmake.exe: Error code 129, while making 'blib\arch\auto\Proc\ProcessT +able\ProcessTable.dll' JWB/Proc-ProcessTable-0.55.tar.gz C:\strawberry\c\bin\dmake.exe -- NOT OK Stopping: 'install' failed for 'Proc::ProcessTable'. Failed during this command: JWB/Proc-ProcessTable-0.55.tar.gz : make NO
Error Using Module Spreadsheet::ParseXLSX
2 direct replies — Read more / Contribute
by roho
on Apr 23, 2018 at 10:52
    I am running Strawberry Perl 5.24 on Windows 10. I installed module "Spreadsheet::ParseXLSX". The module installed with no errors. When I "use" the module, the following error message is displayed:
    Can't load 'D:/perl/vendor/lib/auto/XML/Parser/Expat/Expat.xs.dll' for + module XML::Parser::Expat load_file:The specified module could not be found at D:/perl/lib/DynaL +oader.pm line 193. at D:/perl/vendor/lib/XML/Parser.pm line 18. The error is definitely occurring in 'D:/perl/vendor/lib/XML/Parser.pm +' line 18, which has "require XML::Parser::Expat;" in a BEGIN block. +Running perl -MXML::Parser::Expat -e 0 produces the same error messag +e.
    I double-checked module "XML::Parser::Expat" by running cpan install on it, and it says the module is up to date.
    The path and file 'D:/perl/vendor/lib/auto/XML/Parser/Expat/Expat.xs.dll' exist, so it's not clear why the above error message is displayed.

    Any suggestions are much appreciated.

    "It's not how hard you work, it's how much you get done."

Perl hash keys not considered unique
3 direct replies — Read more / Contribute
by elfstones65
on Apr 23, 2018 at 08:49

    Hi I seem to have a strange issue where adding a keys into a hash is not being considered as unique
    So say I have two files being the keys

    $file1 = 'ENV.FILESOURCE.SOURCE.J2018058.N000001'; $file2 = 'ENV.FILESOURCE.SOURCE.J2018059.N000001'; open LOG, "<$log"; while (defined($line=<LOG>)) { chomp $line; if ($line =~/File \[\/opt\/app\/data\/pulsefba\/process/) { ($processingFile) = ($line =~ /process\/(.+)]/); $files{"$processingFile"} = 1; } if ($line =~ /Finished/) { ($currentFile) = ($line =~ /process\/(.+?)]/); ($read) = ($line =~ /: Read \[(.+)\] events - Processe +d /); ($processed) = ($line =~ /Processed \[(.+?)\]/); ($wrote) = ($line =~ /Wrote \[(.+?)\]/); ($skippedRead) = ($line =~ /Skipped Read \[(.+?)\]/); ($skippedProcess) = ($line =~ /Skipped Process \[(.+?) +\]/); ($skippedWrite) = ($line =~ /Skipped Write \[(.+?)\]/) +; ($totalCount) = ($line =~ /Total Trailer Count - \[(.+ +?)\]/); $files{"$currentFile"}{'read'} = $read; $files{"$currentFile"}{'processed'} = $processed; $files{"$currentFile"}{'wrote'} = $wrote; $files{"$currentFile"}{'skippedRead'} = $skippedRead; $files{"$currentFile"}{'skippedProcess'} = $skippedPro +cess; $files{"$currentFile"}{'skippedWrite'} = $skippedWrite +; $files{"$currentFile"}{'totalTrailerCount'} = $totalCo +unt; print "Debug: $currentFile set to Read: $read\n"; print "File ENV.FILESOURCE.SOURCE.J2018058.N000001 cou +nt: $files{'ENV.FILESOURCE.SOURCE.J2018058.N000001'}{'read'}\n"; print "File ENV.FILESOURCE.SOURCE.J2018059.N000001 cou +nt: $files{'ENV.FILESOURCE.SOURCE.J2018059.N000001'}{'read'}\n"; }

    While looping through a file Output is like below. How Come is file with the J2018059 getting values in the hash for file J2018058?
    Debug: ENV.FILESOURCE.SOURCE.J2018058.N000001 set to Read: 1000
    File ENV.FILESOURCE.SOURCE.J2018058.N000001 count: 1000
    File ENV.FILESOURCE.SOURCE.J2018059.N000001 count: 1000
    Debug: ENV.FILESOURCE.SOURCE.J2018058.N000001set to Read: 2000
    File ENV.FILESOURCE.SOURCE.J2018058.N000001 count: 2000
    File ENV.FILESOURCE.SOURCE.J2018059.N000001 count: 2000
    Debug: ENV.FILESOURCE.SOURCE.J2018058.N000001 set to Read: 3000
    File ENV.FILESOURCE.SOURCE.J2018058.N000001 count: 3000
    File ENV.FILESOURCE.SOURCE.J2018059.N000001 count: 3000
    Debug: ENV.FILESOURCE.SOURCE.J2018058.N000001 set to Read: 4000
    File ENV.FILESOURCE.SOURCE.J2018058.N000001 count: 4000
    File ENV.FILESOURCE.SOURCE.J2018059.N000001 count: 4000
    Debug: ENV.FILESOURCE.SOURCE.J2018058.N000001 set to Read: 5000
    File ENV.FILESOURCE.SOURCE.J2018058.N000001 count: 5000
    File ENV.FILESOURCE.SOURCE.J2018059.N000001 count: 5000

Tk geometry, pixel coordinates precision, canvas and outline
2 direct replies — Read more / Contribute
by Discipulus
on Apr 23, 2018 at 08:07
    Hello monks and nuns!

    before the end of the Tk questions months I have another tk question.

    My goal is to have a canvas filling the mainwindow and I want to drow squares into this canvas to have evenly divided as a chessboard.

    The first question about mainwindow geometry was already answered by the wise kcott in the CB, but I also put it here for completeness and for future readers:

    • A mainwindow with geometry 100x100 goes from 0,0 to 100,100 ie it has 101 pixels! I found this very misleading.. but anyway it'snot the only problem i have.

    Infact, even using a mainwindow with geometry 99x99 I have undesired pixels here and there: in the program below I try to create 4 squares to fulfill the main canvas (but they do not arrive at the east and south borders: run it to see). Also the pixel used to outline the square are problematic..

    • canvas objects support the outline properties: is this added to the object area? I mean a square 50x50 with outline enabled (and is by default) ,means actually 51x51 one? It seems so: run my program and notice that north and west outlines are not shown as if they are outside of the visible area ( so -1,-1 as starting point).
    Notice that -outline => undef means transparent (you'll see the red of the main big canvas underlying), but specifying no outline assumes black as default .
    #!/usr/bin/perl use warnings; use strict; use Tk; # uncomment if module is installed # use Tk::WidgetDump; my ($dx,$dy); my $mw = Tk::MainWindow->new(); # kcot gently confirmed that 100x100 in geometry means # from 0,0 to 100,100 aka 101 pixels!! $mw->geometry("99x99"); # main underlying canvas is filled with RED my $can = $mw->Canvas( -height => 100, -width => 100, -bg => 'red', )->pack( ); # used to dump coordinates my %board; # alternate colors for tales my @alt_col = qw(gold2 green); # squares for tales starting at 0,0 my ($sq_x,$sq_y) = (0,0); foreach my $letter (('A'..'B')){ unshift @alt_col, pop @alt_col; foreach my $num (1..2){ # cycle colors unshift @alt_col, pop @alt_col; $can->createRectangle($sq_x, $sq_y, $sq_x+49,$sq_y+49, -fill => $alt_col[0], # outline undef means ransparent outline # if not specified defaults to black outline -outline => undef , -tags=> ['first'] ); $board{$letter.$num} = {tx=>$sq_x,ty=>$sq_y,bx=>$sq_x+49,by=>$ +sq_y+49}; # add 50 $sq_x += 50; } # reset x to 0 for new row $sq_x = 0; # add 50 to y $sq_y += 50; print "\n"; } # legenda print "ty = top Y tx = top X by = bottom Y bx = bottom X\n"; # dump board coordinates foreach my $key (sort keys %board){ print "$key -> ", ( map{"$_ $board{$key}{$_} "} reverse sort keys %{$board{$key}} ),"\n"; } $can->bind('first', '<1>', sub {&show_click();}); # uncomment if module is installed # $mw->WidgetDump; MainLoop; sub show_click{ my $ev = $can->XEvent; ($dx, $dy) = ($ev->x, $ev->y); print "CLICKED $dx $dy\n"; my $cur_id = ($can->find('withtag','current'))[0]; print "current canvas $cur_id\n\n"; }

    What the program outputs seems what I want; ie squares of 50 pixels sides:

    ty = top Y tx = top X by = bottom Y bx = bottom X A1 -> ty 0 tx 0 by 49 bx 49 A2 -> ty 0 tx 50 by 49 bx 99 B1 -> ty 50 tx 0 by 99 bx 49 B2 -> ty 50 tx 50 by 99 bx 99

    But what is shown on the screen seems misplaced toward north-west by one or more pixels. Also: when I dump the canvas I get:

    # main canvas width: 104 height: 99 reqwidth: 104 reqheight: 104 # squares coordinates dumped (from top left to bottom right) are: 0,0,49,49 50,0,99,49 0,50,49,99 50,50,99,99 # that reflect what I want

    Thanks for the patience and happy Tk month!

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Add your question
Title:
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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    marto facespalms after remembering where is copy of PBP is
    [marto]: getting ready for our new recruit

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (6)
    As of 2018-04-25 10:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?