Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
Media Wiki API 'WrongToken'
2 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
6 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.
[CLOSED] Dereference hash into hashes by key
3 direct replies — Read more / Contribute
by mlodato
on May 22, 2018 at 12:04
    O Wise Ones,
    Is there a one-line way to do something along the lines of:
    my $hash = { key1 => 'value1', key2 => 'value2', }; my ($hash1, $hash2) = _insert_perl_magic_;
    Such that then $hash1 becomes {key1 => 'value1'} and $hash2 becomes {key2 => 'value2'}?

    The closest I've gotten is:

    my @hashes = map { {$_ => $hash->{$_}} } qw(key1 key2); my ($hash1, $hash2) = @hashes;
    But it doesn't seem to work in one line (probably because of some lazy generation or something (I confess I don't truly understand how map works)

-T changed behavior
4 direct replies — Read more / Contribute
by tothestars
on May 22, 2018 at 02:06

    Hi, I am not a Perl programmer but I have a Perl issue that needs to be resolved and will greatly appreciate your help.

    Here’s the case:

    perl -le 'print "Version $] : ", -T "spk_pre_180208_180715_180419_bpo1 +2_p.bsp" ? "text" : "not text"' Linux: Version 5.010001 : not text Version 5.014002 : not text Version 5.016003 : not text Version 5.026001 : tex

    Somewhere (between version 5.20.3 and 5.22.4) there is a Perl patch causing a change in -T's behavior. A file that was previously evaluated as Binary is now evaluated as text.

    Any insight?

    Thanks so much for your help!

    2018-05-22 Athanasius added code and paragraph tags

XMLOut in XML::Simple returning unwanted xml format
3 direct replies — Read more / Contribute
by perl_help27
on May 21, 2018 at 10:10
    Hello Monks, I kind of need your help. I have the following code:
    use XML::Simple; use Data::Dumper; my $hash= { 'root' => { 'item1' => { 'item1a' => 'n1', 'item1b'=>'jh' } } }; my $xs = new XML::Simple; my $xml = $xs->XMLout($hash, NoAttr => 1, RootName=>undef,); print Dumper $xml;
    This is outputting :
    <root> <name>item1</name> <item1a>n1</item1a> <item1b>jh</item1b> </root>
    But I want the xml to be ALL nested like that:
    <root> <item1> <item1a>n1</item1a> <item1b>jh</item1b> </item1> </root>
    What do you think I should do?? Please help I am stuck :S Thanks
perltidy and UTF-8 BOM
2 direct replies — Read more / Contribute
by morelenmir
on May 21, 2018 at 00:07

    As part of the shift to working with a plain text editor I would like to employ the 'perltidy' batch file and script to ensure my code is readable and consistent in presentation. However I have encountered a problem in doing so.

    I use UTF-8 encoding with a matching BOM for all my Perl sources. Unfortunately when I run 'perltidy' on a file with this layout I recieve the following error:

    Perltidy version is 20180220 1: unexpected character decimal 239 (ï) in script 1: unexpected character decimal 187 (») in script 1: unexpected character decimal 191 (¿) in script 1: Giving up after error

    If I remove the BOM, but otherwise keep the UTF-8 encoding then 'perltidy' works as expected. Alternately if I set the -UTF8 switch then I get both the error message and the entire source file becomes double-spaced on new lines. Is there any way around this problem?

    "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
Problems starting the debugger
1 direct reply — Read more / Contribute
by morelenmir
on May 19, 2018 at 21:58

    Hey Guys!

    I am in the process of getting back in to Perl after being away from programming as a whole for quite a while. I thought this might be a good time to learn how to use the built-in debugger. Previously I ran 'EPIC' inside 'Eclipse', but ended up very much disliking that IDE. Therefore this time around I intend to write programmes in a text editor called 'EditPad Pro' and then employ the native Perl debugger as necessary. Unfortunately I have run in to some problems straight out of the gate,

    As a test I started with the simplest of all one-line programmes, saved as '':

    say "Hello";

    Next, at the console window I used the command:

    perl -d

    This is the output I received from Perl:

    Loading DB routines from version 1.51 Editor support available. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. <main::(test.plx:1): print "hello"; Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call + didn't work. The COLUMNS and LINES environment variables didn't work +. at C:/StrawberryPERL/perl/vendor/lib/Term/ReadLine/ line 41 +0. at C:/StrawberryPERL/perl/vendor/lib/Term/ line 462. Term::ReadKey::GetTerminalSize(GLOB(0x28025f4)) called at C:/S +trawberryPERL/perl/vendor/lib/Term/ReadLine/ line 410 readline::get_window_size called at C:/StrawberryPERL/perl/ven +dor/lib/Term/ReadLine/ line 1114 readline::init called at C:/StrawberryPERL/perl/vendor/lib/Ter +m/ReadLine/ line 208 require Term/ReadLine/ called at C:/StrawberryPERL/ +perl/vendor/lib/Term/ReadLine/ line 63 eval {...} called at C:/StrawberryPERL/perl/vendor/lib/Term/Re +adLine/ line 63 Term::ReadLine::Perl::new("Term::ReadLine", "perldb", GLOB(0x2 +8b27bc), GLOB(0x28025f4)) called at C:/StrawberryPERL/perl/lib/perl5d line 6868 DB::setterm() called at C:/StrawberryPERL/perl/lib/ +line 1849 DB::_DB__read_next_cmd(undef) called at C:/StrawberryPERL/perl +/lib/ line 2786 DB::DB called at test.plx line 1 SetConsoleMode failed, LastError=|6| at C:/StrawberryPERL/perl/vendor/ +lib/Term/ line 346. at C:/StrawberryPERL/perl/vendor/lib/Term/ReadLine/ line 1 +581. readline::readline(" DB<1> ") called at C:/StrawberryPERL/per +l/vendor/lib/Term/ReadLine/ line 11 Term::ReadLine::Perl::readline(Term::ReadLine::Perl=ARRAY(0x61 +1b1c), " DB<1> ") called at C:/StrawberryPERL/perl/lib/ li +ne 7367 DB::readline(" DB<1> ") called at C:/StrawberryPERL/perl/lib/ line 1858 DB::_DB__read_next_cmd(undef) called at C:/StrawberryPERL/perl +/lib/ line 2786 DB::DB called at test.plx line 1

    I am using a fresh install of the newest Strawberry Perl (32Bit), which is release I run this in Windows 7 (64Bit), patched with the latest updates. For what it is worth the same error occurs if I try the 64Bit edition of Strawberry Perl either. Other than this, Perl programmes themselves run without any problem. It is only when I try to execute them under the native debugger that I encounter an issue.

    Can any of you chaps suggest a solution for this?

    A quick search of the forum came up with a very similar issue reported by 'Ovid' way back in 2007. However that gentleman encountered the error while employing something called 'Prove', which I have never come across before and is certainly not something I am using myself. I think most of the suggestions in that thread related to using 'Prove', so I do not know how to apply them in my own far simpler situation.


    Well... A degree more persistence with the search function, both here and over at Google suggested another approach to sort this out that was not centred on 'Prove'; create an environment variable 'TERM' and set its value to 'dumb' (case sensitive for each I believe). After doing so perl -d began working like a charm!!! So... I guess that is the fix. Which is good of course, but I have no idea why I couldn't find that result the first half-dozen times I searched for an answer... Weird indeed. Still. The jobs a good'un--problem solved!

    "Aure Entuluva!" - Hurin Thalion at the Nirnaeth Arnoediad.
How do I use "Long Doubles" in perl?
3 direct replies — Read more / Contribute
by cnd
on May 19, 2018 at 10:46
    My perl has them:
    #perl -e 'use Config;print "long doubles\n" if $Config{d_longdbl} eq " +define";' long doubles
    But they are not default:-
    # perl -e 'use Config;if ($Config{uselongdouble} eq "define") {print " +long doubles by default\n";} else {print "not default? How to use???\ +n"}' not default? How to use???
    So how do I force their use?

    I do not want to use external modules, and yes, I know all about base-2 and float precision.

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 having an uproarious good time at the Monastery: (9)
As of 2018-05-24 17:55 GMT
Find Nodes?
    Voting Booth?