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.
PerlMaven has an interesting example that is short by one additional array characterization--one which I have searched for online in vain. Here is his example:
my $one_string = "hello world";
say length $one_string; # 11
my @many_strings = ("abc", "cd", "e", "fg", "hi", "hello world");
say length @many_strings; # 1
say scalar @many_strings; # 6
package Methods {
use strict;
use warnings;
use feature qw(isa);
use Logic::Easy;
no warnings qw(redefine);
sub frobnicate : Multi(frobnicate) {
SIG [$o] where {$o isa 'Foo'};
qq(foo);
}
sub frobnicate : Multi(frobnicate) {
SIG [$o] where {$o isa 'Bar'};
qq(bar);
}
1;
}
__END__
#!/usr/bin/env perl
use strict;
use warnings;
use lib q(.);
use Bar;
use Foo;
use Methods;
use feature qw(say);
my $gizmo = Foo->new();
my $mojo = Bar->new();
# dd $gizmo, $mojo;
say Methods::frobnicate($_) for
($gizmo, $mojo);
__END__
As expected, frobnicate cannot be imported - "there can be only one". Also with Methods->frobnicate() it does not work. Remains only Methods::frobnicate() - as to see. Or is there something else I have overlooked?
I'm creating an XML array with Perl. One line of the XML can potentially contain many characters. Generally it's not an issue. But sometimes the line gets truncated at some arbitrary point, like:
<tag>many characters ends abruptly here in the middl</tag>
I save the line in a scalar array element, then print the entire array. The result has the begin and end tag, but the line in the middle is truncated. In the last fail, it truncated at 23,052 characters which didn't seem like a particularly significant number. In other cases the line greatly exceeds that length, without truncation.
Ideas of why this happens on some lines and not others, or better ways to save these long lines with are appreciated. Maybe more than one tag? I studied MAX XML tag-contents restrictions, as well as Perl scalar length limits, and I don't see any specific limits.
I'm not even invoking taint in the script--but it seems that taint is active (is this because of the Perl version? more on that below), and crashes the program for one simple line:
my @fonts = `/usr/bin/fc-list : family`;
My perl version is:
perl -v
This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-li
+nux-gnu-thread-multi
(with 58 registered patches, see perl -V for more detail)
The error that appears in my logs is: "Insecure $ENV{PATH} while running with -T switch . . .," despite the fact that my shebang line has only #!/usr/bin/perl and, while narrowing the problem down, I have disabled all module uses except one: use CGI qw(-utf8);.
As is clear to be seen, there are no variables, nor any executable code, inside the backticks--so there should be no legitimate security issue worthy of shutting down the execution of the script. They invoke a standard command which is useful for displaying the fonts available and installed on the server. Should those fonts change, such as if more were to be installed, the next run of the script would automatically show this--which is what I want.
But even if I put ls inside those backticks, the whole script will fail, and I get "Internal Server Error" as the message in my browser.
Other options than using backticks seem cludgy at best, and problematic at worst. I could run a cronjob that writes the output of this command to a file, then the perl script reads from that file. To be up-to-date, this cronjob would have to run often, consuming server resources--not to mention the added unnecessary file to be stored in the system.
I could use a system call (assuming taint would allow this--I haven't tried it yet) that would do what the cronjob does, then, after opening and reading the file, I could unlink it. This requires multiple steps, much more code, involves file permissions which create potential failure points, and just seems quite rather unperlish.
I fell in love with perl years ago when it gave me the ability to do what I wanted in easy and intuitive ways. This taintedness is rubbing against the grain.
Why is taint even forced on the script without my having invoked it? In actual fact, I would like the script to run with taint, but I need to be able to run this command, too. Is this as impossible as wanting to have my cake and eat it too?
Hey guys, I need a regex that can check if a string contains exactly two 'x' and one '/', and some numbers, like in this example '240 x 240 x 2/3600'.
I strip all spaces and stuff out of the string upfront. The numbers can be anything from 1 up to 10000. Been trying, but up to now, I haven't found a solution. Anyone?
KInd regards,
Ton
I am enforcing some rules are CRM tags and the regexps are pushing my limits. So, a follow up to a little question over at Re^3: regex in REPLACEMENT in s///
I understood it to equivalent to s/ +$//; when used as above with no new lines.
Part B
The tags should be lowercase and exclude most punctuation. Extraneous punctuation removed and uppercase characters converted to lowercase. Here is what I have tried
I expected the regexp to substitute anything that is not ^ in the character class [] with an empty string. But it seems to strip out anything that is not an 'a' character or a space.
How should I go about properly working out how to construct a regexp to do what I want?
I'm messing around with sockets, and all is well, but my socket server spends most of its time waiting on "my $live_socket = $socket->accept();".
Up until now, I've just been killing it with ^C, but running the bulk of the socket code as a child process seemed a better way to control it.
My problem is that ideally, I want my parent process to terminate the child, but only if it's sitting on $socket->accept(). Suggestions? Cut-down code below...
my $fork_pid = fork();
if($fork_pid == 0){
while(1){
my $live_socket = $socket->accept(); # only allow parent to kill c
+hild here
<do stuff - read from socket, checksum check, write to file, send
+ACK/NAK etc.>
$live_socket->close();
}
}
# parent should kill child nicely (9 is a bit drastic, but <9 doesn't
+seem to work), and only if child is waiting for a socket connection
else{
print "Hit <ENTER> to quit\n";
my $quit = <STDIN>;
kill 9, $fork_pid;
}
Simple. Don't use it right? We'll I have data encrypted with this method that I need to decrypt first in order to upgrade it to something more secure and modern. I'd prefer not to have to decrypt, store it in the db, and then come back and re-encrypt using a new method.
my $ciphertext = $cipher->encrypt_hex($plaintext);
my $text = $cipher->decrypt_hex($ciphertext);
$text and $plantext aren't equal on 3.04 but they are on 2.33. I dug through the documentation and changelog but can't spot anything that says this shouldn't work. There are lots of new options in 3.04. I reached out to Lincoln Stein (the author) but I haven't heard back. Any other experts? :)
EDIT: It looks like I'm not the only one. https://rt.cpan.org/Public/Bug/Display.html?id=134355 Unfortunately I'm using different calling params and not sure how to adapt to my situation. :(
Is there a way to stop the CPAN Testing process?
And if there is, should I?
Last evening I uploaded a new version of Image::Square with new tests using PNG images for input and output instead of JPEG images. This followed the advice I got on Testing image output
The first two test results came in just before I settled down to sleep and they were both PASS so I went to sleep content...only to check this morning and find lots of FAILs 😕
This morning I uploaded a new version using the native GD image format and using cmp_ok instead of ok from hippo's Basic Testing Tutorial
So now that this latest version is uploaded, is there a way to stop CPAN Testers from wasting their precious resources on a module version that will fail at least some of the time? Does it stop automatically when a new version is uploaded?
Edit:
Changed link to point to GitHub repo and not the CPAN Tester results.
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).