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

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
[Win32] Why did they add '-perlio'
2 direct replies — Read more / Contribute
by syphilis
on Jun 22, 2022 at 09:25

    As I mentioned earlier my unthreaded builds of perl were, upon installation, being assigned an @INC that was empty.
    Well .... silly me .... I had assumed that an archname of 'MSWin32-x64-multi-thread' would, upon becoming neither 'multi' nor threaded, change to simply 'MSWin32-x64'.
    But, no - the win32/makefiles decree that it becomes 'MSWin32-x64-perlio' if USE_PERLIO is defined.
    The fact that USE_PERLIO is also defined for the multi-threaded build is apparently unworthy of mention, even though defining USE_PERLIO is equally optional for both multi-thread and non-multi-thread builds of perl, AFAICS.

    So why do they add '-perlio' for the unthreaded build only ?
    Is there a good reason ? (Not that one is required, of course ... heaven forbid !!)

    UPDATE - to elaborate a little on how this led to @INC being empty:
    In these unthreaded builds I was specifying (eg) INST_ARCH=MSWin32-x64 and that didn't match the actual designated archname of MSWin32-x64-perlio .
    Now, I don't exactly know why that resulted in an empty @INC, but the problem went away as soon as I changed to specifying INST_ARCH=MSWin32-x64-perlio .

using a variable with require
5 direct replies — Read more / Contribute
by geoffleach
on Jun 21, 2022 at 23:42
    I would like to be able to write code that requires a package that is specified at runtime and then execute a subroutine in that package. For example:
    $name = 'SomeModule"; require $name; $name::somesub();

    Is there any way to do this?

Can't locate in @INC
4 direct replies — Read more / Contribute
by powerperl
on Jun 21, 2022 at 15:30
    perl -V Can't locate in @INC (you may need to install the Config module) (@INC contains: ). BEGIN failed--compilation aborted. Additional information : @INC has paths of two versions of perl. Path mentioned in @INC has but still it is throwing this error when I execute this command. Similar problem can be recreated with other examples where perl goes for search in @INC. Note that It works fine when I export same paths from @INC to PERL5LIB.
recursive call of current script
5 direct replies — Read more / Contribute
by feumw
on Jun 21, 2022 at 06:46
    We have a script which has a for-loop which kinda works like a switch. Depending on the parameter $sFunction we're running different code.
    for ( $sFunction ) { /test1/ && do { # .... }; /test2/ && do { # .... }; }
    I would like to call a different function in this loop before like
    for ( $sFunction ) { /test1/ && do { # .... }; /test2/ && do { # here I want to call /test1/ part above ... # ... and then do different stuff here }; }
    is there a clean way to do this rather then executing this script with a different arguement again?
Kronecker Product
2 direct replies — Read more / Contribute
by choroba
on Jun 21, 2022 at 01:45
    Spoiler alert: If you participate in The Weekly Challenge, don't read further if you haven't solved week 170 yet.

    Kronecker product is a matrix operation that uses elements of one matrix to multiply the second matrix. Mohammad shows this example:

    A = [ 1 2 ] [ 3 4 ] B = [ 5 6 ] [ 7 8 ] A x B = [ 1 x [ 5 6 ] 2 x [ 5 6 ] ] [ [ 7 8 ] [ 7 8 ] ] [ 3 x [ 5 6 ] 4 x [ 5 6 ] ] [ [ 7 8 ] [ 7 8 ] ] = [ 1x5 1x6 2x5 2x6 ] [ 1x7 1x8 2x7 2x8 ] [ 3x5 3x6 4x5 4x6 ] [ 3x7 3x8 4x7 4x8 ] = [ 5 6 10 12 ] [ 7 8 14 16 ] [ 15 18 20 24 ] [ 21 24 28 32 ]

    When I saw matrices, I immediately thought PDL. After finding a solution, I tried searching for existing solutions, and found the following at Rosetta Code:

    #!/usr/bin/perl use strict; use warnings; use PDL; use PDL::NiceSlice; sub kron{ my $A = shift; my $B = shift; my ($r0, $c0) = $A->dims; my ($r1, $c1) = $B->dims; my $kron = zeroes($r0 * $r1, $c0 * $c1); for(my $i = 0; $i < $r0; ++$i){ for(my $j = 0; $j < $c0; ++$j){ $kron( ($i * $r1) : (($i + 1) * $r1 - 1), ($j * $c1) : (($j + 1) * $c1 - 1) ) .= $A($i,$j) * $B; } } return $kron; }

    Wait, loops? The whole point of PDL is to hide loops. And indeed, my solution doesn't involve them. Also, a benchmark shows my solution is more than twice faster than the Rosetta Code one.

    I'm far from an expert on PDL or matrices in general. But it seems we can easily multiply each element in a matrix by the same number, but it's not so easy to multiply them by different numbers. But we can multiply each element by a matrix, so we just need to "inflate" the matrix, so instead of

    [ 1 2 ] [ 3 4 ]

    we'd have

    [ 1 1 ] [ 2 2 ] [ 1 1 ] [ 2 2 ] [ 3 3 ] [ 4 4 ] [ 3 3 ] [ 4 4 ]

    That's what dummy does (two dummies, in fact, one in each dimension). Multiplying this with the second matrix gives us a result that has all the expected numbers, but a bit different dimensions.

    [ 5 6 ] [ 10 12 ] [ 7 8 ] [ 14 16 ] [ 15 18 ] [ 20 24 ] [ 21 24 ] [ 28 32 ]

    Fortunately, PDL has all the needed methods to reshuffle the submatrices into the expected result.

    It seems correct (it passes the test shown in the challenge and in the Wikipedia page), but my PDL is not so strong. Maybe it can be further improved?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
How to inject role to Caller Module?
1 direct reply — Read more / Contribute
by smarthacker67
on Jun 20, 2022 at 14:08
    Hello Respected Monks I am seeking some help question.
    package Validation; use Moose::Role; sub check_entity { my ( $self, $db_id ) = @_; #some logic my $found_db = sub { #logic verify id present in db return 1; } return $found_db; }

    I have a module that helps me write clean modules using the following `package MyApp::Moose;`. I tried searching a lot and am not sure exactly how I can inject the above role to the caller (so that it will get consumed) and the caller can have access to `check_entity` method.

    I refered

    Note:- I cant create an object of the caller since it has some required entity (*object may not be needed to inject role I believe)

    But unfortunately, I couldn't able to figure out the right way, I am sure there must be a simple way to do it which I am missing out. Also want to do similar for multiple roles in the future once I develop them.

    package MyApp::Moose; use strict; use warnings; use namespace::autoclean; use Hook::AfterRuntime; use Import::Into; use Moose (); use Clone 'clone'; sub import { my ($class, @opts) = @_; my $caller = caller; my %opt = map { $_ => 1 } @opts; strict->import::into($caller); warnings->import(); Clone->import::into($caller,'clone'); if($opt{role}) { require Moose::Role; Moose::Role->import({into=>$caller}); } else { Moose->import({into=>$caller}); after_runtime { $caller->meta->make_immutable(); }; } namespace::autoclean->import( -cleanee => $caller, ); return; } 1;

    * Currently Using above in code like this.

    package MyApp::Process; use MyApp::Moose; sub some_method { my ($self, $db_id) = @_; # I want to call like this $self->check_entity($db_id) || return; } 1;

    I really appreciate any help you can provide.

    I was hoping I could inject it directly to the caller using but after several attempts, I was unable to accomplish

    The same question was Posted on StackOverflow.

Indexing failed for installing Tk
1 direct reply — Read more / Contribute
by Joseph
on Jun 20, 2022 at 00:28

    I'm new to Perl. I have a problem with installing Tk.

    The error message:

    Indexing failed! at C:/Strawberry/perl/vendor/lib/CPAN/ line 77, <DATA> line 69. CPAN::SQLite setup failed at C:/Strawberry/perl/vendor/lib/CPAN/SQLite/ line 325, <DATA> line 69. system C:\Strawberry\perl\bin\perl.exe -MCPAN::SQLite::META=setup,update,check -e setup failed: 6400 at C:\Strawberry\perl\vendor\lib/CPAN/SQLite/ line 318.

    I can't find a solution on google. Hope anyone can help me.


Encoding of emoji character
2 direct replies — Read more / Contribute
by dcunningham
on Jun 19, 2022 at 23:50

    Esteemed monks, I have a problem with encoding of an emoji in perl. The string "Test 😀" is read from a MySQL database table which has encoding latin1. Obviously that's not a UTF-8 character set, but my console seems smart enough to detect the intended output, as a plain "select * from table" on the "mysql" client displays the "grinning face" emoji correctly.

    Then my perl (version 5.26) program logs the text to a log file, and again running "tail -f" on the log file displays the emoji in the text correctly. I also log the bytes using sprintf( "%vX", $text) and it prints "". So the bytes for the emoji are there, in "F0.9F.98.80".

    Then the text is JSON encoded (using the JSON library), and sent using $conn->send_utf8() to a websocket client using Net::WebSocket::Server, however the websocket client (running in a web browser) receives "Test Ÿ˜€". I've tried encode( 'UTF-8', $text ) which did not fix the problem.

    The whole subject of character encoding is not an easy one, and mixing MySQL with Perl with websockets (with JSON for good measure) has made it tricky to tell where the problem is.

    Can anyone help find why the websocket client doesn't receive the emoji correctly please?
How do I use the map command for this?
5 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 19, 2022 at 09:01
    Hello Monks!
    I need to do the following:
    In a document with the following format:
    >id1|various annotation1 sequence of characters1 >id2|various annotation2 sequence of characters2 ... >idN|various annotationN sequence of charactersN
    I want to group the lines that have the same various annotation and sequence but not the same id into one line, like this:
    >id1-id15-id22|various annotation1 sequence of characters1 ...
    When the various annotation is the same, then the sequence is also the same.
    I am pretty sure I need to use the map command, but I am a bit unsure how I would handle this. So far I have used it when I have 2 things to compare, e.g. only the id that would change and the sequence that could be repeated, and I was grouping based on the sequence. Like this:
    use strict; use warnings; my %res; while (<>) { chomp; my ( $name, $rest ) = split /\t/; push @{ $res{$name} }, $rest; } for ( sort keys %res ) { print "$_:", join( ",", @{ $res{$_} } ); print "\n"; }

    But now, I have 3 things instead.
substr out of str error - but why?
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 17, 2022 at 20:19
    Hello fellow monks!
    I keep trying to understand why I am getting an exception error although it does not seem to be some kind of mistake. So, I have a script that is reading a file and storing some number ranges, which I will use to re-structure a string.
    Initial string is something like:
    PEGYNDRQAVNGSFYKLTFAPTFKVGSIGDFFSRPEIRFYTSWMDWSKKLNNYA ......................................................

    and, with my script, I will replace the '.' with other characters. The script seems to stumble upon an error in this chunk:
    while ($rest=~/\<REGION seq\_beg\=\"(\d+)\"\s+pdb\_beg\=\"\d+\"\s+seq\ +_end\=\"(\d+)\"\s+pdb\_end\=\"\d+\"\s+type\=\"(\w+)\"\/\>/mg) { $start=$1; $end=$2; $type=$3; $TM_part_to_store = "$start-$end"; $length_part="$start-$end"; if($type==1) { substr($topo_initial, ($start-1), ($end-$start+1), ($side1 x ( +$end-$start+1))); } elsif($type==2) { substr($topo_initial, ($start-1), ($end-$start+1), ($opposite{ +$side1} x ($end-$start+1))); } elsif($type eq 'B') { push @all_TMs_line, $TM_part_to_store; } else { substr($topo_initial, ($start-1), ($end-$start+1), ('U' x ($en +d-$start+1))); } }

    where I am getting the info I need and change the '.' accordingly. For debugging, I made the script print the string length and each $start and $end, to see which substring is out of bounds, in the form of string length <TAB> range
    Weirdly enough, in the string that it fails, which has a length of 413 characters, the debugging prints the following:
    413 1-6 413 7-15 413 16-34 413 35-46 413 47-53 413 54-67 413 68-83 413 84-85 413 86-95 413 96-112 413 113-118 413 119-133 413 134-142 413 143-153 413 154-160 413 161-174 413 175-181 413 182-186 413 187-193 413 194-217 413 218-224 413 225-237 413 238-244 413 245-266 413 267-273 413 274-282 413 283-290 413 291-304 413 305-311 413 312-320 413 321-328 413 329-347 413 348-355 413 356-369 substr outside of string at

    As you can see, for some weird reason, and although my string's length is 413 chars and there are more ranges after 356-369, it exits with this error.
    Any ideas?

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 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 studying the Monastery: (3)
As of 2022-06-26 18:08 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (86 votes). Check out past polls.