Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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
How do I get a list in a perl hash generated from an XML?
2 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.

Obtaining OAuth2 Access Token
2 direct replies — Read more / Contribute
by Bod
on Apr 16, 2021 at 10:07

    I am trying to connect to LinkedIn using LWP::Authen::OAuth2. Authorisation goes fine but when I come to exchange the authorisation token for an access token, I get this error:

    Endpoint: https://api.linkedin.com/v2/accessToken JSON: { "serviceErrorCode":65604, "message":"Empty oauth2 access token", "status":401 }
    That doesn't seem to make alot of sense to me as I would expect the OAuth2 Access Token to be empty in a request to get it!

    This is the bare bones of what I am doing...

    my $linkedin = LWP::Authen::OAuth2->new( client_id => 'xxxxxxx', client_secret => 'xxxxxxx', authorization_endpoint => 'https://api.linkedin.com/uas/oauth2/a +uthorization', token_endpoint => 'https://api.linkedin.com/v2/accessTok +en', redirect_uri => "https://$ENV{'HTTP_HOST'}/cgi-bin/pos +tdog.pl?command=authorize_linkedin", scope => 'w_member_social', save_tokens => \&save_linkedin_token, ); ######################### # LinkedIn button clicked sub linkedin { my $auth_url = $linkedin->authorization_url; print "Location: $auth_url\n\n"; exit 0; }
    The code above behaves as expected by going off to LinkedIn, authorising the app and calling the callback URL
    The callback URL does this:
    sub authorize_linkedin { my $token = $linkedin->request_tokens( code => $data{'code'}, ); print "Content-type: text-plain\n\n"; print "ERROR: $data{'error'}\n\nMessage: $data{'error_description' +}\n\n"; print "TOKEN: $token\n"; print $data{'code'}; exit 0; }
    The error (above) is generated at the request_tokens call. $data{'code'} contains the code passed as a query parameter to the callback URL.

    I feel I must be missing something obvious here...

(Windows) verbatim command line arguments inside Perl script
5 direct replies — Read more / Contribute
by LanX
on Apr 16, 2021 at 08:37
    Hi

    I need to write a Perl script which is generating a .bat-file while passing it's command line arguments 1-to-1

    Is there any way to access the "raw" command-line in order to catch quoting and escaping?

    To highlight the problem:

    D:\>perl -E"say qq(@ARGV)" "1 2" "3" 4 1 2 3 4

    As you can see are all quotes and escapes lost while calling Perl (for good reasons)

    But my script has to generate another .bat which does

    ... some_cmd "1 2" "3" 4 ...

    Typical edge-case justifying this problem are win-paths with whitespace, but cmd.exe has also some arcane escaping rules.

    I already have a workaround:

    I discovered that .bat has %* to hold the verbatim command line arguments, and came up with a workaround of a .bat calling my perl-script

    D:\tmp\exp>type cmd_wrapper.bat @echo off echo %* |perl -wE"chomp(my $args = <STDIN>);say qq(some_cmd $args)" D:\tmp\exp>cmd_wrapper.bat "1 2" "3" 4 some_cmd "1 2" "3" 4 D:\tmp\exp>

    Question: any better solution?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    PS: I don't think this question is overly Win specific, when creating a bash script on Linux I'd face the same problem.

DateTime throwing an error
1 direct reply — Read more / Contribute
by nachtmsk
on Apr 15, 2021 at 20:16

    Hi,

    I have a script in cron that I haven't changed in months. I use the DateTime module in the script. This afternoon, the script stopping running properly. I ran the script manually and received the error below.

    Unrecognized character \x0E; marked by <-- HERE after warnings;<-- HE +RE near column 14 at /usr/local/share/perl/5.28.1/Role/Tiny/With.pm l +ine 4. Compilation failed in require at /usr/local/share/perl/5.28.1/Specio/C +oercion.pm line 10. BEGIN failed--compilation aborted at /usr/local/share/perl/5.28.1/Spec +io/Coercion.pm line 10. Compilation failed in require at /usr/local/share/perl/5.28.1/Specio/D +eclare.pm line 11. BEGIN failed--compilation aborted at /usr/local/share/perl/5.28.1/Spec +io/Declare.pm line 11. Compilation failed in require at /usr/local/lib/arm-linux-gnueabihf/pe +rl/5.28.1/DateTime/Types.pm line 12. BEGIN failed--compilation aborted at /usr/local/lib/arm-linux-gnueabih +f/perl/5.28.1/DateTime/Types.pm line 12. Compilation failed in require at /usr/local/lib/arm-linux-gnueabihf/pe +rl/5.28.1/DateTime/Duration.pm line 12. BEGIN failed--compilation aborted at /usr/local/lib/arm-linux-gnueabih +f/perl/5.28.1/DateTime/Duration.pm line 12. Compilation failed in require at /usr/local/lib/arm-linux-gnueabihf/pe +rl/5.28.1/DateTime.pm line 14. BEGIN failed--compilation aborted at /usr/local/lib/arm-linux-gnueabih +f/perl/5.28.1/DateTime.pm line 14. Compilation failed in require at processcamerathree.pl line 8.

    I thought possibly something because corrupt so I tried to reinstall DateTime using cpan.
    When I do that I get a lot of the same error as above.

    Any suggestions on how to proceed?

    Thanks!
    Mike

Mojolicious-Hypnotoad-Nginx - rendering images
4 direct replies — Read more / Contribute
by pdkakoba
on Apr 15, 2021 at 09:24

    Kindly seeking assistance in rendering images (any static file from /public) in a default Mojolicious-Hypnotoad-Nginx setup. Following the standard guides, this setup successfully renders the default welcome page. However adding one exception: a .png image to this page, it is not rendered, even the favicon.ico doesn't appear.

    There is mention of "hooks" needed to correct for the workings of a Reverse Proxy, but after a long search one doesn't find a clear full example -- particularly for Nginx.

    The welcome.html.ep has this simple addition: tree. It appears alone as an image when one enters this url: http://example.com/tree.png

SerialPort issues under Windows 10
3 direct replies — Read more / Contribute
by clinicalAD
on Apr 15, 2021 at 06:46
    Hello, Am trying to use Win32::SerialPort on a portable strawberry perl implementation, version 5-32-1. (32 bit). Using it to try and set up a datalogger on a Win10 machine, for an instrument that reports through a RS232 output. Also have a general test bed which is two windows PCs connected by a serial cable, and one sending messages (from Powershell) to the other - which testing has shown acts in the same way as the end use. Have come across a variety of scripts online. For some reason though, I can't get any Perl script to output the RS232 input on its own. However, if I open and close the port in powershell, suddenly the exact same Perl script DOES work! The perl script continues to work as expected until the PC is rebooted. So I'm wondering if there's a key parameter needing to be set, which is sorted by Powershell but not by Perl? Couldn't see anything obvious in the SerialPort documentation. An example script that only works when powershell has "activated" the port is:
    use Win32::SerialPort; # Use instead for Windows my $port=Win32::SerialPort->new("COM1") || die "couldn't open COM1"; $port->baudrate(9600); # Configure this to match your device $port->databits(8); $port->parity("none"); $port->stopbits(1); #$port->handshake("none"); $port->user_msg(ON); print "Port COM1 successfully opened\n"; $count = 0; my $chars=0; my $buffer=""; print "1\n"; do { print "2\n"; my ($count,$saw)=$port->read(255); # will read _up to_ 255 char +s print "3\n"; if ($count > 0) { $chars+=$count; $buffer.=$saw; print $saw; } print "4\n"; $port->lookclear; print "5\n"; } while 1; $port->close;
    This code stops after printing "2" if I've not opened/closed the port in Powershell yet - it's the reading of RS232 input where the script seems to get blocked quite consistently across different scripts. Powershell code to open and close is:
    $port = new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one $port.open() $port.close()
    Powershell to send RS232 on test bed is: $port.WriteLine("test") Unfortunately access permissions do not let me run a powershell script, and I'd rather not have the end user have to manually type in powershell code each time the PC is turned on... Many thanks for any advice provided.
Crypt-Random-Source: 0.14 fails to install
2 direct replies — Read more / Contribute
by chandantul
on Apr 14, 2021 at 09:02

    Hello All, I am getting the following error Crypt-Random-Source: 0.14 fails to install while installing OIDC::Lite. I am in Windows 10 platform

    Please check below errors

    t/sugar.t .............. 1/? Undef did not pass type constraint "Class +Name" (in $self->{"weak_source"}) at (eval 121) line 17 "ClassName" is a subtype of "Str" "Str" is a subtype of "Value" "Value" is a subtype of "Defined" Undef did not pass type constraint "Defined" (in $self->{"weak_sou +rce"}) "Defined" is defined as: (defined($_)) # Tests were run but no plan was declared and done_testing() was not s +een. # Looks like your test exited with 255 just after 3. t/sugar.t .............. Dubious, test returned 255 (wstat 65280, 0xff +00) All 3 subtests passed Test Summary Report ------------------- t/factory.t (Wstat: 65280 Tests: 1 Failed: 0) Non-zero exit status: 255 Parse errors: No plan found in TAP output t/sugar.t (Wstat: 65280 Tests: 3 Failed: 0) Non-zero exit status: 255 Parse errors: No plan found in TAP output Files=7, Tests=17, 4 wallclock secs ( 0.11 usr + 0.09 sys = 0.20 CP +U) Result: FAIL Lockfile removed. ETHER/Crypt-Random-Source-0.14.tar.gz C:\Strawberry\perl\bin\perl.exe ./Build test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports ETHER/Crypt-Random-Source-0.14.tar.gz Stopping: 'install' failed for 'Crypt::Random::Source'. Failed during this command: ETHER/Crypt-Random-Source-0.14.tar.gz : make_test NO

    Can you please help to fix?

Breaking symmetry with a regex
6 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 13, 2021 at 18:50

    I have a string that has the form 'a:b:' where 'a' and 'b' are both numbers. How do I drop the second colon, or convert it to // ?

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 meditating upon the Monastery: (4)
As of 2021-04-20 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?