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

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
Result of failed regexp match not pushed onto array
1 direct reply — Read more / Contribute
by zork42
on Feb 16, 2019 at 07:02

    I'm completely mystified by this.

    In the 3 example code blocks below I'm pushing the same things onto an array.

    When I push the result of a failed match, nothing is pushed onto the array (unless I first save the result in a variable).

    Why is this?!


    use strict; use warnings; my @foo; push @foo, 1; print scalar @foo, "\n"; # output +: 1 push @foo, 'abc' =~ /abc/; print scalar @foo, "\n"; # output +: 2 push @foo, 'abc' =~ /abcd/; print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, ''; print scalar @foo, "\n"; # output +: 3 push @foo, 0; print scalar @foo, "\n"; # output +: 4 push @foo, undef; print scalar @foo, "\n"; # output +: 5 @foo = (); print "\n"; push @foo, (1); print scalar @foo, "\n"; # output +: 1 push @foo, ('abc' =~ /abc/); print scalar @foo, "\n"; # output +: 2 push @foo, ('abc' =~ /abcd/); print scalar @foo, "\n"; # output +: 2 - nothing added to @foo?! push @foo, (''); print scalar @foo, "\n"; # output +: 3 push @foo, (0); print scalar @foo, "\n"; # output +: 4 push @foo, (undef); print scalar @foo, "\n"; # output +: 5 @foo = (); my $bar; print "\n"; $bar = 1; push @foo, $bar; print scalar @foo, "\n"; + # output: 1 $bar = 'abc' =~ /abc/; push @foo, $bar; print scalar @foo, "\n"; + # output: 2 $bar = 'abc' =~ /abcd/; push @foo, $bar; print scalar @foo, "\n"; + # output: 3 - this time its been added to @foo $bar = ''; push @foo, $bar; print scalar @foo, "\n"; + # output: 4 $bar = 0; push @foo, $bar; print scalar @foo, "\n"; + # output: 5 $bar = undef; push @foo, $bar; print scalar @foo, "\n"; + # output: 6
Perl to C problem
2 direct replies — Read more / Contribute
by lbrandewie
on Feb 16, 2019 at 00:49

    Hey folks,

    I'm having a problem. I'm porting some perl to C to speed it up, and I can't get the two languages to agree on what's going on. I'm using ActiveState's perl 5.24.1 for Windows. Consider the following code:

    $test = "\n\n\n\n\n";
    for ($x = 0; $x < length($test); $x++) {
        print ord(substr($test, $x, 1));

    The code prints a string of 10s, indicating that the 13/10 combination in Windows has been translated to a bare 10. I can understand that, for compatibility reasons. But it seems to mean there is no way in Windows of telling a 13/10 combo from a bare 10. This makes a difference in my code, in that I can't get C and perl to hash the same string the same way. Am I missing something? Is there no way at all to tell a 13/10 from a 10 in Windows perl?

    I believe the upshot of this, where my code is concerned, is that perl is doing it "wrong" compared to C and I'll never get the two to agree. I hope I'm wrong about that. I note with interest and mild annoyance that strlen("\n") == 1 even in C for windows. But C allows me to get at the underlying character buffer and perl does not.



Function::Parameters, Types::Standard, and Perl::Critic
1 direct reply — Read more / Contribute
by choroba
on Feb 15, 2019 at 18:36
    After switching to Function::Parameters, I noticed Perl::Critic no longer worked. For example, for the old fashioned
    sub foo { $_[0] + 1 }
    perlcritic -1 outputs
    Always unpack @_ first at line 22, column 1. See page 178 of PBP. (S +everity: 4) Subroutine "foo" does not end with "return" at line 22, column 1. See + page 197 of PBP. (Severity: 4)

    but for the equivalent

    use Function::Parameters; fun foo ($x) { $x + 1 }
    it doesn't say anything.

    I've found a partial workaround: running perlcritic on the output of B::Deparse instead of the source itself. It reports lots of sins committed by Function::Parameters themselves (e.g. Magic variable "$^H" should be assigned as "local" at line 14, column 44. See pages 81,82 of PBP.) but seems to work.

    Unfortunately, it gets more complex when Types::Standard are added to the mix.

    use Function::Parameters; use Types::Standard qw( Num ); fun foo (Num $x) { $x + 1 }
    The code doesn't look much more complicated, but running perl -MO=Deparse on it just hangs. Output of strace ends with endlessly repeating lines:
    mmap(NULL, 192512, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1 +, 0) = 0x7fe772fdf000 mremap(0x7fe771772000, 532480, 536576, MREMAP_MAYMOVE) = 0x7fe77177200 +0 brk(0x49ac000) = 0x49ac000 brk(0x49cd000) = 0x49cd000 mremap(0x7fe771772000, 536576, 540672, MREMAP_MAYMOVE) = 0x7fe77177200 +0 brk(0x49ee000) = 0x49ee000 brk(0x49ed000) = 0x49ed000 brk(0x4a0e000) = 0x4a0e000 brk(0x4a2f000) = 0x4a2f000 mremap(0x7fe771772000, 540672, 544768, MREMAP_MAYMOVE) = 0x7fe77177200 +0 ...
    The perl process seems to grow in memory very slowly at the same time.

    Has anyone succeeded in linting a code that uses the above mentioned modules? Tips, tricks, suggestions welcome.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Parsing JSON out of an incremental stream
2 direct replies — Read more / Contribute
by Sec
on Feb 15, 2019 at 11:40

    Hi. I'm looking for a simple way to parse JSON objects out of a stream.

    I am calling an HTTP service that returns a stream of json objects over time.

    To read it, i registered a simple callback handler in LWP::UserAgent

    $ua->add_handler( response_data => sub { my($response, $ua, $h, $data) = @_; return "true" if $data =~ /^\s*$/; print "Callback triggered\n"; print "data: .${data}.\n"; my $result = eval { $json->decode( $data ) }; if ($@){ print "error: $@\n"; return "true"; }; print Dumper $result; return "true"; } );

    As expected, i sometimes get partial json objects in $data.

    My first idea would be to so something like m/{.*?}/ against $data. Or rather some more elaborate version that deals with balanced parens and quoted strings.

    My question is, is there an easier / simpler / more straightforward way to deal with something like this that I am missing?

    -- Sec
    EDIT: to clarify a bit.

    The problem is that in the callback $data may just be

    { "foo": "bar" } { "foo" : "q
    which means I have to grab one object, handle it, keep the rest and wait for more data.

    I know how to write code for that "the long way". I was asking if there is something clever or an existing module that can help with this.

Print out hash
2 direct replies — Read more / Contribute
by catfish1116
on Feb 15, 2019 at 11:07

    I am trying to create a hash and then print it out. When executing program, I am getting $key & $value need explicit package. O wise monks, please show me the error of my ways :)

    #! /usr/bin/perl use v5.12; use warnings; ## 2/15/19 my @people = qw{ fred barney fred wlima dino barney fred pebbles +}; my %count; # new empty hash $count{$_}++ foreach @people; while ( ($key, $value) = each %count) { print "$key => $value\n"; } ~

    TIA the catfish

can has ELI5 namespace::autoclean pls?
2 direct replies — Read more / Contribute
by Ea
on Feb 15, 2019 at 09:58
    Friday afternoon question: what's the real purpose of namespace::autoclean in your OO code?

    I've seen it in classes written with Moose, but the description Keep imports out of your namespace doesn't really speak to me about the advantage of using it. Where am I going to get bitten if I don't use it. Just a simple answer will do.



    Sometimes I can think of 6 impossible LDAP attributes before breakfast.

    Mojoconf was great!

Useful heuristics for analyzing arrays of data to determine column header
3 direct replies — Read more / Contribute
by nysus
on Feb 14, 2019 at 18:06

    I'm writing a Perl module to try to autodetect whether a spreadsheet/csv file has a header row. It's a somewhat tricky problem especially when trying to factor in the kind of malformed data people might feed in. I'm sticking with simple cases for now. I'd like to try to do some statistical analysis of the data to help me. Unfortunately, my knowledge of statistics is very weak. I'm feeling my way in the dark. So take this sample column for example:


    It's obviously a column of states. One thing that might jump out to a computer is that the length of the first row is 5 letters while the rest of the rows are two letters. Things can of course get fuzzier. The first row might have 5 letters while the rest of the columns have 2 OR 3 letters. Other tell-tale signs might be the header column is a string while the column is full of numbers. Or the header column might be named "STATUS" while the data might only contain the words "ACTIVE" or "RETIRED." The size of the column is also an important factor. If there is a lot of data, any statistical approach will likely be more accurate.

    The Math::NumberCruncher module has some useful functions I think I could use like standard deviation to help me analyze things like how diverse the dataset is for a certain property (length, number of unique values, etc.). But I'm not really sure how I might apply it to be useful in a practical way. I found this interesting article was useful but there's no code and not being familiar with statistics, I'm still not clear on exactly how that analysis was done.

    I intend to analyze each column and try to come up with some kind of "likely has header" factor based on the analysis. If it looks like most columns have a header, then it will determine that the spreadsheet has a header row.

    Sorry this question is so open-ended. But any tips/advice you can think of would be appreciated.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

Math With Big Numbers
5 direct replies — Read more / Contribute
by AlligatorStamp
on Feb 14, 2019 at 15:32

    Hello. I am having trouble getting Perl to give me correct answers with large numbers. I am using Perl 5.14.2 on Windows.

    6^25 = 28430288029929701376.

    When I run the following:

    my $bigNumber = 6**25; printf ("%.f", $bigNumber);

    I get 28430288029929701000.

    Assuming that this lack of accuracy was due to 32-bit Perl's trouble with numbers above 2^53, I tried the following:

    use Math::BigInt; my $bigNumber = Math::BigInt->new(6**25); print "$bigNumber";

    which gives me 28430288029929700000

    I don't understand why BigInt is giving me an even less accurate answer.

LWP::Useragent doesn't work on certain HTTPS websites?
2 direct replies — Read more / Contribute
by sectokia
on Feb 14, 2019 at 06:37

    Hi wise monks, I have found that simple LWP get on HTTPS doesn't always work. Below is example on a server which it never seems to work on:

    use warnings; use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); my $res = $ua->get(''); print $res->content;

    The error I get is:

    Status read failed: Connection reset by peer at /usr/share/perl5/Net/H +TTP/ line 282.
    I had a look at that file and its over my head. Any ideas? Thanks!

Gtk3 and CSS styles
1 direct reply — Read more / Contribute
by Anonymous Monk
on Feb 13, 2019 at 10:31

    In Gtk2, I could apply a style to various widgets using RC strings. Under Gtk3, it's necessary to use CSS styles.

    I can't find any working examples of the new Perl code, besides one from 2012 which no longer works. (The site won't let me post the link.)

    I am able to apply a CSS style to a whole window, using the script below. But applying the style to a single widget (in this case, a Gtk3::TextView) does not work, and I can't see why it's wrong.

    #!/usr/bin/perl use strict; use diagnostics; use warnings; #use Glib qw(TRUE FALSE); use Gtk3 '-init'; # Set up CSS style my $provider = Gtk3::CssProvider->new(); # Why are these lines necessary? What function do they serve? my $display = Gtk3::Gdk::Display::get_default; my $screen = Gtk3::Gdk::Display::get_default_screen($display); Gtk3::StyleContext::add_provider_for_screen($screen, $provider, 600); # This applies the style to the whole window #my $theming = "* {\nbackground-color: #000000;\ncolor: #FF0000;\n}"; #$provider->load_from_data($theming, -1, undef); # But this doesn't apply the style (just) to the textview my $theming = "GtkTextView {\nbackground-color: #000000;\ncolor: #FF00 +00;\n}"; $provider->load_from_data($theming, -1, undef); # Open a Gtk3 window containing a Gtk3::TextView my $window = Gtk3::Window->new('toplevel'); $window->set_title('Hello world'); $window->set_position('center'); $window->set_default_size(600, 400); $window->signal_connect('delete-event' => sub { Gtk3->main_quit(); exit; }); my $scrollWin = Gtk3::ScrolledWindow->new(undef, undef); $window->add($scrollWin); my $textView = Gtk3::TextView->new; $scrollWin->add_with_viewport($textView); $textView->get_buffer()->set_text("Hello, world!"); $window->show_all(); # Start Gtk3's main loop Gtk3->main();
Adding text file data to hashes and array
4 direct replies — Read more / Contribute
by Tigor
on Feb 13, 2019 at 05:38

    I have a text file which contains fruits data which get sold every month as below

    file name is fruits.txt

    the text file data looks like as below.Please  get average of the values stored in array as well.I have tried harder to get the ouput but stuck.
    Fruit Jan feb mar apr Apple 40 45 50 54 orange 12 25 24 29 Pineapple 10 20 30 40

    I want output as below

    {apple => [40,45,50,54] Orange =>[12,25,24,29]}

    i have written a code for the above desired ouput.Please find my code below

    use strict; use warnings; my $filename = 'fruits.txt'; #opening the text file open(DATA, '<', $filename) or die "Could not open file '$filename' $!"; readline(DATA);#skipping the header my %data; while (<DATA>) { my @fields = split; my $key = join(' ', splice(@fields, 0, 2)); $data{$key} = \@fields; } for my $key (sort(keys(%data))) { printf("%s=> %s\n", $key, join(' ', @{$data{$key}})); }
On the road again with Gtk2 and PAR::Packer
4 direct replies — Read more / Contribute
by frazap
on Feb 13, 2019 at 03:00

    Some years ago I succeed in having Gtk2 script packed with PAR after including dll with --link option.

    These were Glib.dll, Gtk2.dll, Cairo.dll, Pango.dll.

    The trick that was working then was to include the above dll and to rename the the Glib, Gtk2, Pango folders in lib/auto to hide them from the search path.

    The dll were then taken from the par archive at shlib\MSWin32-x86-multi-thread-64int\ folder and the exe works.

    More details here

    The problem:

    I did a new perl install (strawberry perl v. 5.24) when my OS was changed. I compile the Gtk2 from source.

    I can't find these dll (Glib.dll, Gtk2.dll) in my new perl install, I have instead Glib.xs.dll, Gtk2.xs.dll etc. I did find the first group in my tmp folders under C:/Users... where par archives are extracted: I have copied these dll in c:/docs/perl_dll

    I tried to pack the following script
    use strict; use warnings; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; my $window = Gtk2::Window->new; $window->signal_connect (destroy => sub { Gtk2->main_quit; }); $window->show_all(); Gtk2->main;

    Using Shawn Laffan's or the -x option of pp without any dll included make an exe that crash

    Can't load 'C:\Users\rappazf\AppData\Local\Temp\par-...\778ce824.xs.dl +l' for module Pango: load_file:The specified module could not be found at + C:/strawberry/perl/lib/ line 193.
    objdump -x 778ce824.xs.dll | grep "DLL Name" gives DLL Name: libcairo-2.dll DLL Name: libglib-2.0-0.dll DLL Name: libgobject-2.0-0.dll DLL Name: libpango-1.0-0.dll DLL Name: libpangocairo-1.0-0.dll DLL Name: msvcrt.dll DLL Name: Cairo.xs.dll DLL Name: Glib.xs.dll DLL Name: KERNEL32.dll DLL Name: perl524.dll
    Adding the xs.dll with --link did change the errors, but the inclusion of the 4 xs.dll does not give a working exe.

    With these dll on the option for pp

    -l 'C:/strawberry/perl/site/lib/auto/Cairo/Cairo.xs.dll' -l 'C:/strawberry/perl/site/lib/auto/Glib/Glib.xs.dll' -l 'C:/strawberry/perl/site/lib/auto/Gtk2/Gtk2.xs.dll' -l 'C:/strawberry/perl/site/lib/auto/Pango/Pango.xs.dll' -l 'C:\docs\perl_dll\Pango.dll' -l 'C:\docs\perl_dll\Glib.dll' -l 'C:\docs\perl_dll\Gtk2.dll' -l 'C:\docs\perl_dll\Cairo.dll'
    Without hidding the lib/auto/Glib .../Gtk2 .../Pango I get the error
    GLib-GObject-CRITICAL **: g_boxed_type_register_static: assertion `g_t +ype_from_name (name) == 0' failed at C:/strawberry/perl/lib/DynaLoade line 210. cannot register alias Gtk2::Pango::Attribute for the unregistered type + (null) at C:/strawberry/perl/lib/ line 210. Compilation failed in require at script/ line 12. BEGIN failed--compilation aborted at script/ line 12.
    Renaming these folder and adding the dll above still gives the error below
    Can't locate loadable object for module Glib in @INC (@INC contains: C +:\Users\rappazf\AppData\Local\Temp\par-72617070617a66\cache-6d3232558 +24cd4b77983f4c61ece5aa2d701ac21\inc\lib C:\Users\rappazf\AppData\Loca +l\Temp\par-72617070617a66\cache-6d323255824cd4b77983f4c61ece5aa2d701a +c21\inc CODE(0x31e7e8c) CODE(0x31e8084)) at C:/strawberry/perl/site/l +ib/PAR/ line 99. Compilation failed in require at line 30. That's were 'use Gib +;' is called in and adding the Glib.dll, Gtk2.dll from the pe +rl_dll folder does not change this.

    Can someone explains the difference between Glib.dll and Glib.xs.dll ? How come the first group is missing from my perl tree and my scripts are still working ?

    How can I packed this script ?


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 imbibing at the Monastery: (3)
As of 2019-02-16 15:02 GMT
Find Nodes?
    Voting Booth?
    I use postfix dereferencing ...

    Results (95 votes). Check out past polls.