Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
Effect of redirecting output to /dev/null on $? value
2 direct replies — Read more / Contribute
by Special_K
on Aug 02, 2020 at 20:14

    For reference, this is somewhat of a follow up question to a question I asked previously:

    What is the impact on the value of $? if the output of an application is redirected to /dev/null? This is running under RHEL7.

    For example, suppose I have a perl script that calls a compiled executable that seg faults:

    `my_executable_that_seg_faults`; my $exit_value = $? >> 8; my $signal_num = $? & 127; my $dumped_core = $? & 128; printf("return value is %d\n", $?); printf("exit_value = $exit_value\n"); printf("signal_num = $signal_num\n"); printf("dumped_core = $dumped_core\n");

    Running the above code produces the following output:

    return value is 11 exit_value = 0 signal_num = 11 dumped_core = 0

    This output is consistent with what I would expect based on information found here related to the $? variable:

    However if I change the executable call above to the following:

    `my_executable_that_seg_faults > /dev/null`;

    The following output is produced:

    return value is 35584 exit_value = 139 signal_num = 0 dumped_core = 0

    The above output is significantly different even though the application still seg faulted. Here are my questions:

    1. Why did the value of $? completely change when I added the output redirect to /dev/null but kept everything else the same?

    2. Does the value of $? in the second case still contain the same information but encoded differently?

    I created a different application with an intentional segmentation fault and called it using the above perl script. It produced the exact same output, suggesting that this behavior is not dependent on the behavior of the called application.

Net Websocket Server
2 direct replies — Read more / Contribute
by alvise
on Aug 01, 2020 at 15:59
    Good evening Monks, referring to Net::Websocket::Server Perl module, which is the difference between utf8 and binary ?
    on_connect => sub { my ($serv, $conn) = @_; $conn->on( }, utf8 => sub { }, binary => sub { },
    since whatever I send() to the server, it is always received as utf8, even if I declare type='binary' in the JavaScript socket declaration.

    Thanks in advance.

Mojo::DOM exception handling help
4 direct replies — Read more / Contribute
by Anonymous Monk
on Aug 01, 2020 at 13:11

    Hello wise Monks who are always wiser than I am. I have several thousand html files that I'm parsing using Mojo::DOM. I've encountered a few older files that are surprisingly missing one the fields I'm trying to extract. Not many by enough to cause my script to error out. Here's the code I have:

    my $dom1 = Mojo::DOM->new( $$temp_content ); my $abstr = $dom1->at('div.abstract-content > p')->text; print "Abstract is: $abstr \n\n";

    Here's the what I tried to do to catch the exception (when abstract is null)

    my $abstr = $dom1->at('div.abstract-content > p' || 'not provided')- +>text;

    Here's the error that I'm getting: Can't call method "text" on an undefined value at /Volumes/HD-PATU3/ line 5.

    I've looked at the Mojo::DOM documentation and can't seem to find an answer. I've sure it's easy and being a nub I"m missing something simple. Thanks in advance for your wisdom and assistance

Getting code-ref of anonymous caller
1 direct reply — Read more / Contribute
by LanX
on Jul 31, 2020 at 11:13
    As the following demo shows is caller(1) reporting the name of the calling sub + package.

    In the case of a named subroutine one is able to get the code-ref of the caller by inspecting the STASH.

    But this doesn't work with anonymous subs.

    Caller claims they are part of the stash (here main::__ANON__ ) but accessing the associated code-ref fails. It most likely a newly autovivificated code-ref.

    Any better way to get the callers code-ref?

    First the output

    CURRENT SUB: CODE(0x24c9198) main::upper CODE(0x24c9198) CURRENT SUB: CODE(0x24e8568) main::__ANON__ CODE(0x24c0580) # <--- OOPS

    Here the demo:

    use strict; use warnings; use Data::Dump qw/pp dd/; use feature "current_sub","say"; sub lower { my %caller; package DB { @caller{ qw/ package filename line subroutine hasarg +s wantarray evaltext is_require hints bitmas +k hinthash / } = caller(1); }; my $caller_name = $caller{subroutine}; my $caller_ref =\&{$caller_name};; say $caller_name," ",$caller_ref; } # --- normal sub upper { say "CURRENT SUB: ",__SUB__; lower( "UPPER" ); } upper(); # --- ano sub my $ano = sub { say "CURRENT SUB: ",__SUB__; lower("ANNO"); }; $ano->();

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Fork and creating a thread exits the process
6 direct replies — Read more / Contribute
by fluks
on Jul 31, 2020 at 07:44
    for my $c (@car_links) { my $pid = fork(); if ($pid == 0) { # Reserve for 30 min. total. for (1..6) { reserve_car($c, $username, $password); sleep 300; } exit; } push @known_cars, $c; write_url_to_known_cars($c, $fh); }
    for my $c (@car_links) { threads->create(sub { threads->detach; # Reserve for 30 min. total. for (1..6) { reserve_car($c, $username, $password); sleep 300; } }); push @known_cars, $c; write_url_to_known_cars($c, $fh); }

    Both of these exit after fork() and threads->create() on Windows 10 using Strawberry Perl and ActiveState 5.26.3 and 5.28.1. What could be the reason and is it possible to fix this? Both versions work fine on Linux.

    I tried AnyEvent too, but I couldn't get it to work either and other event systems seem to need their on loops. The above code is already run in another loop, so I would like to stick with it.

Simple way to skip spaces and # comments
2 direct replies — Read more / Contribute
by leszekdubiel
on Jul 31, 2020 at 06:43

    I parse string using /gc and \G regexes. Current solution for skipping white characters and comments starting from # to the end of line is:

    $$a =~ /\G(?=(\s|#))(?:\s++|#.*+)++/gc;

    Look ahead ?= assertion is necessary, because zero-length matches cause problems. That solution unfortunatelly gives error:

    Complex regular subexpression recursion limit (32766) exceeded

    I have changes to such one:

    while ($$a =~ /\G(?:\s++|#.*+)/gc) {};

    but this is not elegant. I have changed "+" to "*" after "\s" and this also solves the problem, but I don't know why...

    $$a =~ /\G(?=(\s|#))(?:\s*+|#.*+)++/gc;

    Questions: (1) what is the better solution to strip white chars and commments (2) why * instead of + solves the problem?

How the auto-increment operator works?
4 direct replies — Read more / Contribute
by zapdos
on Jul 30, 2020 at 21:09

    With the example print ++($foo = "Az"); # prints "Ba" The logic in my head is that it printed uppercase "B" because it's the letter that comes after the uppercase "A", and it printed lowercase "a" because it's what comes after lowercase "z" going back to the first letter of the alphabet that's "a" since "z" is the lastest.

    So why print ++($foo = "zz"); prints "aaa"? It makes no sense to me. Shouldn't it print just "aa" since there's two z characters?

no ppm on ActiveState perl?
2 direct replies — Read more / Contribute
by guthrie
on Jul 30, 2020 at 20:39
    I am trying to port a perl program from linux to Windows, and it uses XML::XPath Trying to load it, most advice seems to be to use ppm, but AS doesn't seem to provide that anymore - is that right? And "pip install" fails, as I don't have a pip in the ASPerl installation. Trying to install it from CPAN gives lots of dmake errors like below.

    As does "cpan install"; fails with:

    " MANWAR/XML-XPath-1.44.tar.gz E:\PLang\ActivePerl\bin\perl.exe Makefile.PL INSTALLDIRS=site -- O +K Running make for M/MA/MANWAR/XML-XPath-1.44.tar.gz x[91m[DMAKE] error: the following arguments are required: command. +.[0m ... e:\Plang\Python\python36\scripts\dmake.exe -- NOT OK
    Lots of reports on this sort of thing, but I didn't find anything simple that works yet. Any hints? Should I dump AS perl and move to Strawberry? Please excuse any novice confusion here!
Use cases for 'sub Pckg::func { }' ?
4 direct replies — Read more / Contribute
by LanX
on Jul 30, 2020 at 19:08
    It's possible to define a fully qualified sub by adding the package to the name.

    So I played around with sub X::foo and noticed that sub resolution happens in the surrounding package.

    That looks consistent to me, it's analogue to BEGIN{ *X::foo = sub { } } where the anonymous sub carries its package context around.

    Hence calling foo() will work while bar() fails, because Data::Dump wasn't imported into X::

    use strict; use warnings; use Data::Dump qw/pp dd/; sub X::foo { pp(\@_) }; BEGIN { *X::baz = sub { pp(\@_) } }; package X; sub bar { pp(\@_) } ; foo(1..3); # [1, 2, 3] baz(1..3); # [1, 2, 3] bar(4..6); # Undefined subroutine &X::pp +called

    Question: What are the use cases of that pattern?

    The only thing which comes to mind is monkey patching a sub in another package without adding inner helper functions into that package.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

help with XS pointers
3 direct replies — Read more / Contribute
by Bpl
on Jul 30, 2020 at 18:01
    Hi monkers, in these days I was building an XS library, based on the librtlsdr library ( github link: ) for now, some functions works fine but the "rtlsdr_open" call gives me some problems. For starting the code (from the github library) referred to the call is:
    RTLSDR_API int rtlsdr_open(rtlsdr_dev_t **dev, uint32_t index); # where rtlsdr_dev_t is initialized as: typedef struct rtlsdr_dev rtlsdr_dev_t;
    I think that my implementation of rtlsdr_dev is wrong, in the XS code I wrote
    int rtlsdr_open(dev, index) RTLSDRDevice_T **dev uint32_t index # where RTLSDRDEVICE_T is typedef rtlsdr_dev_t RTLSDRDevice_T;
    and from the TYPEMAP file the structure are declared as:
    now, the problem is that, when I run this code:
    use SDR::RTLSDR qw(:all); # my library my $serial_number = "00000001"; # from dmesg output my $index = rtlsdr_get_index_by_serial( $serial_number ); # it returns + 0, this is not an error value so I think is good my $device = rtlsdr_get_device_name( $index ); print rtlsdr_open( $device , $index)
    I have the following error:
    SDR::RTLSDR::rtlsdr_close: dev is not of type RTLSDRDevice_TPtr at sdr line 7.
    Now, I think that the result is related to some pointer error but also with:
    print rtlsdr_open( \$device , $index)
    I had the same error code. hope in some helps Thanks Edoardo M.

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2020-08-04 05:47 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (31 votes). Check out past polls.