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
Augmenting and reducing data structures
6 direct replies — Read more / Contribute
by sciurius
on Apr 22, 2021 at 08:22

    Assume you have a (big) perl data structure, a hash, of which all keys have values that can be scalars, arrays or hashes. Arrays and hashes can contain scalars, arrays or hashes, and so on.

    For example:

    $orig = { a => { b => [ 'c', 'd' ], e => [ [ 'f' ] ] } };

    Assume there is an operation 'augment' that takes this data structure and another (smaller) structure. The original data structure will be modified so that all the data from the smaller structure is now in the original structure as well.

    For example, augmenting the strucure above with

    { a => { e => [ [ 'g' ] ] } }

    will yield

    { a => { b => [ 'c', 'd' ], e => [ [ 'g' ] ] } }

    What I'm looking for is the reversed operation, let's call it 'reduce', that takes the original and augmented structures as arguments, and returns the (minimal) structure that is needed to augment the original.

    I'm sure that there are modules that already implement this but I haven't been able to identify them...

    A cut/paste ready test program:

    use strict; use Test::More tests => 2; use Storable qw(dclone); my $orig = { a => { b => [ 'c', 'd' ], e => [ [ 'f' ] ] } }; my $new = dclone($orig); my $delta = { a => { e => [ [ 'g' ] ] } }; augment( $new, $delta ); my $augmented = { a => { b => [ 'c', 'd' ], e => [ [ 'g' ] ] } }; is_deeply( $new, $augmented, "augment" ); reduce( $new, $orig ); is_deeply( $new, $delta, "reduce" );
Removing unused Perl modules
4 direct replies — Read more / Contribute
by no longer just digit
on Apr 21, 2021 at 22:14

    Today, doing some module renaming work, I found that I had "use <Module>" in a lot of files where it was not being used. I expected to find a CPAN solution for finding unused "use Module" statements but the best I got was this old stackoverflow answer:

    https://stackoverflow.com/questions/13219442/find-unused-used-perl-modules

    I already know Test::Vars for removing unused variables, but is there a good Test::Modules or similar for detecting unused modules included into files?

Parse multipart/form-data with MIME::Parser
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 21, 2021 at 19:11
    I've been making an experimental webpage via nginx perl modules (not CGI!), and while I've been successfull on presenting the input page to the client (a main input field, some side fields, the send/reset buttons), I had ran into some problems with reciving the data.
    Let's imagine a hypothetical scenario: a user fills in the tables and sends a POST request to the server, with the request headers containing a "content-type" field (which sould be "multipart/form-data" and the data, which should have been formated as per standarts and contain the stuff (text, image, other) that the user had put in the tables. The server, upon reciving the request, does actions as dictated by the code, with one of them being to prosess (use) data contained with the request.
    In order to properly use the data, you have to extract it, and to do so, you use a parser. However, the multipart content type specification is too large to make a parser that will parse the input properly all by myself in a short time. I have set my eyes upon MIME::Parser, however I haven't been able to find documentation that I could understand (or apply), nor could I find other people asking the same (or close) questions (And when I can find them, the answer usually involves CGI, which I've decided to not use from the begining of the project).
    Am I missing something, or had I overlooked a more simple solution?
More Effecient Method Chaining
2 direct replies — Read more / Contribute
by fennewald
on Apr 21, 2021 at 18:08

    I'm writing some one off functionality to work with other CLI tools. I was curious (1) what was the idiomatic way to do what I describe, and (2) if there is a faster way

    This specific example deals with the command gst-inspect-1.0. You don't need to know what it does, just this. Calling gst-inspect-1.0 without arguements returns a list of plugins. Calling gst-inspect-1.0 with a plugin name returns information about that specific plugin. Each call to gst-inspect-1.0 takes about half a second, give or take (so suprisingly long).

    I want to work with the description for each plugin, so I wrote the following snippet:

    my %plugins = map { $_ => scalar `gst-inspect-1.0 $_` } # Inspect the plugin map { $1 if /^\w+:\s+(\w+)\b/ } # Reduce to plugin name grep { /^\w+:/ } # Filter header and empt +y lines out split /\n/, `gst-inspect-1.0`; # Bring in input
    Is my code idiomatic? Should I be creating separate arrays for each of these steps? Is there any way to have line 2 run in parallel? Each call does take a while and it adds up fast with ~1000 plugins!
Validate XML with schemas specified inline (xsi:schemaLocation) (with libxml or anything different from Xerces)
2 direct replies — Read more / Contribute
by jjmoka
on Apr 21, 2021 at 17:00
    I've been (successfully) using XML validation (against xsd/schemas specified as xsi:schemaLocation inside the XML itself)
    with Xerces via this wrapping validating class:
    XML::Validate
    which has 3 backends for validating, one being Xerces
    XML::Validate::Xerces

    Just a snippet of how such XML-docs are:
     
    <person xmlns="http://www.cafeconleche.org/namespaces/person"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.cafeconleche.org/namespaces/person
                          http://www.elharo.com/person.xsd">
      <name>
        <first_name>Alan</first_name>
        <last_name>Turing</last_name>
      </name>
       ...
    
    I'm trying now to move out from Xerces (libxml or anything else)
    The libxml shipped backend (of that XML::Validate):
    XML::Validate::LibXML
    doesn't seem anyhow able to manage the validation against xsd/schema but only against dtd.
    It doesn't seem this class is going to be updated either, as it was not since 2006.

    Is there any perl "standard" way nowadays to validate XML with bultin xsd/schemas in xsi:schemaLocation ?
    I would have chosen XML::LibXML::Schema
    which seems still the "standard" way. It also didn't receive updates since a while and it states:
    Currently it supports only schema parsing and document validation.
    
    I would then assume it doesn't support the validation from just the XML (with the inline xsi:schemaLocation)
    All the examples I've found are a 2 steps process:
    1. you must first know where the schema is (local or remote (if remote http only, not https)), to create an instance of XML::LibXML::Schema
    2. feed this created instance with the XML-doc, to validate it.

    So the only way to use that with inline xsi:schemaLocation, would seem to extract first them out from the XML-doc and get the url, create a validator instance with that location and then validate the XML-doc.
    Am I missing a proper way to use this class, or am I missing any other better class ? Thank you very much to everyone.
backslash found where operator expected at
4 direct replies — Read more / Contribute
by tokodekat
on Apr 21, 2021 at 16:51
    hai all

    I have error message regarding about Amazon SES ,

    Backslash found where operator expected at ./ses-get-stats.pl line 171 +, near "SES::call_ses \" (Do you need to predeclare SES::call_ses?) syntax error at ./ses-get-stats.pl line 171, near "SES::call_ses \" Global symbol "$response_code" requires explicit package name at ./ses +-get-stats.pl line 172. Global symbol "$response_content" requires explicit package name at ./ +ses-get-stats.pl line 174. Execution of ./ses-get-stats.pl aborted due to compilation errors.</p>
    The script is

    parse_args; validate_opts; prepare_params; my ($response_code, $response_content) = SES::call_ses \%params, \%opt +s; switch ($response_code) { case '200' { # OK print_response $response_content; exit 0; } case '400' { exit 1; } # BAD_INPUT case '403' { exit 31; } # SERVICE_ACCESS_ERROR case '500' { exit 32; } # SERVICE_EXECUTION_ERROR case '503' { exit 30; } # SERVICE_ERROR else { exit -1; } }
    can someone told me what should I do with this backslash \%params and \%opts;
regex matches when it seems that it shouldn't
1 direct reply — Read more / Contribute
by Lotus1
on Apr 21, 2021 at 14:46

    I'm preparing to present an intro to Perl for some coworkers so I'm glad I found this problem before presenting. The regex in line 10 matches when it shouldn't. I had to stare at it for a while before figuring it out. I'm posting here to give the newbies or anyone else a chance to debug a silly problem.

    use warnings; use strict; $_ =''; # find empty lines. print " - empty line\n" if /^$/; # find lines that contain one or more whitespace characters. print " - contains whitespace 1\n" if m/^\s+$/ =~ $_; ## line 10 print " - contains whitespace 2\n" if m/^\s+$/;

    Running deparse on the script makes it obvious:

Test::Script debug execution via scripts_run
3 direct replies — Read more / Contribute
by dpopchev
on Apr 21, 2021 at 09:59

    I have a script which uses several input files, creates some temps and outputs others. It is tested via Test::Script and file content/creation is tracked via Test:Directory and Test::File.

    After adding a new feature, making sure the unit tests are passing, the script tests are failing. I attempt to debug by running the test file as  perl -d:pdbtk t/path/to/test.t but cannot dive into the script itself.

    It looks like the line executing the script from Tests::Script is the following:

    (${$opt->{stdout}}, ${$opt->{stderr}}) = capture { system(@$cmd) };
    but there is no way of 'stepping into' the execution.

    I tried setting breakpoing inside the script, when entering the file, with  $DB::single = 1 but no success.

    I tried adding debug flag to the  script_runs arguments, but it takes no effect.

    edit: formatting
Download big file in chunks with plack response
4 direct replies — Read more / Contribute
by Thenothing
on Apr 21, 2021 at 06:04
    Hello everyone I really tried to fix this problem but this time I need his help. This code work to download a file with little size:
    use strict; use warnings; use Plack::Response; use Path::Tiny; use File::MimeInfo; sub return_psgi { my $self = shift; my $path_file = shift; my $content_type = mimetype($path_file); $content_type = 'application/octet-stream' if not defined $content +_type; my $content = path($path_file); my $content_download = $content->slurp; require File::Spec::Unix; my $basename = $path_file; $basename =~ s|\\|/|g; $basename = ( File::Spec::Unix->splitpath($basename) )[2]; $basename =~ s|[^\w\.-]+|_|g; my $response = Plack::Response->new(200); $response->headers( [ 'Content-Type' => $content_type, 'Content-Disposition' => qq[attachment; filename="$basename +"], ], ); $response->body($content_download); return $response->finalize; }
    But I want to download big files greater of 1GB, so change to download in chunks, to this code:
    open (FILE, "< $path_file") or die "can't open $path_file: $!"; binmode FILE; local $/ = \102400; while (<FILE>) { my $var = scalar $_; $response->body($var); return $response->finalize; } close FILE;
    Another intent, using autoflush:
    use IO::Handle; my $BLOCK_SIZE = 1024 * 1024; open(my $fh, "<:raw", $path_file) or die "Can't read from input file $ +path_file $!\n"; while (read($fh, $buffer, $BLOCK_SIZE)) { $response->body($buffer); $fh->autoflush; } close $fh; return $response->finalize;
    The 2 previous intentions download file a size of kb, corrupt file.

    can you please tell me where is my error in my code?
Calling Ajax
3 direct replies — Read more / Contribute
by htmanning
on Apr 21, 2021 at 02:55
    Monks, Our server was upgraded yesterday and a new version of Perl (5.30) was installed. We previously had 5.22. We were calling Ajax like this:
    use CGI::Ajax;
    After the upgrade we received this error:
    Can't locate CGI/Ajax.pm in @INC (you may need to install the CGI::Aja +x module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/p +erl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_64-linux-gnu/per +l5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.30 /usr/sha +re/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl- +base) at log.pl line 31.
    It seems like Ajax is not a default module in 5.30 so it was not automatically installed, but the server company installed the Ajax module for us. The site was still broken so they reverted the server back to 5.22 until we can resolve this. Is there a big difference in Ajax in the new version of Perl or could something else be at issue? I know that's a vague question but when the server gurus couldn't figure it out it left us at a loss. 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":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.