Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

The Monastery Gates

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

Donations gladly accepted

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

New Questions
Post using LWP useragent curl post request to useragent request
2 direct replies — Read more / Contribute
by smarthacker67
on May 25, 2018 at 17:31
    Hi Monks I am trying to create equivalent post request using LWP::Useragent
    curl ENDPOINT \ -H "Authorization: Bearer ACCESS_TOKEN" -X POST \ -F attributes='{"name":"test.txt", "parent":{"id":"0"}}' \ -F file=@test.txt
    my request looks like this
    my $json = encode_json {"name" => "test.txt", "parent" => {"id" => 0}} +; my $resp = $ua->post( ENDPOINT , {file => <$fh> ,'attributes'=>$json }, 'Authorization' => 'Bearer ' . $token , Content_Type => 'multipart/form-data' . );
    but not working getting 400 response Can some one help me. I am actually trying to upload the file to the endpoint with the curl I am able to but looking for equivalent LWP useragent request.
Looking for combinatorics with state
6 direct replies — Read more / Contribute
by Anonymous Monk
on May 25, 2018 at 16:19
    I'm working a a password recovery program and make use of various combinatorics libraries (Algorithm::Combinatorics, Algorithm::Permute, Set::Product). Depending upon the input, the program can run for days or weeks. I often wish that I could restart a job from a saved state or distribute the processing to multiple machines, but none of these libraries provide access to their state. In fact, I couldn't find a combinatorics library in any language that does. I would think this is not an uncommon task. Is it not easy to accomplish or not possible?
[OT] What is 'Good Practice' use of an .ini/.conf file: Database or Active Document?
4 direct replies — Read more / Contribute
by ozboomer
on May 25, 2018 at 00:30

    Howdy, all..

    Not really a Perl question :(... but I don't know how to even title my query...

    There's a text file with lots of sections in it, like an '.ini/.conf' file. Each section can be 'used' or 'unused' by its application.

    Some folk want to use the text file as 'storage', keeping ALL possible items in it, even though they're not in current use (a database); others call it an 'active document' that should only contain 'in-use' sections.

    Is there a recognized/recommended 'good practice' for this sort of thing?

    Thanks for any thoughts...

XS unpackstring, flags and useage
1 direct reply — Read more / Contribute
by coyocanid
on May 24, 2018 at 19:09
    Hello Honored Geschwistern,

    I am working on a perl XS project and would like to use unpack in the XS/C portion of the code. (Using pack in the perl portion seems to do what I want).

    I'm reading the API documents for unpackstring and have some questions questions

    • What are the flags that are allowed? I've been searching in vain
    • The documentation says that the results are put on the perl stack automatically. I don't see much explaining the perl stack in detail
    • How to get the return values from the unpacklist to the perl caller, possibly using the perl stack as a middleman.

    The engine implementing the unpack() Perl function.

    Using the template pat..patend, this function unpacks the string s..strend into a number of mortal SVs, which it pushes onto the perl argument (@_) stack (so you will need to issue a "PUTBACK" before and "SPAGAIN" after the call to this function). It returns the number of pushed elements.

    The strend and patend pointers should point to the byte following the last character of each string.

    Although this function returns its values on the perl argument stack, it doesn't take any parameters from that stack (and thus in particular there's no need to do a PUSHMARK before calling it, unlike "call_pv" for example).

    I32 unpackstring(const char *pat, const char *patend, const char *s, const char *strend, U32 flags)
pp bleach main script and modules
1 direct reply — Read more / Contribute
by Anonymous Monk
on May 24, 2018 at 14:27

    Hello monks,

    I have a script and a couple of my own modules I want to use to create an executable with pp. I want to hide my code though. I know: nothing can be hidden in a proper way, and and and. We do not need to start the discussion again. I prefer to have my .pl script and my .pm modules hidden, at least to the curious eye. I normally use the following:

    pp -u -f Bleach -o executable

    This works just fine, however only is "bleached", not my own modules. Is there a way to bleach also some modules (not all core modules!). Or there are others method to make my code not readable to the curious eye (again, I know that if something wants to read it... he/she will read it). perl2exe makes this possible (even if it is possible with some work to get the original code)

Subtle(?) issue(?) with lvalues and serialization
3 direct replies — Read more / Contribute
by vr
on May 24, 2018 at 09:08
    use strict; use warnings; use feature 'say'; use Sereal qw/ encode_sereal decode_sereal /; use Storable qw/ freeze thaw /; say 'Storable: ', ${ thaw freeze \substr 'abc', 0 }; say 'Sereal: ', ${ decode_sereal encode_sereal \substr 'abc', 0 }; __END__ >perl Use of uninitialized value in say at line 8. Storable: Sereal: abc

    I was passing lists of string references to mce_map for parallel processing. Because strings are substrings of larger strings, I thought to take shortcut by not creating temporary scalars, but to pass references of substr return values directly. Rather, refs are collected in large array (why whould I store duplicates?), then this array becomes parameter for mce_map. I should have known they are not references to scalars, but to lvalues, though.

    Everything works fine until it doesn't, on a particular machine -- without, it turns out, Sereal installed. Storable, used by MCE in lieu of Sereal, treats these lvalue refs differently.

    I didn't find anything googling for "Perl Storable lvalue", etc. Apart from my sloppy coding, I'm not even sure, if it's Storable issue, Sereal issue, or something else (maybe, MCE should check these?). Kind of, Sereal can be faster drop-in replacement for Storable. Except, it looks, not always. Any thoughts?

Media Wiki API 'WrongToken'
3 direct replies — Read more / Contribute
by Random_Walk
on May 24, 2018 at 04:11

    Good $localtime dear monks

    I am using the MediaWiki API to create a page updating bot. I got through some problems getting my headers accepted thanks to help from bliako in the thread Rest::Client Headers not getting through. But now I still can't get wiki to accept the token it gives me. From some googling I understand you need to get a token, then hand it over along with a login request to link that token to a logged in session. This is all I'm trying here, but when I send the token I just got, I get the error 'WrongToken'

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use MIME::Base64; use JSON; use REST::Client; use Getopt::Long; use HTTP::Cookies; use URI::Escape; use Sys::Hostname; $|++; # Output buffering off my $wiki = 'https://wikihost'; my $api = '/mediawiki/api.php'; my $JSON = JSON->new->allow_nonref; # Create rest client and set some options my $client = REST::Client->new(); $client->setHost($wiki); my $ua = $client->getUseragent(); # Get the LWP User agent $ua->ssl_opts(verify_hostname => 0); my $cookie_jar = HTTP::Cookies->new( file => "/tmp/lwp_cookies.dat", autosave => 1, ignore_discard => 1, ); $ua->cookie_jar( $cookie_jar ); # Give the chap a cookie # First get a token. This works my $request = 'action=query&meta=tokens&type=login&format=json'; my $result = post($request); print Dumper $result; my $token = uri_escape( $result->{query}{tokens}{logintoken} ); print "URI Escaped Token: $token\n"; # Now we log in with that token. I've tried both the below requests $request = 'action=login&lgname=Marvin&lgpassword=ldiode&lgtoken=$toke +n&lgdomain=wikihost&format=json'; # $request = 'action=login&lgname=Marvin&lgpassword=ldiode&lgtoken=$to +ken&format=json'; $result = post($request); print Dumper $result; sub post { my $query = shift @_; $client->POST($api, $query, {'Content-type' => 'application/x-www- +form-urlencoded'}); if ( $client->responseCode() == 200 ) { return $JSON->decode( $client->responseContent() ); } else { print "Failed with code $client->responseCode()\n"; print Dumper $client->responseContent(); } }
    And this is the output I get
    ./ $VAR1 = { 'batchcomplete' => '', 'query' => { 'tokens' => { 'logintoken' => '3de22ae8b84f7190 +35b25e69ae7b33655b06705a+\\' } } }; URI Escaped Token: 3de22ae8b84f719035b25e69ae7b33655b06705a%2B%5C $VAR1 = { 'login' => { 'result' => 'WrongToken' } };
    Does anyone see any obvious errors in my code, or know the wiki API well enough to spot my error. The one thing I am not sure about is cookie handling. I have cargo culted that cookie setup from various example found on t'web. Should that cookie setup be sensible, or an I best to clear the cookie file between runs? Or have the cookies nothing to do with what's going on here?

    Thanks in Advance,

    Pereant, qui ante nos nostra dixerunt!
Sort text by Chapter names
7 direct replies — Read more / Contribute
by Bman70
on May 24, 2018 at 01:24
    Dear monks,

    Is there any way to take a block of text that includes different Chapter names, and then sort all of the text so that the Chapter names are in alphabetic order, while keeping the correct text with each Chapter?

    Example of the text I'm trying to sort:

    Chapter One There were lots of monkeys here and they ate all the bananas... lots more text up to hundreds of words. Chapter Nine This chapter has probably 1000 words. Chapter Two Here is the text in the second chapter... Chapter Five Here is the text in the fifth chapter... every chapter is of differing length, some long some short.

    What I would want is to sort these chapters to be in Alphabetical order.. they don't need to be in order numerically. I just want to know if there's a sort function that can arrange each title in order while preserving the text associated with each title. Resulting in something like:

    Chapter Five Here is the text in the fifth chapter... every chapter is of differing length, some long some short. Chapter Nine This chapter has probably 1000 words. Chapter One There were lots of monkeys here and they ate all the bananas... lots more text up to hundreds of words. Chapter Two Here is the text in the second chapter...

    The text is all in a text file. There are some newlines and a mix of letters and numbers.

    Thank you for any tips you can offer.

Perl app won't compile /run from cron
4 direct replies — Read more / Contribute
by dazz
on May 23, 2018 at 17:06
    Hi I have a perl script that runs OK from the terminal but won't run from cron. I get a logged error that looks like this:
    May 24 09:00:02 trackcam3 test_cron: Can't locate Image/ in @IN +C (you may need to install the Image::Grab module) (@INC contains: /e +tc/perl /usr/local/lib/arm-linux-gnueabihf/perl/5.24.1 /usr/local/sha +re/perl/5.24.1 /usr/lib/arm-linux-gnueabihf/perl5/5.24 /usr/share/per +l5 /usr/lib/arm-linux-gnueabihf/perl/5.24 /usr/share/perl/5.24 /usr/l +ocal/lib/site_perl /usr/lib/arm-linux-gnueabihf/perl-base) at /home/d +arren/ line 33, <DATA> line 1. May 24 09:00:02 trackcam3 test_cron: BEGIN failed--compilation aborted + at /home/darren/ line 33, <DATA> line 1.
    So I ran:
    darren@trackcam3:~ $ cpan Image::Grab Loading internal null logger. Install Log::Log4perl for logging messag +es Reading '/home/darren/.cpan/Metadata' Database was generated on Wed, 23 May 2018 19:54:03 GMT Image::Grab is up to date (1.4.2).
    This looks like cron doesn't have the right path in the environment
    The path statement in crontab looks like:
    PATH=/home/darren/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin +:/usr/bin:/sbin:/bin # PATH =/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    The uncommented path is the same as the one in my user profile. The commented path is the one that has worked for years.
    This problem started when I build a new server from scratch using a documented procedure. Something I have done quite a few times. Something new has happened during the build. I don't know what that was and I don't know how to solve this problem.

problem with simple STDIN to UART script
1 direct reply — Read more / Contribute
by CSTJr
on May 23, 2018 at 12:42

    I've written a little keyboard to UART script as a first step to something more complex. The following code will let me send data from a PuTTY terminal over the serial connection, display it (STDOUT), and send a period back over the UART. In other words, the UART connection is working.

    However, if I uncomment the "my $outchar=<>;" line, everything stops working. I suspect this is because it's blocking. I've also tried replacing the line with a "my $outchar=getc();" line. This works a little better in that the characters coming in over the serial port eventually print, but only if a couple characters are typed on STDIN. This despite the `stty cbreak` instruction.

    use warnings; use strict; use IO::Termios (); $| = 1; my $fh = IO::Termios->open('/dev/ttyAPP4', '115200,8,n,1') or die "IO::Termios->open: $!"; `stty cbreak`; print $fh "Begin\n\r"; while(1){ my $inchar=<$fh>; # my $outchar=<>; if($inchar){ print $inchar; print $fh "."; } # if($outchar){ # print $fh $outchar; # } }

    Is there a non-blocking approach to doing what I'm after? I managed to get the IO:Termios module installed, though it was a little nightmarish. I dread having to add another module. :p

    Thanks in advance for any advice!

Using Tk 8 menubar in its own window
1 direct reply — Read more / Contribute
by chrstphrchvz
on May 23, 2018 at 07:28

    Howdy, Monks!

    Recently I've been trying to get a Perl/Tk program (HSW12, an IDE for programming microcontrollers) working with Tcl::pTk::TkHijack. One reason for doing so is letting the program be run on macOS using the native aqua UI instead of XQuartz. (Here's the full story on GitHub.)

    One step I've taken is adapting the menubar to use the Tk 8 menu (cf. Mastering Perl/Tk - "12.2 Menubars and Pulldown Menus") rather than by packing Menubuttons. That way the native menubar at the top of the Mac's screen gets used instead of a menu in a window. However, the layout of the program has been such that the menubar is in its own window. When using the Tk 8 menu on non-Mac aqua, there is now a small amount of extra space (about 30 pixels vertically); and because there are no other widgets in the window, there doesn't appear to be anything to e.g. pack and remove the extra space. (See the bottom of this GitHub comment for a screenshot.)

    Any ideas for how to remove this empty space/a way to force "pack" without widgets?

    Some things I've already tried: I haven't found a suitable dummy widget that will shrink beyond some minimum size ≫0 (again, roughly 30 pixels). I thought I might set the toplevel's maxheight to the height of the menu, but I haven't found where that can be retrieved (the width and height of the menu is always 1x1 according to $menu->geometry). I'd rather not hardcode a height because the menu's height varies by platform and user theming/customizations.

    Of course, one workaround would be to only use the Tk 8 menubar if running on Mac aqua (hiding the now-empty window using withdraw), and use the packed Menubuttons otherwise. One day I might use a module like Tk::IDElayout (compare to dock windows/panels in newer UI toolkits) which can let everything reside in a single toplevel window by default.

splitting a string on pre-defined tags
3 direct replies — Read more / Contribute
by rajaman
on May 22, 2018 at 16:03
    Hi Monks,

    $string='C*ID1*Mac*C release for EA's D*ID1*Spore1 game*D; D*ID1*Spore 1*D is better than D*ID2*Spore 2 game*D.';

    I am trying to split the above string and save the result in an array in such a way that each tagged segment is a separate array element. For example, 'C*ID1*Mac*C', 'D*ID1*Spore1 game*D', 'D*ID1*Spore 1*D', and 'D*ID2*Spore 2 game*D' should become four separate array elements.

    The usual splitting on space or '*' does not work, e.g.:

    @fields = split(/\s/, $string);

    Please suggest.

    Thank you.
Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2018-05-27 08:09 GMT
Find Nodes?
    Voting Booth?