Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

The Monastery Gates

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

If you're new here please read PerlMonks FAQ
and Create a new user.

Quests
poll ideas quest 2021
Starts at: Jan 01, 2021 at 00:00
Ends at: Dec 31, 2021 at 23:59
Current Status: Active
2 replies by pollsters
    First, read How do I create a Poll?. Then suggest your poll here. Complete ideas are more likely to be used.

    Note that links may be used in choices but not in the title.

Perl News
Pumpking resignation
on Apr 13, 2021 at 10:39
7 replies by davies

    This was discussed in the chatterbox yesterday. Analysis, with links for those who want to be even more depressed, is at https://www.theregister.com/2021/04/13/perl_dev_quits/. I wish I had the technical skills to volunteer.

    Regards,

    John Davies

    Update for thost of you suggesting tech skills aren't needed. I have never written a line of C. I have no idea what a makefile does. Spreadsheets running VBA or 123 macros are my comfort zone. And yes, I'm still comfortable with the /X versions of 123 macros.

The "A" in awk has gotten a special award
on Mar 31, 2021 at 20:04
1 reply by perlfan
Supplications
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;
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?
extracting only those keys with uniq values in array list
8 direct replies — Read more / Contribute
by v15
on Apr 19, 2021 at 19:08
    Hello everyone I am dealing with Hash of Arrays in Perl. Here is my code so far:
    while(<>){ chomp; my @s = split /\t/,$_; my $r_name = $s[0]; my $seq = $s[9]; push @{$read2seq{$r_name}}, substr($seq,0,12); }
    For every key($r_name) , I have 2 values. I want to print the names of those keys($r_name) where the 2 values for a particular key($r_name) are same. If the 2 values for the particular key are different from each other, then I do not want to output that key. I cannot use the module use List::MoreUtils qw(uniq) since it is not installed on my cluster. How can I do this?
How do I get a list in a perl hash generated from an XML?
4 direct replies — Read more / Contribute
by PV66
on Apr 19, 2021 at 04:48
    Hi Community, Really hoping to get some perl-wisdom on this one, its my second day working on Perl today, and I'm just stuck with an issue that I can't seem to figure out.

    Basically I have an XML

    <root> <ref name="abc_sia_%version1.ref%"> <func envname = "test01" objectdir = "/home/pv66" base="default_771"/> </ref> </root>
    I want to get the ref name's value, that is "abc_sia_%version1.ref%" as my hash key and the corresponding elements of the tag in a list. So I used Simple XML's function
    my $xml = new XML::Simple; my $data = $xml->XMLin("/home/pv66", ForceArray => 1, KeyAttr => { ref + => "name"}); print Dumper ($data);
    But this gives me
    $VAR1 = { 'ref' => { 'abc_sia_%version1.ref' => { 'func' => [ { 'envname' => 'test01', 'objectdir' => '/home/pv66', 'base' => 'default_771' } ] } };
    And this extra level of hierarchy (after ref) is undesired. I've read at some places that XML::Simple is not the preferred module, and XMLLib should be used, but I couldn't find any resources that drew a parallel to the ForceArray attribute of the XML in method. The desired hash should be:
    $VAR1 = { 'abc_sia_%version1.ref' => { 'func' => [ { 'envname' => 'test01', 'objectdir' => '/home/pv66', 'base' => 'default_771' } ] } };
    Any help pertaining to this would be appreciated.
Why does $Config{ccflags} include "-fwrapv" on many gcc builds of perl ?
1 direct reply — Read more / Contribute
by syphilis
on Apr 19, 2021 at 00:58
    Hi,

    The -fwrapv documentation states:
    This option instructs the compiler to assume that signed arithmetic ov +erflow of addition, subtraction and multiplication wraps around using + twos-complement representation.
    I thought that this was the usual (default) behaviour, anyway. And some quick experimentation I've done supports that notion.
    So, why is the flag specified on so many gcc bulds of perl (eg Windows and Ubuntu) ?

    Is it just a case of deciding that the perl flags might as well be open and specific about this ?
    Perhaps -fwrapv is present simply to alert us to the fact that another option (-ftrapv) is available ? (Not that I think you'd want to build perl with -ftrapv)
    Or are there actually cases where -fwrapv is not the gcc default ?

    Cheers,
    Rob

    PS: For those curious, the -ftrapv documentation states:
    This option generates traps for signed overflow on addition, subtracti +on, multiplication operations.
    The documentation also points out that -ftrapv and -fwrapv override each other - ie, you can have one or the other, but not both.
Injecting a value into HTTP::Header
1 direct reply — Read more / Contribute
by Bod
on Apr 18, 2021 at 16:52

    As part of connecting to LinkedIn using LWP::Authen::OAuth2, I have decided to write a sub-class of LWP::Authen::OAuth2::ServiceProvider which is designed to be sub-classed for exactly this kind of application. This way, it will hopefully be useful to other people.

    However, I need to override a method which has not been designed to be overridden! The LinkedIn API doesn't comply with the OAuth2 spec. The token_type parameter is mandatory but missing. Therefore, I am trying to inject it into the response from LinkedIn by sub-classing the method, adding the missing parameter and then calling the method in the super class. But I am not managing to get the injection to work.

    Here is the sub in LWP::Authen::OAuth2::ServiceProvider that I am overridding:

    The author has commented the point it fails as # Someone failed to follow the spec...!

    This is my sub that overrides the above...

    sub construct_tokens { my ($self, $oauth2, $response) = @_; my $content = eval {$response->decoded_content}; eval {decode_json($content)}; $response->push_header( 'token_type', 'Bearer' ) unless $@; $self->SUPER::construct_tokens($oauth2, $response); }
    I'm trying to set token_type as Bearer so that the rest of the sub in the superclass doesn't complain.

    Is there a good way to to inject this parameter or am I approaching this in the wrong way?

Disable Plack::Middleware::AccessLog
2 direct replies — Read more / Contribute
by 1nickt
on Apr 17, 2021 at 20:09

    Learned friends,

    The doc for Plack::Middleware::AccessLog states This middleware is enabled by default when you run plackup as a default development environment.

    I am wanting to run another middleware for logging and thus wish to disable the default. Can anyone tell how?

    Thanks!


    The way forward always starts with a minimal test.
Making distribution on Strawberry Perl for CPAN
2 direct replies — Read more / Contribute
by Bod
on Apr 16, 2021 at 19:09

    I've uploaded Business::Stripe::WebCheckout to CPAN as a developer release. Release 1 failed when Stripe couldn't be contacted. Release 2 has cured this problem and, so far, all the CPAN Testers reports have passed 😊

    However, Windows doesn't have gzip. So in Makefile.PL I have this line to use IO::Compress::Gzip which is included with Strawberry Perl:

    dist => { COMPRESS => q{perl -MIO::Compress::Gzip=gzip,:constants -e" +my $$in = $$ARGV[0]; gzip($$in => qq($$in.gz), q(Level) => Z_BEST_COM +PRESSION, q(BinModeIn) => 1) or die q(gzip failed); unlink $$in;"}, S +UFFIX => 'gz', },
    This is included in the tarball that was uploaded.

    Does this need changing in the release?
    My best guess is that it is OK as it is only used when building the distribution, not when installing it but I wanted to check this is correct.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2021-04-23 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?