Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Seekers of Perl Wisdom

( #479=superdoc: print w/replies, xml ) Need Help??

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 get the TOTAL length/size of an array?
6 direct replies — Read more / Contribute
by Polyglot
on Sep 21, 2023 at 00:40
    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

    I want to know what it would take for the next one:

    say __?__ @many_strings; # 21

    Blessings,

    ~Polyglot~

How to bring in multimethods
3 direct replies — Read more / Contribute
by karlgoethebier
on Sep 20, 2023 at 09:28

    Hi all, please consider this:

    Foo.pm

    package Foo { use strict; use warnings; use Class::Tiny; 1; } __END__

    Bar.pm

    package Bar { use strict; use warnings; use Class::Tiny; 1; } __END__

    Methods.pm

    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__

    run.pl

    #!/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?

    «The Crux of the Biscuit is the Apostrophe»

When can the character length of a Perl Scalar become an issue?
3 direct replies — Read more / Contribute
by misterperl
on Sep 20, 2023 at 08:53
    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.

Proper and acceptable use of backticks in a modern Perl script
2 direct replies — Read more / Contribute
by Polyglot
on Sep 18, 2023 at 06:23
    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?

    Blessings,

    ~Polyglot~

Regex question
2 direct replies — Read more / Contribute
by Umdurman
on Sep 16, 2023 at 19:53
    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
chomp and regexp
2 direct replies — Read more / Contribute
by Bod
on Sep 14, 2023 at 10:38

    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///

    Part A

    I have never got chomp to work so I avoid it...

    my $tag = 'test text '; chomp $tag; print $tag . '#'; > test text #

    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

    $tag = lc $tag; $tag =~ s/[^az09 _\-\+\.]//g;
    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?

Kill a child nicely
8 direct replies — Read more / Contribute
by Melly
on Sep 14, 2023 at 07:11

    Hi Monkeys

    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; }
    map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
    Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
Crypt::CBC 2.33 -> Crypt::CBC 3.04 incompatibilities
2 direct replies — Read more / Contribute
by mmlenz
on Sep 13, 2023 at 08:51
    Got a situation where I can't use the same params in 2.33 with 3.04:
    my $cipher = Crypt::CBC->new( -key => $pass, -cipher => 'Blowfish', -header => 'randomiv' );
    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. :(
Stopping tests
3 direct replies — Read more / Contribute
by Bod
on Sep 13, 2023 at 06:51

    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.

Why are these undefs different?
4 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 12, 2023 at 23:05
    Hello, If I run the following code:
    use Data::Dumper; use List::Util qw(first); my @x = ({"aaa" => 100}); say Dumper([grep { $_->{"aaa"} < 4 } @x]->[0]); say Dumper((first { $_->{"aaa"} < 4 } @x));
    I get:
    $VAR1 = undef; $VAR1 = undef;
    But if I instead do
    say Dumper([grep { $_->{"aaa"} < 4 } @x]->[0]->{"aaa"}); say Dumper((first { $_->{"aaa"} < 4 } @x)->{"aaa"});
    I get
    $VAR1 = undef; Can't use an undefined value as a HASH reference at test.pl line 7.
    Why are these two undefs different? Thanks.

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


  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • 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).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2023-09-21 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?