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.
Ea has been a dynamo of ideas, including of making a PDL Advent calendar. We're tracking that on https://github.com/PDLPorters/pdl/issues/503 - anyone who feels a yen to help out, please comment there!
I'm using a Perl script with Apache and mod_perl to generate dynamic responses to client requests. At times I would like to signal Apache that my response is complete so the client can begin processing the response while on the server I perform a bunch of other clean up tasks. My code looks like this
use CGI::Simple;
my $q = new CGI::Simple;
print $q->header(%HTTP_HEADERS);
print $html;
# response complete
... continue with various tasks ...
use Audio::TagLib;
$test = shift;
$file = Audio::TagLib::FileRef->new($test);
say 'title:', $file->tag()->title()->toCString();
say 'track', $file->tag()->track()->toCString();
My computer is a new Dell Inspiron with an Intel i9 3900.
I wrote a cli similation of a simple solitaire card game that I knew the winning stats for.
I was disappointed in the results; but here is the culprit.
pops and shifts and individual selections from lists were unstable about 5% of the time.
Lots of run-time warning msgs about uninitiated values and more failures that went undetected.
Unitiated cards in the source list, and uninitiated variables supposed to have receive popped or selected cards
The code sequence causing the problem was : search a nine card list for a pair, cover both cards of a pair with cards from the stock, search the updated list for a pair or pairs.
One or both of the covering cards never arrived maybe 5% of the time.
So, the basic question is : with twenty some CPU cores looking for work, is Perl really up for this environment?
I guess we have all been there: You make an "obviously trivial" change ... and the code fails to behave.
This is a stripped down version of the "good" code:
use 5.030;
use warnings;
my @subrefs = ();
for my $i (1..3) {
push @subrefs, sub { print $i; };
}
for (@subrefs) {
$_->();
}
It seems the anonymous subroutine is not built with the value of $i, but with a reference to $i.
I would not have expected this. Is this a bug, a quirk, or something which can be found documented somewhere?
Edited to add:
I wrote that bad code BC (Before Coffee), remembering that I had done something very similar before. I have now dug that up:
use 5.030;
use warnings;
my @subrefs;
my $i = 0;
for (1..3) {
push @subrefs, sub { print ++$i };
}
for (@subrefs) {
$_->();
}
This prints 123. The difference is that the increment is done when the subroutine is called, not when the subs are declared and pushed to the array.
In BC state I failed to see that difference.
Hi Monks,
I am having some trouble with a MySQL statement, or rather the fetchrow_hashref to get the data.
It always returns the error message "DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at", but nevertheless the results are completely fine and as expected.
I have no idea what is causing this error.
$error returns 1
My code:
my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pw) || die;
my $sql;
my $sth;
if($ENV{'REQUEST_METHOD'} eq 'GET')
{
my $daten;
$daten = $ENV{'QUERY_STRING'};
my @formularfelder = split(/&/, $daten);
my @formular = ();
my $i=0;
foreach my $feld (@formularfelder)
{
(my $name, my $value) = split(/=/, $feld);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$formular[$i] = $cgi->escapeHTML($name);
$i++;
$formular[$i] = $cgi->escapeHTML($value);
$i++;
}
if(!defined $formular[0] || $formular[0] ne "dl" || ! defined $for
+mular[1] || $formular[1] !~ /^\d+$/)
{
Navi::print_navi(": Nice try... :");
print qq{<div id="category">[ NICE TRY ]</div>};
print "<b>Nice try...</b>";
}
else
{
$sql = qq{select p.category as category, p.titel as titel, p.b
+eschreibung as beschreibung , c.werbung as werbung from movie_project
+s as p join movie_categories as c on c.id = p.category WHERE p.id = ?
+};
$sth = $dbh->prepare($sql) or die "Error: $DBI::errstr";
my $error = $sth->execute($formular[1]) or die "Error: $DBI::e
+rrstr";
print "$error\n";
while (my $ergebnis = $sth->fetchrow_hashref())
{
$ARGV[0] is my subroutine name, and $ARGV[1] it's arg. The sub is a method in a class and I blessed object $A with new(), that class has a sub with a name contained in $ARGV[0].
I researched using a variable as a sub name and found may examples with syntax like \$sub , &$sub , \&$sub, "do", "eval", even a seemingly unusual &{\&{$sub}}();
I can get some of these to work on a local sub, but not as part of my class object $A , trying syntax using those examples, like:
I'm just learning Perl and as an exercise I'm writing a small script that reads a file and outputs every URL it can find. I know there are modules for this, this is just for learning purposes, and to brush up on regular expressions. I came up with the following regular expression:
my $re = qr(
(
(?:[a-z][a-z0-9+-.]*)
://
(?:
(?:
[a-z0-9._~!:;=&'\$\(\)\*\+\-\,]+@
)?
(?:
\[${ipv6}\]
| ${ipv4}
| [a-z0-9._~!;=&'\$\(\)\*\+\-\,]+
)
)
)
)xi;
foreach ($ARGV[0]) {
open my $fh, '<', $_ or die("Error opening file $_.\n");
while (my $row = <$fh>) {
chomp $row;
next if $row eq "";
if ($row =~ $re) {
print "$1\n";
}
}
close($fh);
}
As you can see, I'm using qr to define the regular expression, as it's composed of other regular expressions defined in the code (omitted here for brevity). This gives me the most flexibility to later on refactor this script to make it more general purpose, or at least that is the idea.
The file is read line by line, comparing against $re, and correctly printing the first URL it finds on that line. And that's the issue, it only finds the first match even when there are multiple URLs on that line. Typically, this is where I'd use the global flag, except that apparently I cannot use it with qr as I get an error: Unknown regexp modifier "/g".
I've been reading about this but haven't been able to figure out a way to search the entire line to capture all matches. I tried using the s flag, different delimiters for qr, in case that made any difference, and of course tried modifying $re to use operators like + and *, but without any results.
So, I don't know if I'm misunderstanding the problem that I need to solve, or I just don't know enough about Perl to use it effectively. I would say the issue is that declaring regular expressions with qr is not what I need for this particular case but I'm just not sure. Any ideas? Thank you!