Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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.

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.

Post a new question!

User Questions
Repeatable rand() and keys() for debugging
1 direct reply — Read more / Contribute
by ibm1620
on Dec 09, 2022 at 12:14
    Hello Perlmonks,

    I'm having trouble debugging a heavily random program because I'm unable to force it to repeat its behavior.

    After reading perlrun and web-searching a bit I concluded that the following snippet at the top of the program might do the trick:

    #!/usr/bin/env perl use v5.36; BEGIN { srand(1); # rand generator is repeatable $ENV{PERL_PERTURB_KEYS}=0; # traversing keys is repeatable $ENV{PERL_HASH_SEED}=1; # hashing is repeatable? }
    In addition to calling rand() to choose an array index, it also traverses lists of key(%hash). As far as I know, those are the only places where randomness can occur. (No hashes are modified after they're constructed at initialization.)

    I gather from perldoc perlrun ("this mode is as close to pre 5.18 behavior as you can get.") that perhaps there are no guarantees anymore?

    Can anyone suggest what I might be doing wrong or overlooking?

How do I get AnyEvent::Handle to munge arrow keys?
No replies — Read more | Post response
by isync
on Dec 09, 2022 at 11:45
    Dear Monks,
    I'm working on implementing a simple data entry thingy and connect to an AnyEvent::Handle via the telnet command-line program. I'm trying to get it to not just "read" on getting a newline, but also on pressing arrow keys. I know that I can register a different EOL character via ::Handle's push_read() method, but I haven't got it to work on my terminal, btw. which is Ubuntu's default, where I then run the default telnet command.

    What *does* work is a procedure like:
    1. I enter a "right arrow",
    2. the terminal then echoes the char "^[[C") and
    3. I press ENTER after that,
    4. the "special arrow char" is transmitted.
    Of course, as ::Handle by default operates on the newline/carriage-return and transmits all the stuff before that.
webperl - DOM manipulation and event example
1 direct reply — Read more / Contribute
by sectokia
on Dec 08, 2022 at 16:58

    Hi monks,

    Does anyone know of, or have any examples of webperl that show how to manipulate the DOM and set call backs to perl subs for browser events?

    I tried google around but all the examples I could find were just basic things like scripts that run at a set position of a HTML file and have their output embedded into the file.


How can I find installed packages in a broken Perlbrew?
7 direct replies — Read more / Contribute
by monsignor
on Dec 07, 2022 at 04:02

    When I upgraded my system I had to "deactivate" perlbrew by renaming ~/.perl5 and removing it from the path.

    Is there an easy way to find out what packages I had installed? The easiest way for me to put things back is to do a fresh install of Perlbrew and then reinstall my packages.

What's the point of this 'overload' idiom?
3 direct replies — Read more / Contribute
by Cody Fendant
on Dec 06, 2022 at 23:23

    I'm working with some code which frequently uses this idiom I guess you'd call it?

    use overload '""' => sub {shift->name;};

    So I've read the Perldoc and it has this example:

    package Number; use overload [snipped] '""' => sub { ...; };

    which says it's "an anonymous subroutine to implement stringification: this is called whenever an object blessed into the package Number is used in a string context (this subroutine might, for example, return the number as a Roman numeral)."

    So I guess, something like this?

    ### example package package Number; my $self = {}; sub new { my $class = shift(); $self->{decimal} = shift(); return bless( $self, $class ); } my @roman_numbers = qw[ undef I II III IV V VI VII VIII IX X ]; use overload '""' => sub { return $roman_numbers[ $self->{decimal} ] }; 1; ### main package main; my $instance = new Number(3); print $instance; ### it will print 'III'

    So, two questions:

    1. Why would I want this particularly?
    2. what's the specific thing in use in the code I'm seeing, use overload '""' => sub {shift->name;}; and why would I want that?
Accessing dotted tags with Mojo::DOM
1 direct reply — Read more / Contribute
by Fletch
on Dec 06, 2022 at 13:03

    Other day was monkeying with something trying to parse and grobble through an XML file (specifically a jenkins task configuration file) using Mojo::DOM. The file parsed fine, and I can work my way down to the tag in question ( using something like walking children on the parent element. However I'm presuming that the dots are being taken as CSS class names and neither find nor at will return the element node I'm trying to get. Tried a couple of "obvious" things like using \Q and manually backslashing the dots, and passing a regex qr// as well; neither to any avail.

    This was a one-off excuse to play experiment with Mojo::DOM and I've since worked around using XML::Twig instead. That being said though I'm still curious if there'd be any way to do this with the former.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Modules 101 :: Using a Local Module from CPAN
5 direct replies — Read more / Contribute
by redapplesonly
on Dec 06, 2022 at 12:10

    Hi Perl Monks,

    So I'm a Perl newbie, making lots of rookie mistakes. I've made two posts on this website* already in my fumbling attempts to solve a singular problem: I need a way to ensure only one instance of my Perl script can run at a time. Stevieb was kind enough to recommend his Script::Singleton solution, but I was too much of a Perl beginner to realize what he'd handed to me at first.

    It was a bolt of lightning when I realized that Script::Singleton was another script, and all I needed to do was copy that script from the site**, save it in a local directory, and then have my script run his script. Easy Peasy. Except, I'm struggling at that "have my script run his script part." (I think this is prob something you Perl Ninja Black Belts do all the time, so you can discuss the process in shorthand. But for newbs like me, the process is confusing.)

    (FYI, I'm developing on a Ubuntu 20.04 machine, using Perl 5.30.0)

    Let me show you actual code. First, here's Stevieb's script:

    package Script::Singleton; use strict; use warnings; use Cwd qw(abs_path); use IPC::Shareable; our $VERSION = '0.03'; sub import { my ($class, %params) = @_; $params{glue} = abs_path((caller())[1]) if ! exists $params{glue}; IPC::Shareable->singleton($params{glue}, $params{warn}); } sub __placeholder {} 1;

    I've saved this script in the same directory as, plus run a chmod 775 on it, just to be sure. Stevieb's script should be good to go.

    Now for the part where I'm all thumbs. I've read through a few "Perl Module" tutorials, trying to puzzle how my script can use or require the module script. Here was what I thought would be the best attempt:

    #!/usr/bin/perl use warnings; use strict; require '/home/demo/pullWorkload/scpLRSIControllerDir/toys/Singleton.p +m'; use Script::Singleton; # Line 6 :: Actually run the code in "Si" ??? package main; print "Starting script...\n"; sleep(10); print "I can use \"!\".\n";

    But script output is:

    me@/path/to/my/scripts/$ ./runTest.perl Can't locate Script/ in @INC (you may need to install the +Script::Singleton module) (@INC contains: /etc/perl /usr/local/lib/x8 +6_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_ +64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/pe +rl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64 +-linux-gnu/perl-base) at ./toy02.perl line 6. BEGIN failed--compilation aborted at ./runTest.perl line 6. me@/path/to/my/scripts/$

    Okay, obviously my script is searching throughout all the directories in @INC for, but not looking in the local directory. Frustrating.

    If things were working correctly, I'd hope that the first time the script runs, it would execute Stevieb's code, confirm that it is the only instance of this script running, then sleep for 10 seconds. In theory, a second execution of my script before those 10 seconds are up should... I dunno, not execute at all?

    If I comment out Line 6 (which I thought was necessary to actually run Stevieb's code), then my script runs with no errors. But you can run multiple instances of my script at the same time, which is no bueno.

    Anyway, I apologize for (A) essentially asking you guys the same question three times in a row, and (B) asking a question which is probably Perl 101 to you guys. I do research my questions before I post them, FWIW. Any comments or criticism is appreciated. Thank you!

    *Full Disclosure :: My previous posts are: Ensure Only One Instance of Your Script is Running... with 'ps -ef' ? Mechanism for ensuring only one instance of a Perl script can only run? **The CPAN site where I found Stevieb's code is located:
Unsigned 64-bit integer as Judy key
4 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 06, 2022 at 11:42
    $ perl -MJudy::L=Set -e 'Set($j, 1<<63, 123)' Coercing -9223372036854775808 to 0. Can't use negative values as keys. + at -e line 1.

    Can anyone please test if they get same error? Argument can't be treated as signed int by design, can it? Or is my system/Perl/Judy broken?

Extract delimited words from string
5 direct replies — Read more / Contribute
by Anonymous Monk
on Dec 06, 2022 at 04:44
    how can I parse strings like this below to extract the words delimited by quotation marks?
    50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 A255" 0 70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 G12 B12 + A255" 0
    Thanks for your help!
How to get Tooltips working in Win32::GUI::TreeView?
1 direct reply — Read more / Contribute
by The_Dj
on Dec 05, 2022 at 22:46

    Greetings Monks!

    I want to offer a tooltip on TreeView items.

    I already use Win32::GUI::Tooltip on some buttons and text boxes,
    but I can't figure out how to point a tooltip to the nodes in a TreeView :-(

    Bonus points if there is a way to update the tooltip after creation (So I can include 'last clicked: <date>' text

    Thanks for any help.

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":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-12-09 19:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found