How to set the UTF8 flag?
4 direct replies — Read more / Contribute
|
by dissident
on Aug 17, 2025 at 22:31
|
|
|
Somewhere in a script that has turned all UTF8 flags on, Perl seems to get confused.
No matter what I try, it destroys the encoding when writing the data to a file, by writing two characters for every Unicode byte.
Thus most Unicode characters become a 4-byte, 2-character near-random looking string.
Could the UTF8 flag be not set where it should be set?
As it is apparently impossible to reliably check the UTF8/Latin state of a string variable, the best solution might be just to set the UTF8 flag when the data is known good UTF8.
So, is there a function or some reliable technique to set the UTF8 flag?
Or could it maybe be a better idea in those cases to write the file as raw binary data instead, to circumvent unwanted conversions?
Any other idea what to do?
|
Is it possible load optional modules from PERL5OPT?
3 direct replies — Read more / Contribute
|
by Anonymous Monk
on Aug 17, 2025 at 00:02
|
|
|
I have an internal module that I include in all my scripts. I have been treating it as an optional dependency by loading it like so BEGIN { eval { require Internal::Module } }. This allows me to share my scripts and not have to worry about the internal module not existing on the users' system. But now I'm thinking it would be easier to manage by removing the extra boilerplate and including the module via the env var like so: export PERL5OPT="-MInternal::Module". This only works if the module exists, but I also need it to work if it does not exist. I've been playing around with the if module but haven't been able to get it to work for this case. Is it possible to do what I want?
|
WWW::Mechanize::Chrome : random complaining that Log4perl is not initialised and test fails
1 direct reply — Read more / Contribute
|
by bliako
on Aug 11, 2025 at 16:54
|
|
|
Log4perl: Seems like no initialization happened. Forgot to call init()
+?
23:28:21 Sent 'Target.closeTarget' message $VAR1 = '{"id":11,"params":
+{"targetId":"F75959D2137237798CF1FCB89E87FFAA"},"method":"Target.clos
+eTarget"}';
23:28:21 Can't call method "irand" on an undefined value at (eval 140)
+ line 17 during global destruction.
As an example, consider the following test-script. For me, the below script sometimes passes and sometimes it fails (see below for when exactly), although all subtests succeed all the time.
#!/usr/bin/env perl
use Test::More;
use WWW::Mechanize::Chrome;
use Log::Log4perl qw(:easy);
# This is for the mech obj, Set priority of root logger to ERROR
Log::Log4perl->easy_init($ERROR);
my %default_mech_params = (
headless => 1,
# log => $mylogger,
launch_arg => [
'--window-size=600x800',
'--password-store=basic', # do not ask me for stupid chrome ac
+count password
# '--remote-debugging-port=9223',
# '--enable-logging', # see also log above
'--disable-gpu',
'--no-sandbox',
'--ignore-certificate-errors',
'--disable-background-networking',
'--disable-client-side-phishing-detection',
'--disable-component-update',
'--disable-hang-monitor',
'--disable-save-password-bubble',
'--disable-default-apps',
'--disable-infobars',
'--disable-popup-blocking',
],
);
my $mech_obj = WWW::Mechanize::Chrome->new(%default_mech_params);
ok(defined($mech_obj), "WWW::Mechanize::Chrome->new() : called.") or B
+AIL_OUT("failed to create WWW::Mechanize::Chrome object");
done_testing();
Here is what it says when I run make all && make test. Note that this fails all the time.
Manifying 1 pod document
PERL_DL_NONLAZY=1 "/usr/bin/perl" "-MExtUtils::Command::MM" "-MTest::H
+arness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/l
+ib', 'blib/arch')" t/*.t
t/xx.t .. 1/? Log4perl: Seems like no initialization happened. Forgot
+to call init()?
2025/08/11 23:40:43 Sent 'Target.closeTarget' message $VAR1 = '{"metho
+d":"Target.closeTarget","id":11,"params":{"targetId":"FF3672A330F5788
+556F1B4B18CCC5610"}}';
2025/08/11 23:40:43 Can't call method "irand" on an undefined value at
+ (eval 140) line 17 during global destruction.
t/xx.t .. Dubious, test returned 255 (wstat 65280, 0xff00)
All 1 subtests passed
Test Summary Report
-------------------
t/xx.t (Wstat: 65280 (exited 255) Tests: 1 Failed: 0)
Non-zero exit status: 255
Files=1, Tests=1, 11 wallclock secs ( 0.03 usr 0.00 sys + 0.41 cusr
+ 0.12 csys = 0.56 CPU)
Result: FAIL
Failed 1/1 test programs. 0/1 subtests failed.
make: *** [Makefile:873: test_dynamic] Error 255
However, when I run make all && perl -Iblib/lib t/xx.t, sometimes it fails with above complaining and sometimes it succeeds.
I use the latest W:M:C v0.73. It fails with perl v5.40.2. On another computer with perl v5.38.2 I see no failures (after 10 consecutive runs). Both running latest linux.
Thanks, bw, bliako
ps. my question is not urgent, don't stop swimming to answer it...
ps2. OT but Corion does the dreadful Log4* abomination Log::Log4perl need to be used by WWW::Mechanize::Chrome?
|
Using system sort order
3 direct replies — Read more / Contribute
|
by sciurius
on Aug 11, 2025 at 05:59
|
|
|
Dear monks,
I want my perl program to sort just like the system it is running on.
This is trivial on Linux, but somehow seems complicated on Windows and macOS.
I use the following test program
#! perl
use strict;
use warnings;
use utf8;
binmode STDOUT => ':utf8';
my @words = ( "Maria Lenore", "Mária Helena", "María Dolores" );
my @sorted = sort @words;
if ( "@sorted" eq "María Dolores Mária Helena Maria Lenore" ) {
print("Sorted OK\n");
}
elsif ( "@sorted" eq "Maria Lenore María Dolores Mária Helena" ) {
print("Sorted C (ASCII)\n");
}
else {
print("Sort failed: @sorted\n");
}
On Windows, I get
PS C:\Users\Johan> perl locale.pl
Sorted C (ASCII)
PS C:\Users\Johan> perl -Mlocale locale.pl
Sorted C (ASCII)
PS C:\Users\Johan>
Same on macOS.
When I create files with names "Maria Lenore", "Mária Helena" and "María Dolores" they sort correctly in the Explorer (Windows) and Finder (macOS).
How can I have perl use the same sort order as the system?
Windows 10 with Strawberry Perl 5.30
MacOS 10.15 with Perl 5.34.
These versions are a bit old, but I can not imagine I'm the first to try such a sort in 25+ years.
|
How to pass custom options to pod2man from Extutils::MakeMaker?
1 direct reply — Read more / Contribute
|
by nicholashubbard
on Aug 10, 2025 at 14:00
|
|
|
With ExtUtils::MakeMaker, I would like to set the title of my manpage using pod2man's "--center" option. Unfortunately I cannot figure out how to do this. I have read through Makemakers doumentation but cannot find any mention of passing custom flags to pod2man. I noticed that in MakeMaker's outputted Makefile, there is a rule "manifypods" that handles creating the manpages with pod2man. However, I cannot figure out how to customize this rule. I noticed that pod2man is executed via a function "ExtUtils::Command::mm::pod2man, that does allow passing options, but I'm unsure of how I can gain control of this functions invocation.
Does anybody know how I can do this? Does anybody know of any other projects that pass custom options to pod2man from ExtUtils::MakeMaker?
|
unpack Binary FAIL
2 direct replies — Read more / Contribute
|
by marinersk
on Aug 07, 2025 at 20:09
|
|
|
I'm sure this is going to be really basic, but I've gone several decades using Perl and never needed the pack or unpack functions before.
#!/usr/bin/perl
use strict;
use warnings;
#
my $pReqsca = "ABCD";
my @bytary = unpack 'C*', $pReqsca;
+
my $binstr = '';
+
foreach my $scabyt (@bytary) {
+
my $intbyt = int($scabyt);
+
my $hexbyt = sprintf("%02X", $intbyt);
+
my $binbyt = unpack 'b8', $scabyt;
+
$binstr .= $binbyt;
+
print " Byte = [$scabyt] Integer = [$intbyt] Hex = [$hexbyt] B
+inary = [$binbyt] Binary String = [$binstr]\n";
print "Aborting Loop\n";
last;
}
Results:
W:\Steve\PerlMonks>perl sopw-584100.pl
Byte = [65] Integer = [65] Hex = [41] Binary = [01101100] Binary
+ String = [01101100]
Aborting Loop
Same results using 'B8',
Last time I checked, Hex 41 should show binary 01000001.
What am I missing here?
|
Module to deal with PGP-encrypted email messages in 2025
1 direct reply — Read more / Contribute
|
by Nocturnus
on Aug 07, 2025 at 13:16
|
|
|
Dear well-respected monks,
I was wandering in the darkest night without any hope and orientation, but finally saw the promising lights of the monastery. The heart began to beat again, and I dare to ask you for your wisdom regarding the following question:
I need to deal with PGP-encrypted email messages. To be precise, I would like to permanently decrypt thousands of PGP-encrypted email messages that I have on disk in the form of .eml files.
To my surprise, I haven't found a PGP module for Perl that actually is usable in 2025 with recent Perl versions. Mail::PGP Mail::GPG seemed like a good candidate, but I now have lost nearly two days with trying to install it using CPAN in three different environments (Cygwin, Strawberry Perl / Native Windows and Linux). It seems that it cannot be used with recent Perl versions any more; no wonder that I've failed in trying to make it work.
Then there is Mail:GnuPG. It seems to be the predecessor of Mail:PGP, seems to contain some bugs, and seems to be even more outdated. I managed to install it under Linux, but failed to install it under Cygwin and under Strawberry Perl. The latter would not be the key point, but for certain reasons it would simply be more comfortable for me to run the script under native Windows. At least, Mail::GnuPG could be the last resort.
I am quite sure that I have missed something. What module would you, dear monks, use nowadays to decrypt PGP-encrypted email messages?
To avoid misunderstandings:
This question is about email messages that mostly contain nested MIME structures. When decrypting, all mail headers, MIME parts and attachments of the respective message should be preserved or decrypted, respectively. This means that I cannot use the Perl modules that can handle PGP on ordinary files; that would indeed be a no-brainer.
Furthermore, I could use the GnuPG application directly to permanently decrypt those messages; I have verified that this is possible in general. However, when doing this, the mail headers of the original message do not exist any more in the decrypted version. Instead, the decrypted version contains the headers that were in the encrypted part of the original message. For example, the Date header gets lost; funnily enough, it exists only as a non-encrypted header in the original message.
When anything else fails, I'll eventually take that route: Decrypt using the GnuPG application directly, then merge the headers from the original message with those of the decrypted message. As mentioned above, I'd like to listen to the monks before I take that effort.
Your devoted novice Nocturnus
|
Bluetooth LE?
1 direct reply — Read more / Contribute
|
by cavac
on Aug 05, 2025 at 04:37
|
|
|
I'm trying to connect to a Bluetooth LE (low energy) device, specifically the MXTP-100 printer (that extremely cheap thermal printer with the cat ears). I tried Net::Bluetooth, but that doesn't seem to support Low Energy devices.
I can connect and print just fine to an old Epson bluetooth printer (not LE), but newer LE devices don't seem to work with Net::Bluetooth.
To be honest, i have absolutely no idea what i'm doing. Does anyone have any suggestions?
EDIT: Ooops, sorry, forgot to include my test code:
(I just hacked one of the examples, so code quality is low. Also please ignore the mentioning of GPS, i just didn't bother to update all variable names and comments)
|
Possessive sub-pattern with non-greedy content + recursion: WHY does this work??
3 direct replies — Read more / Contribute
|
by Anonymous Monk
on Aug 04, 2025 at 11:42
|
|
|
Say, a string has embedded palindromes (PD); a PD in this case has odd (at least 3) number of letters. I want a piece of code to be executed exactly once for every PD including nested ones. Let "piece of code" be 'say'. Regex to extract a PD is well-known, and of course TIMTOWTDI, the question is not how to better solve the task.
Almost accidentally I found a solution, which I'm puzzled about. I don't know if it could be a simpler SSCCE without recursion.
say 'match' if 'abABCDCBAdeXYZYXfg' =~ /
(?>^.*?)
(
(
(.)(?-3)\g-1 | (.).\g-1
)
(?{
say $2
})
)
/x;
Output:
YZY
XYZYX
CDC
BCDCB
ABCDCBA
Bingo, just what I need. Match fails -- it's irrelevant. But HOW? If either of the following is removed: (1) "^" anchor; (2) independence (s/>/:/), or (3) entire 1st pair of parens, then there are multiple visits to each PD i.e. not what I want. In fact, for (3) the PD groups are also visited in reversed order, this I don't understand neither. And why both PD groups (not only 1st one) are visited.
Being greedy (s/\*\?/*/) produces no output, this I understand (I hope).
If, in case of code above i.e. without removing anything, the engine gives characters back one by one (I thought this is how backtracking works) before it ultimately fails when it (suddenly, somehow) remembers about independence, then I'd expect YZY is found and said; and then XYZYX is found and embedded code executed for both YZY and XYZYX, i.e. both are said (i.e. YZY said twice in total).
P.S. I've seen "Embedded Code Execution Frequency" section in perlre.
|
Documentation for ->&
2 direct replies — Read more / Contribute
|
by ikegami
on Aug 03, 2025 at 13:40
|
|
|
|
|
|