Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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
Looking for a loop
1 direct reply — Read more / Contribute
by WisDomSeeKer34
on May 28, 2018 at 03:46
    The code says it all:
    #!/usr/bin/env perl use strict; use warnings; use utf8; use feature 'say'; sub choose { say "choose a number: 1 2 3 4 5 "} choose(); my $x = <STDIN>; say $x; if ($x<1 or $x>5) { say"You have chosen the wrong number"; choose(); $x = <STDIN>; }

    If you enter the wrong number, you get the new message again. But only once. I want the message to repeat until you type in the right number.

    I am working on this code, because I have still the Basic syntax in mind:

    if ($x<1 or $x>5) THEN GOTO 10

    and that doesn't work that way in Perl.

How to open file inside loops with file names generated using an array?
2 direct replies — Read more / Contribute
by skooma
on May 28, 2018 at 03:02
    I have a bunch of files in a folder.

    file_1.1_2.1.txt, file_1.2_2.2.txt and so on.

    I am reading those files and extracting the info from the file name file_<data1>_<data2>.txt. After getting the info it is assigned to array @data1 and @data2 . I only require files in this format, there are also other files which i dont need. I am trying open the files using foreach.
    foreach my $dat1 (@data1){ foreach my $dat2 (@data2){ open (FH, "<", "file_$dat1\_$dat2.txt") or die "a horrible death $! +"; .....do something...... } }
    If I run this I am getting error "No such file or directory." but running it outside the loop works. For Example.
    open (FH, "<", "file_1.1_2.1.txt") or die "a horrible death $!";
INSERT file contents inside a table
2 direct replies — Read more / Contribute
by theravadamonk
on May 28, 2018 at 02:17

    Hi Perl Monks,

    I hv a /tmp/quarantine.ok file". It's contents are given below

    May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz

    I am currently displaying its contents with CGI via web browser. This is my code.

    #!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing $ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin"; print "Content-Type: text/html; charset=utf-8\n\n"; print "<body bgcolor=\"#95B8DB\" onload=\"parent.adjustMyFrameHeight() +;\">"; print "<div style=\"min-height:500px; dispaly:block;\">"; open FILE, "/tmp/quarantine.ok" or die $!; while(<FILE>) { chomp; print "$_\n\n"; print "<br />"; print "<br />"; print " \n"; } close FILE; print "</div>"; print "</body>";

    Now, I want to INSERT file contents inside a table. I need these below table headers

    TIME CATEGORY Sender Recipients Quarantined-ID . Under those I want to display table data.

    Under time, Date and Time should displayed, Under CATEGORY, it's CATEGORY should be displayed ( whether its SPAM, BANNED or INFECTED). Under sender, email senders. Under recipients, recipients. Under Quarantined-ID, Quarantined-ID should be displayed.

    How can I achieve it?

    I came across this URL.

    http://www.perlmonks.org/bare/?node_id=86850

    But, I can't get it done.

    Your ideas r welcome.

what is "milliBarr"?
2 direct replies — Read more / Contribute
by morgon
on May 26, 2018 at 12:28
    Hi

    reading through the comments on https://log.perl.org/2018/05/goodbye-search-dot-cpan-dot-org.html I came across this:

    there's an old term in Perl, the 'milliBarr' which applies to Graham's particular style.
    "Graham" referring to Graham Barr.

    As I've never heard that "old" term before: Does anybody have any clue what it refers to?

    Many thanks!

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.
    unpackstring

    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 myscript.pl

    This works just fine, however only myscript.pl 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 180524.pl Use of uninitialized value in say at 180524.pl 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
    ./TestToken.pl $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,
    R.

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2018-05-28 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?