Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
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. 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?

    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 line 7.
    Why are these two undefs different? Thanks.
regex in REPLACEMENT in s///
3 direct replies — Read more / Contribute
by perlboy_emeritus
on Sep 12, 2023 at 15:18
    Hello Monks;

    I've been asked to review and refactor/optimize a large collection of perl5 scripts, and a great many that include regular expressions would appear to benefit from s///e, the eval modifier, as in s/PATTERN/CODE/e, iff I can substitute ANY regex for CODE. Programming Perl 4th, pg 254 says, to wit: "Applications of this technique are limitless", and I agree if I can generalize the use of any regular expression in CODE. However, things are not going according to plan in my perl (v5.36.1) built for darwin-2level. A very simple case in point (I have much more complex instances once I can get this basic example to work.

    my $str = 'This ia a real number, 123456.56'; say $str; (my $res = $str) =~ s{ (\d+) }{ sprintf("%s", 4*$1) }xe; # $1*4, just +to show /e works say $res; say "\$1 = $1"; (my $adj = $1) =~ s/(\d)/3/g; # replace each \d with '3', same reason say $adj; ($res = $str) =~ s{ (\d+) }{ ($adj = $1) =~ s/(\d)/3/g }xe; # replace +'123456' with '333333', proof of concept say $res;

    This yields:

       This ia a real number, 123456.56
       This ia a real number, 493824.56
       $1 = 123456
       This ia a real number, 6.56

    The resulting string after substitution should be 333333.56. There are, indeed 6 substitutions, so it looks like the boolean value of each iteration is being substituted. The fragment '($adj = $1) =~' I believe is required because $1 is immutable, and the result is the same with /ee. Any suggestions re patterns of regex expressions in the CODE part of S/PATTERN/CODE/ would be very appreciated.

    Thanks Monks for your help.

Uncontrolled Memory Allocation
4 direct replies — Read more / Contribute
by Rishi2Monk
on Sep 12, 2023 at 09:27

    Checkmarx, a static analyzer tool is throwing security issue with the below code, saying that $file_list is accessing Uncontrolled Memory Allocation.

    open ( INFILE, "<", "$inputfile" ) || die( "Cannot read list file +$inputfile" ); while ( <INFILE> ) { $file = $_; chomp ( $file ); $file_list{$file} = "1"; }

    I tried to restrict the size of the hash variable as mentioned below but the error is not resolved.

    if(length($file) <= (1 * 1024 * 1024)) { $file_list{$file} = "1"; }
    Kindly help me to understand the reason as the above code looks ok to me.
postfix "for" question?
3 direct replies — Read more / Contribute
by misterperl
on Sep 11, 2023 at 10:18

    (Perl 5.16 on linux).. I often prefer postfix expressions, like  say $_ for 1..10;

    As a natural extension, I'd like to use nested postfix loops. ChatGPT suggested:

    say $_ for 1..5 for 1..10;

    which seemed a strange proposition, because how could the scalar $_ contain both $i and $j? I tried it, thinking: "I gotta SEE this!". I thought perhaps Perl was clever enough to make $_ a reference to a list, but say or print of that would only be the reference, which was even more perplexing why it was suggested.But it only throws a syntax error on the second "for".


A good way to input data into a script w/o an SQL database
10 direct replies — Read more / Contribute
by ObiPanda
on Sep 09, 2023 at 20:58

    I have a working script and thus- the following is completely unnecessary. For the most part, I'm just wanting to "improve" the script by separating the data from the core script, and I'm asking for suggested good/best practices.

    The data entries are each elements in an array of hashes. Here is an example of some the hash keys.

    { Sub_Name => "", Archive_File => "", Lib_Sub_Path => "", }

    Is it best practices to leave it in the script or to save it to one data file or a list of data files? For my specific situation, there is no reason to set up an SQL server and database. I just want to use a simple file or files in a directory which contain the information I need to run the script with a little bit of abstraction.

    Another question: is there a good CPAN module to use to input it into an array of hashing as already implemented in the script, I have no idea what to look for.

XML::LibXML::Node::addSibling() -- nNode is not a blessed SV reference ...
1 direct reply — Read more / Contribute
by mldvx4
on Sep 09, 2023 at 08:22


    I'm branching out from working with HTML::TreeBuilder::XPath and HTML::Element which I am comfortable with. I am now trying to streamline some existing scripts by using XML::LibXML and XMLL::Element instead since that seems to allow transformation between various instances of XML more easily.

    Or maybe not. There are some differences but by and large everything I have tried has gone well, and the parts I have refactored seem to run more quickly, except now I have become stumped by the error produced on line 19 in the script below, "XML::LibXML::Node::addSibling() -- nNode is not a blessed SV reference". How can I get past that and add a node?

    #!/usr/bin/perl use XML::LibXML; use XML::Element; use strict; use warnings; # produces the following error: # XML::LibXML::Node::addSibling() -- nNode is not a blessed SV referen +ce ... my $html = XML::LibXML->load_xml( IO => *DATA{IO} ); foreach my $dd ($html->findnodes('//dd[*/*/a[@class="f"]]')) { if (my $f = $dd->find('./*/*/a[@class="f"]/@href')) { print qq(F=$f\n); my $cc = XML::Element->new('dd'); $cc->push_content('foo'); $dd->addSibling($cc); # this line fails } } print $html->toString,"\n"; exit(0); __DATA__ <html><body> <dl> <dd><details><summary>Example</summary> <dl> <dd><details><summary>one <a href="http://localhost/s1" class="s">0</a> <a href="http://localhost/f1" class="f">0</a> </summary></details></dd> <dd><details><summary>two <a href="http://localhost/s2" class="s">1</a> <a href="http://localhost/f2" class="f">1</a> </summary></details></dd> <dd><details><summary>three <a href="http://localhost/s3" class="s">2</a> <a href="http://localhost/f3" class="f">2</a> </summary></details></dd> </dl></details> </dd> </dl> </body></html>

    The script successfully selects a specific set of dd elements below but cannot add a sibling to any of them, at least not the ways I've tried so far. I've tried quite a few permutations and variations on the theme, too.

does a hash element as a loop parameter cause significant slowdown?
6 direct replies — Read more / Contribute
by misterperl
on Sep 08, 2023 at 13:55
    ChatGPT suggested that I save hash values as constant vars and use:

    my $n1 = $data->{n1}; my $n2 = $data->{n2}; for my $c (1..$n1) { $data->{"exclude-$c-$_"} = 'yes' for 1..$n2; }

    instead of

    for my $c (1..$data->{n1}) { $data->{"exclude-$c-$_"} = 'yes' for 1..$data->{n2}; }

    I don't like the idea of more vars- would this really be significant? Most of my n1/n2 are like 10 or less....

why wont $i increment?
1 direct reply — Read more / Contribute
by misterperl
on Sep 08, 2023 at 08:14
    I have this expression:

    delete $data->{"exclude-$k{$_->{name}}-$i++"} for @a;

    which runs @a times as expected in dbg, but $i remains at it's initial value? Why doesn't $i++ increment each time?

Add your question
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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-09-24 07:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found