http://qs1969.pair.com?node_id=479

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
How to create and install a module compatible with both UTF8 and Perl 5.8.3 without using non-core modules?
2 direct replies — Read more / Contribute
by Polyglot
on Dec 02, 2023 at 12:04
    My attempt at launching my module belly-flopped. I've learned that even though Test::More is part of Perl core going back to v5.6.2, Test::More::UTF8 was never part of Perl's core. Furthermore, Test2, said to be unicode compatible, only made it to core in Perl 5.25.1.

    My module does not need to use unicode. If the unicode comments and POD were stripped out of it, it would not even require "use utf8;" as there is no unicode in its actual code. But because it is for utf8 and the tests, to be meaningful, incorporate uft8 characters, the entire module fails to install on perl systems which do not already have the non-core module "Test::More::UTF8."

    In order to make the installation as easy and fuss-free as possible, and more space-conserving, too, I don't want to require the installation of non-core modules. If there is not a better way to do this, my next version release will abandon all of those useful tests and put in one or more tests which require no unicode at all--just a "free pass" so to speak. This way, at least, the install would be able to complete.

    So the question remains, is it even possible to create, and install, a module designed for Perl 5.8.3 compatibility, that provides UTF8 functions, and that does not require any non-core modules--and do this with reasonable UTF8-based tests?


    UPDATE:

    I think I've managed to find a hackish solution that should get me by for now. I've dumped once again (I should not have gone back to it after dropping it the first time) the "Test::More::UTF8" package earlier thought to be a "solution" for the lack of UTF8 compatibility of Test::More...it wasn't, and even when the package was manually installed (not part of core), it tended to generate some inexplicable 'wide character' warnings. So, I went with a pure-ASCII solution, no need even for "use utf8" in the testing script. Instead of testing on actual UTF8 characters, the script now tests on the hexadecimal codepoints returned from the module. If the correct codepoint is returned, the function can legitimately be considered to be installed and functioning. It would have been nice to test on real unicode, but oh well...the module will still work just fine, I'm sure.

    Blessings,

    ~Polyglot~

Converting Unicode
3 direct replies — Read more / Contribute
by BernieC
on Dec 01, 2023 at 18:20
    I can't get unicode to work. I have a file that , mixed in with regular text, there are the unicode characters for open quote, close quote and apostrophe. i've been trying to write a little program to replace them with non-unicode characters {" " and ,}. I've tried this test program
    these are the constants from cryptfix # use constant APOSTROPHE => "" ; # use constant OPENQUOTE => "" ; # use constant CLOSEQUOTE => "" ; # use constant COMMA => "" ; # This is the version from a hex dump of the crypt text file use constant APOSTROPHE => "\x{e2}\x{80}\x{22}" ; use constant OPENQUOTE => "\x{e2}\x{80}\x{90}" ; use constant CLOSEQUOTE => "\x{e2}\x{80}\x{9d}" ; use constant COMMA => "" ; unless (@ARGV) { die "usage: <crypts-text-file>\n" ; } open(CRYPTS, "<", $ARGV[0]) or die "Can't open $ARGV[0]: $!" ; while (my $line = <CRYPTS>) { say "apostrophe in line $." if $line =~ /@{[APOSTROPHE]}/; say "open quote in line $." if $line =~ /@{[OPENQUOTE]}/; say "close quote in line $." if $line =~ /@{[CLOSEQUOTE]}/; }
    and I feed it the text file with the unicode characters in it and it never finds any. I'm not sure what I'm getting wrong.
OpenAPI generator issue
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 30, 2023 at 06:44
    I have generated a perl client for a rest api with openapi generator. The slimmed down AuthorizeRequestDTO class is :
    package WWW::OpenAPIClient::Object::AuthorizeRequestDTO; require 5.6.0; use strict; use warnings; use utf8; use JSON qw(decode_json); use Data::Dumper; use Module::Runtime qw(use_module); use Log::Any qw($log); use Date::Parse; use DateTime; use base ("Class::Accessor", "Class::Data::Inheritable"); __PACKAGE__->mk_classdata('attribute_map' => {}); __PACKAGE__->mk_classdata('openapi_types' => {}); __PACKAGE__->mk_classdata('method_documentation' => {}); __PACKAGE__->mk_classdata('class_documentation' => {}); # new plain object sub new { my ($class, %args) = @_; my $self = bless {}, $class; $self->init(%args); return $self; } # initialize the object sub init { my ($self, %args) = @_; foreach my $attribute (keys %{$self->attribute_map}) { my $args_key = $self->attribute_map->{$attribute}; $self->$attribute( $args{ $args_key } ); } } __PACKAGE__->method_documentation({ 'username' => { datatype => 'string', base_name => 'Username', description => '', format => '', read_only => '', }, 'password' => { datatype => 'string', base_name => 'Password', description => '', format => '', read_only => '', }, }); __PACKAGE__->openapi_types( { 'username' => 'string', 'password' => 'string' } ); __PACKAGE__->attribute_map( { 'username' => 'USERNAME', 'password' => 'PASSWORD' } ); __PACKAGE__->mk_accessors(keys %{__PACKAGE__->attribute_map}); 1;
    I'm trying to create a new object by setting the username and password fields but they always come up undefined !!
    my %h= ('username' => 'TEST_USER','password' => 'dsds'); my $authobj=WWW::OpenAPIClient::Object::AuthorizeRequestDTO->new(%h); print Dumper $authobj; $VAR1 = bless( { 'username' => undef, 'password' => undef }, 'WWW::OpenAPIClient::Object::AuthorizeRequestDTO' );
    The init method does
    $self->$attribute
    what does that mean ? that it calls a $self->update() and a $self->password() methods? But these do not exist in the code!They are only described in __PACKAGE__->method_documentation
    Am I missing something or hasn't the openapi generator generated those two methods?
Strange error when using JSON module "XS.c: loadable library and perl binaries are mismatched"
2 direct replies — Read more / Contribute
by WithABeard
on Nov 30, 2023 at 02:42

    I get this weird error when using the JSON or JSON::XS module:

    $ perl -MJSON -e ''

    XS.c: loadable library and perl binaries are mismatched (got first handshake key 0xeb80080, needed 0xf380080)

    And the same error for perl -MJSON::XS -e ''

    perl -v This is perl 5, version 38, subversion 0 (v5.38.0) built for x86_64-li +nux-thread-multi ...

    What could cause this, and what can I do to fix it?

perl interpreter must be named my_perl?
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 30, 2023 at 00:53

    Hi Monks,

    Today I've started to study perlembed, Here is the first code example in Perlembed:

    #include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution * +/ static PerlInterpreter *my_perl; /*** The Perl interpreter *** +/ int main(int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); exit(EXIT_SUCCESS); }
    Ican build it successfully, and it works as expected.(windows 10, strawberry perl 5.32) But if I change interpreter variable name to his_perl, it doesn't work!
    #include <EXTERN.h> #include <perl.h> static PerlInterpreter *his_perl ; /*** The Perl interpreter * +**/ int main(int argc, char **argv, char **env) { PERL_SYS_INIT3(&argc,&argv,&env); PERL_SYS_TERM(); his_perl = perl_alloc(); perl_construct(his_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(his_perl, NULL, argc, argv, (char **)NULL); perl_run(his_perl); perl_destruct(his_perl); perl_free(his_perl); exit(EXIT_SUCCESS); }
    It can't be build, and complains identifier "my_perl" is undefined. I notice in perl.h aTHX hard code define to my_perl, I guess that is the reason why his_perl version can't be built. But why is this name(my_perl) so important, that it should write it into perl.h? Please enlighten me.
sending raw soap envelope with soap::lite
1 direct reply — Read more / Contribute
by ffrost
on Nov 29, 2023 at 12:26

    Hoping this is an easy problem to solve as my attempts are failing miserably. Using the AI bots and Google only seem to create solutions where the output is all mungled up or the XML elements get repeated, or worse new XML elements are added.

    In short, I'm creating a specific soap envelope I need to send to the endpoint web service. The reason is due to complexity and the inability for SOAP::Lite to render the output correctly. This also provides more control for changes as they occur. But when I try to make the call to the web service and send the envelope it seems to be sending the data modified, or the method call is not correct, etc.

    I'll post an example below and hopefully someone has a 'duh' answer I seem to be missing.

    use SOAP::Lite; use SOAP::Lite on_action => sub {sprintf '%s/%s',@_}; $proxy = 'https://proxy.com/2.3'; $uri = 'https://uri.dataservices.training'; $method = 'methodCall'; $soap = SOAP::Lite ->proxy($proxy) ->uri($uri); $soapenv = <<'EOF'; <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope ... (bunch of NS values)> <soap:Header> (bunch of header settings here) </soap:Header> <soap:Body> (bunch of body settings here) </soap:Body> </soap:Envelope> EOF $results = $soap->call("$uri/$method",$soapenv);

    This is resulting in the error "Element ..($uri/$method) ... can't be allowed in valid XML message.

Compare two strings of same length, character-by-character
5 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 28, 2023 at 16:51
    Hi Monks,
    I have two strings, of the same length, and I want to check how many of the characters are the same.
    Example:
    $str1='LFGSLSIIVAHHM'; $str2='LFGSLSIIVSHHM';

    I don't want to do LCSS, because it will only give me LFGSLSIIV, although we also have HHM. What I have done is to split each string and then compare the characters in each position, increase a 'correct' counter by 1 if they are the same and then compare the number of correct characters to the length of the string (to see what % of the string was the exact match).
    I was wondering if there would be any quicker solution to this, with a module or something that I might not be familiar with.
Trying to translate overflowing JS code to Perl
3 direct replies — Read more / Contribute
by bliako
on Nov 28, 2023 at 13:46

    Wise Monks,

    I am trying to translate some JS code into Perl. The problem is that I think said JS code does not handle well overflows when doing a left shift and it calculates the result (let's say a checksum) wrongly. But this is now the result and I must emulate that result in my Perl translation. I can not fix the JS code, it is not mine. Question is: how?

    Here is the gist of it assuming a 64bit Linux console with node.js installed and bash:

    node -e 'console.log(1<<30);' 1073741824 echo $((1<<30)) 1073741824 # same result perl -e 'print 1<<30, "\n"' 1073741824 # same result node -e 'console.log(1<<31);' -2147483648 # overflow in 32bit! echo $((1<<31)) 2147483648 # correct result perl -e 'print 1<<31, "\n"' 2147483648 # same result # to obtain the overflow in bash echo $((1<<63)) -9223372036854775808 # overflow in 64bit # still perl does not perl -e 'print 1<<63, "\n"' 9223372036854775808 perl -e 'print 1<<64, "\n"' 0 # gotcha! # this is when I realised the problem: node -e 'console.log(1169367104<<5);' -1234958336 echo $((1169367104<<5)) 37419747328 perl -e 'print 1169367104<<5, "\n"' 37419747328 # same result

    (note: if you don't have node.js open your browser, open its 'web developer tools', go to tab 'console' and just paste that bit of harmless js code

    So, I am asking: how can I make perl print -1234958336 when left-shifting 1169367104<<5 ?

    use Test::More; is(1169367104<<5, -1234958336, "simulated JS 32bit shift."); done_testing;

    bw, bliako

    Edit: I have asked a more general question about JS/C/Perl integer overflows at https://stackoverflow.com/questions/77584450/emulate-javascripts-32bit-signed-integer-arithmetic-to-c-or-perl-some-discr

Starman "Server closed connection without sending any data back"
2 direct replies — Read more / Contribute
by nikosv
on Nov 28, 2023 at 05:17
    I have setup a plack psgi application tha works with cgi files. When I start starman server from the shell of the user 'starman' the service runs fine and responds to requests.
    When I try to set it up as a systemd Unit (Centos) , the client gets an error of "Server closed connection without sending any data back" without emitting any data.OS is Centos7. Can't find anything in the log files,expect from dmseg which has error :
    111362894.832068 starman master 24776: segfault at c1 ip 00007f29bd8d2057 sp
     00007fff6ba44168 error 4 in XS.so7f29bd8cf000+15000
    
    The service is set up as :
    [Unit] Description=StarmanService [Service] Type=simple User=starman Group=starman ExecStart=/home/starman/perl5/bin/starman -E debug --ssl-key=/home/sta +rman/starm anpem+5-key.pem --ssl-cert=/home/starman/starmanpem+5.pem --listen :50 +00:ssl --w orkers=1 /home/starman/app.psgi --access-log /home/starman/access.log +--user=sta rman --error-log /home/starman/error1.log Restart=always WorkingDirectory=/home/starman/cgi-bin [Install] WantedBy=multi-user.target
How to create and name module tarball for CPAN
3 direct replies — Read more / Contribute
by Polyglot
on Nov 27, 2023 at 13:42
    Another simple newbie CPAN question:

    What should I name my tarball for upload to CPAN?

    I have chosen to give the package I am creating a home at Regexp::CharClasses::Thai on CPAN, so the final filename in that tree is simply "Thai.pm". However, obviously it needs to find its way past the first two levels in that tree...so I'm assuming that I should name it "Regexp-CharClasses-Thai.tar" (which contains the tarball made of the entire package and its associated files). Would this be a correct assumption? Alternatively, should it be compressed as well, i.e. "Regexp-CharClasses-Thai.tar.gz" OR "Regexp-CharClasses-Thai.tgz"?

    Also, do I create the tarball to include the directories './Regexp/CharClasses/Thai/', or just the final directory (the one with the MakeFile.PL, etc., in this case /Thai/), or something else, e.g. no prior directories at all, just the one with the package files and subdirs?

    Note that I have already checked, and found no specifics on how to name or structure the tarball (it's as if all the documentation assumes a root-level module) in any of these 'official' places:

    I hate asking so many questions, but I'm rather stumped on this one and wishing the kind folk who have worked so hard on the documentation for how to create and submit modules to CPAN would have given some thought to the creation of the tarball once the module itself was prepared. (Or perhaps one of you might direct me to where this was done and has yet escaped my notice.)

    One additional question that came up while reading the materials linked above: Can I just submit a link to CPAN and then host the module myself? If so, how would this be done?

    Blessings,

    ~Polyglot~


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