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.
I have what should be a simple question, but I can't figure out how to do it in DBIx::Class (which I'm using elsewhere in this code, so I need it for this too).
I have a single table, that (for this purpose) contains an ID, a name, and a score (and other data not relevant for this question). I want to get the Result object for the highest score for each name. That's it. So, for the data
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 1 | John | 24 |
| 2 | John | 60 |
| 3 | Mary | 4 |
| 4 | John | 10 |
| 5 | Kate | 30 |
| 6 | Mary | 20 |
+---+------------+-------+
I want to get back rows 2 (John's highest score), 5 (Kate's highest (and only) score), and 6 (Mary's highest score).
That is, the SQL would be something simple like SELECT MAX(score), name FROM scores GROUP BY name; (except that I want to get the Result object, not the actual column values).
How can I accomplish this? The docs are pretty sketchy about grouping in general, and I can't find a good parallel anywhere.
I have a fairly substantial, but 'old', set of tests for Cclite. They use, or used use Test::WWW::Selenium; which now doesn't seem to find a webdriver on any version of Selenium that I download
Error requesting http://10.0.0.65:4444/selenium-server/driver/:
404 Not Found
I'd really like to find some way to use/re-use them without too much extra work. I'm prepared to do a little Java and recompile Selenium if it comes to that
Hello folks. I am puzzled. I've been noticing a message from cpan that I
don't remember seeing before; an example is below. I was seeing 17 build dirs
and then I ran install for a couple more today, and it jumped to 19. Why and how is cpan
doing this? I know it's necessary when a module being built/tested/installed has
prerequisites but they seem to be hanging around long after they are necessary.
Anyone? If you're in the know, kindly enlighten me.
Prepending blib/arch and blib/lib of 19 build dirs to PERL5LIB, reaching size 2952; for 'install'
perl is CygwinPerl 5.40.3
CPAN/cpan is v2.38
EDIT
I've attempted to answer the question for myself, see below. Only with partial success.
This is related to my module Crypt::SecretBuffer, where the goal of the module is to prevent leaking secrets into the freed heap memory of the perl process where something could later come along and scan the address space looking for leftover secrets. The SecretBuffer tries to isolate data from being looked at by any normal Perl ops, so that it can be exclusively be viewed by XS/C functions and wiped clean when it goes out of scope.
So, in that context, is there any way to use "pregexec" to apply the perl regex engine to my buffer but prevent it from making copies into perl-owned buffers? pregexec seems a bit under-documented... It says "described in perlreguts" but while that has pages upon pages of the inner workings of the regex engine, it doesn't even tell the meaning of the return value of pregexec or explain exactly what the final "nosave" parameter means. Ideally it would be a flag that does exactly what I want and avoids copying any buffers into any global variables, but that doesn't seem to be the case from looking at the C code. (which I admit I haven't taken the time to fully understand yet)
I'd also be OK if it made copies, but someone could tell me a reliable way to go zero out the buffers of those SVs so that all the captures magically appear to be full of NUL characters afterward.
Basically I'd like it to behave like standard C library regexec that just records positions of the capture groups in an array. I'm also debating if I should just use libc's regexes and declare that limitation on the SecretBuffer API, that you have to restrict yourself to Posix extended regex notation.
Update:
So actually the "nosave" parameter does appear to do some of what I want. Setting that flag prevents any of the magic variables from getting updated.
perl -e 'use v5.40;
use Inline C => q{
int call_pregexec(SV *regex, SV *sv) {
REGEXP *rx= SvRX(regex);
STRLEN len;
char *buf= SvPV(sv, len);
return pregexec(rx, buf, buf+len, buf, 0, sv, 1);
}
};
say "098mnb" =~ /([0-9])([a-z])/;
say call_pregexec(qr/([a-z])([0-9])/, "abc123");
say $&;
say $1; say $2;
say $+[0]; say $+[1];'
I am learning Perl using a book downloaded from the internet.
Opal is a system for charging fares on public transport in NSW Australia. I have had four Opal cards and they identified by 16 digit numbers. I have four target directories into which the files related to these cards go and I have tried to set up a hash to map between card numbers and the destination directories but it does not work and the error messages make no sense to me.
This is the current form of the hash definition which is at line 26 in the script:-
my %OCN_PFX_To_TDir=(
3085220111379778 => '3085220111379788_Rexx',
3085220334620644 => '3085220334620644_Spare',
3085220346432582 => '3085220346432582_Rexx2',
3085220352420034 => '3085220352420034_Algol'
);
This is the input followed by the error messages I get:-
carl@md-tower-004:~$ perl ~/bin/copy_opal1.plx
Can't modify constant item in scalar assignment at /home/carl/bin/copy
+_opal1.plx line 26, near ");"
Execution of /home/carl/bin/copy_opal1.plx aborted due to compilation
+errors (#1)
(F) You aren't allowed to assign to the item indicated, or otherwi
+se try
to change it, such as with an auto-increment.
Uncaught exception from user code:
Can't modify constant item in scalar assignment at /home/carl/bin/
+copy_opal1.plx line 26, near ");"
Execution of /home/carl/bin/copy_opal1.plx aborted due to compilat
+ion errors.
I am writing a small program to copy Opal related files from my downloads directory to the appropriate destination directories.
Basically it tells me that I am trying to assign a scalar value to the % sign representing the modulus operator which cannot receive such a value but the % sign is to mark the variable as being a hash.
I have tried various changes on the definition, originally I had both keys and values in double quotes now I have the values in single quotes, originally I used commas to separate key value pairs. No matter what I change the error messages remain the same.
It may be that the book I downloaded is old and some syntax suggested in the book no longer works but no matter how many queries I have put into Google and how many AI overviews I have seen and hit list links that I have followed nothing has confirmed this.
Hello Monks and Nuns. I was working with cpan (the installer) on CygPerl today and
tried the command recent; it didn't work and this is what I saw:
cpan2> recent
CPAN: XML::LibXML loaded ok (v2.0210)
Fetching 'http://search.cpan.org/uploads.rdf'
CPAN: LWP loaded ok (v6.80)
DONE
Catching error: "XML::LibXML::Error=HASH(0xa01ebe148)" at /usr/local/share/perl5/site_perl/5.40/CPAN.pm line 397.
CPAN::shell() called at -e line 1
Could anyone else try this and see what happens? I'd appreciate it.
My system setup is:
This is perl 5, version 40, subversion 3 (v5.40.3) built for x86_64-cygwin-threads-multi
/usr/lib/perl5/vendor_perl/5.40/x86_64-cygwin-threads/XML/LibXML.pm
XML::LibXML version 2.0210
CPAN version 2.38
@INC:
/usr/local/lib/perl5/site_perl/5.40/x86_64-cygwin-threads
/usr/local/share/perl5/site_perl/5.40
/usr/lib/perl5/vendor_perl/5.40/x86_64-cygwin-threads
/usr/share/perl5/vendor_perl/5.40
/usr/lib/perl5/5.40/x86_64-cygwin-threads
/usr/share/perl5/5.40
Nov 05, 2025 at 18:08 UTC
A just machine to make big decisions
Programmed by fellows (and gals) with compassion and vision
We'll be clean when their work is done
We'll be eternally free yes, and eternally young Donald Fagen —> I.G.Y. (Slightly modified for inclusiveness)
I am trying to crank down restrictions on an FCGI script. I have DBI opening an SQLite3 database as read-only. This is how I am currently connecting to the database:
my $dbh = DBI->connect("dbi:SQLite:dbname=$database",
undef,
undef,
{
AutoCommit => 0,
RaiseError => 1,
on_connect_do => "PRAGMA foreign_keys = ON",
sqlite_open_flags => "DBD::SQLite::OPEN_READONLY",
})
or die("Could not open database '$database': $!\n");
However, AppArmor still gives the following error about needing write access, even though the error and restrictions causing it do not impair the script from appearing to work as expected:
If I add w to that, the errors go away. However, I don't want to do that because the script's access is supposed to remain read-only. Yet it appears that DBI is somehow still requesting some kind of write access, even if it does not use it. What can I change in the DBI connection to ensure that it is purely read-only as far as the database goes?
Previously (5.32), there was consistency as expected: PV address was "real" RAM address; and simple fact of "packing to pointer" didn't change scalar' guts. Now, I see some weird huge ("virtual"?) addresses -- even though everything works (including, not shown above, accessing (or acquiring, same values) these pointers from C). Where such numbers come from, is this the new normal? Is difference in "Dump" output related to that, or totally another issue?
I'm an inexperienced Perl programmer (my background is mainly in Python), charged with keeping a legacy app running, and I'm having trouble figuring out how to validate a date. (At least, that's what I think the problem is.)
We have a function that takes a date value and does some transformations on it. This is thoroughly tested and works fine for its use case, but we don't actually have a test for what happens if someone sends garbage into it—it assumes the input really is a valid date. Normally, this is fine; the input comes from internal sources and should always be OK. We now need to extend it for a case when we don't have control over the incoming data. So, I tried to wrap it in an eval to catch any errors. Currently, it looks like this:
# in MyApp::Util
sub convert_datetime {
my ( $self, $incoming_datetime ) = @_;
my $new_dt;
eval { $new_dt = DateTime::Format::ISO8601->parse_datetime( $incom
+ing_datetime ); };
return undef if $@;
# No error, now we can convert the date (not shown)
}
When I run this, however, I get (I've changed the file paths):
ok 141 - convert_datetime doesn't blow up if sent bogus date
Invalid date format: at /path/to/MyApp/Util.pm line 818.
eval {...} called at /path/to/MyApp/Util.pm line 818
MyApp::Util::convert_datetime("MyApp::Util", "FOO") called at t/Ut
+il.t line 297
...propagated at t/Util.t line 298.
So the test "works" in that it returns an "ok" value, but it also prints the whole "Invalid date format" thing. How do I get it to _not_ do this, or otherwise test to see if the input is valid before I pass it to DateTime::Format::ISO8601->parse_datetime? The docs for this function say "The parse_datetime method will attempt to match these ambiguous strings" etc., but doesn't say what happens if it can't, or what you're supposed to do to test for valid input.
I tried to write a test script, but even though it looks identical (to me), this does _not_ print the "Invalid date format":
#!/usr/bin/env perl
use strict;
use warnings;
use DateTime::Format::ISO8601;
eval {my $foo = DateTime::Format::ISO8601->parse_datetime("FOO");};
print "ERROR\n" if $@;