Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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
Rosetta Code Error or $n // 100
3 direct replies — Read more / Contribute
by pgmer6809
on Mar 24, 2019 at 15:58

    On Rosetta (https://rosettacode.org/wiki/Continued_fraction#Perl )

    there is the perl solution to evaluating Continuous fractions. The code given is: (<===== comments added by me)

    sub continued_fraction { my ($a, $b, $n) = (@_[0,1], $_[2] // 100); #<========= what is $_[2] // 100 ?? $a->() + ($n && $b->() / continued_fraction($a, $b, $n-1)); } printf "&#8730;2 &#8776; %.9f\n", continued_fraction do { my $n; sub +{ $n++ ? 2 : 1 } }, sub { 1 }; # <===== Note there is no third arg p +assed printf "e &#8776; %.9f\n", continued_fraction do { my $n; sub { $n++ + || 2 } }, do { my $n; sub { $n++ || 1 } }; # <===== Note there is no + third arg passed printf "&#960; &#8776; %.9f\n", continued_fraction do { my $n; sub { + $n++ ? 6 : 3 } }, do { my $n; sub { (2*$n++ + 1)**2 } }, 1_000; #<== +== Here there is a 3rd arg printf "&#960;/2 &#8776; %.9f\n", continued_fraction do { my $n; sub { + 1/($n++ || 1) } }, sub { 1 }, 1_000; #<==== Here there is a 3rd arg

    The output shown on Rosetta is correct

    .

    My TWO questions are:

    1) Why can they pass in two arguments sometimes (two function refs) and three arguments other times (two function refs, and an integer count?

    2) What value does the expression $_[2] // 100 assign to $n ?

Mojolicious::Plugin::OpenAPI strict or filter object representation
1 direct reply — Read more / Contribute
by Veltro
on Mar 24, 2019 at 11:33

    Dear monks,

    I am looking for a possibility to indicate that the schema definition must be 'exact' in the sense that if more parameters are specified I want the validation to 'fail' for the request, and 'fail' or 'filtered' for the response (hopefully by specifying this in the OpenAPI spec). I wonder if anyone knows if this can be done in swagger 2.0 (or newer versions)?

    As far as I understand now, swagger 2.0 allows you to tell that parameter are 'required' by defining the parameters like this (Just as an example a 'animal' object):

    # In a POST request: ... parameters: - in: body name: body schema: $ref: '#/definitions/animal' required: true ... # In a resonse: ... responses: 200: description: animal response schema: $ref: '#/definitions/animal' ... # and then create the animal definition: definitions: animal: description: animal properties: animalid: description: The animal ID type: integer animalname: description: The animal name type: string type: object required: [ 'animalid', 'animalname' ]

    However, there is nothing that stops anyone from sending an animal with many more parameters other than 'animalid' or 'animalname' or could even add completely unrelated data in the request. Similarly it also does not stop the back-end from creating an object containing many more parameters and send those to the client as well.

    What if I would send a 'user' object and I had accidentally created a database query resulting in an object containing the password as well (because let's say I was turning beer instead of coffee into code one night). I did not specify the password as a valid parameter in the spec, but it gets send anyhow! How could I potentially either block the entire response, or if desired, filter out the password and send the response with just those parameters that were specified in the spec?

    Another reason why this would be desired is to detect typos, In case a parameter is optional and e.g. called animalname, but is send as animalName, this is not detected. But when the definition would have been taken 'strict' then this typo would have been detected. The response error could say something like: animalName is not a valid parameter!

    I was digging through the entire OpenAPI spec, but have not been able to find anything. Has anyone found a solution for this?

    Thanks,

    Veltro

Can I use Mojo::IOLoop::Subprocess for a non-blocking process?
1 direct reply — Read more / Contribute
by talexb
on Mar 23, 2019 at 00:05

    I have a Mojolicious monitoring web app that's working well. I'd like to extend it to give it the ability to start scripts running.

    My code is something like this:

    use Mojolicious::Lite; use Mojo::IOLoop::Subprocess; ... post '/' => sub { ... my $message = 'INFO: Something useful'; my $cmd = 'cd /home/foo/bar && ./some-script.pl >some.out 2>some.e +rr'; my $subprocess = Mojo::IOLoop::Subprocess->new; $subprocess->run( sub { my $subprocess = shift; my $result = qx/$cmd/; return $result; }, sub { my ($subprocess, $err, @results) = @_; if ( $err ) { $message .= "ERROR: $err"; } else { $message = $results[0]; } } ); $subprocess->ioloop->start unless $subprocess->ioloop->is_running; ... $c->render(template => 'index'); };
    In the $cmd, I'm cd'ing into a directory and running a known script. The good news is that this works .. the script does actually run fine, and the output and error files are created correctly. What I'm not expecting is that the Mojolicious web app doesn't return immediately while the script runs; rather, it waits for the script to finish.

    The behaviour that I'd like is to see that the script started, with the web page refreshing a second or two later. At some point a couple of minutes later, the script will finish .. but I really don't care about the output.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

With substr, index the offset don't get proper start position
5 direct replies — Read more / Contribute
by Oskym
on Mar 21, 2019 at 06:26

    Hi PerlMonkeys!

    I'd made a simple script trying to get the final part from a line from the 4th space occurrence.

    The Output should be like this:

    "GetCertificate](3) Returning context. ID=PTWEBSERVER, Lang=ENG"

    Instead of that I'm getting this:

    "(6) 01/02/19 06:30:30 GetCertificate(3) Returning context. ID=PTWEBSERVER, Lang=ENG"

    Can I ask for help to any expert?

    Here is the code:

    use strict; use warnings; my $line='PSAPPSRV.26476584 (6) 01/02/19 06:30:30 GetCertificate(3) Re +turning context. ID=PTWEBSERVER, Lang=ENG'; my $SpaceChar = ' '; my $offset = 0; my $result = substr $line,index($line, $SpaceChar, $offset); while ($offset &lt; 4) { $result = substr $line,index($line, $SpaceChar, $offset); print "$result \n"; $offset++; print "offset: $offset \n"; }

    Thanks for your help!!

Reading and printing an XLSX file
2 direct replies — Read more / Contribute
by jsuresh
on Mar 21, 2019 at 03:03
    Hi Team, I'm trying to read an XLSX file and write it as XLSX file. I can read it but unable to print it as a new XLSX file. I have also used the required module XLSX::writer but still it is now allowing me to create a new xlsx file. I also need to to check for the values starting from 0.00 and replace it as '0.00'(just wanted to add a single quotes to it). please provide us a solution. It is not allowing me to proceed further.

    Thanks,

    use Spreadsheet::XLSX;

    my $excel = Spreadsheet::XLSX -> new ('/tmp/temp.xlsx');

    print $excel;

    my @array;

    my $workbook = Excel::Writer::XLSX->new( 'perl.xlsx' );

    #my $FILENAME="/tmp/Newfile.xls";

    my $workbook = Spreadsheet::WriteExcel->new("$FILENAME");

    my $worksheet1=$workbook->addworksheet("Worksheet1");

    $worksheet1->write( "A1", "Hi Excel!" );

    foreach my $sheet (@{$excel -> {Worksheet}}) {

    printf("Sheet: %s\n", $sheet->{Name});

    $sheet -> {MaxRow} ||= $sheet -> {MinRow};

    foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {

    $sheet -> {MaxCol} ||= $sheet -> {MinCol};

    foreach my $col ($sheet -> {MinCol} .. $sheet -> {MaxCol}) {

    my $cell = $sheet -> {Cells} $row $col;

    if ($cell) {

    printf("( %s , %s ) => %s\n", $row, $col, $cell -> {Val});

    my $temp=$cell->{Val};

    $worksheet1->write($row,$col,$cell -> {Val});

    # push(@array,$temp);

    # $sheet->write($row,$col,"CHECK");

    } }

    }

    last

making a markovian "mad lib"
2 direct replies — Read more / Contribute
by Aldebaran
on Mar 20, 2019 at 18:32

    Hello Monks,

    I'm trying something new that I've been bouncing around in my head since bliako posted his markovian meditation n-dimensional statistical analysis of DNA sequences (or text, or ...). bliako has all these interesting and abstruse scripts that are right on the edge of what I can replicate. I was able to do so once with the Markovian Frankentext of that thread but could not do it again. I want to reach for something much simpler, indeed as simple as I can imagine it as an SSCCE. With that as an introduction, I'll put a statement of the problem in readmore tags:

    Thanks for your comment

Split lines in file to columns
4 direct replies — Read more / Contribute
by Magnolia25
on Mar 20, 2019 at 05:35
    Hi Monks,

    Below are the lines in my file. I am reading them into array and storing column values into variables.

    ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription|RowCode|Suppli +er ID::Region ValuesInColumn1 DataColumnB XYZ RowDescription at RowCode ValuesInColumn1 DataColumnB ABC RowDescription at RowCode

    The following will take data from my file, split it.That works if the columns are divided on spaces. But value for colD is not captured correctly, as string in colD has sapces in between. If value for $colB = XYZ and $colD contains substring = Region , I need to replace the $colB from XYX to N/A.

    foreach my $line (@a) { my ($colA, $colB, $colC,$colD) = split( /\s+/, $line); #print "$colD \n"; }

    Please help.

circular area in a coordinates grid (AoA)
8 direct replies — Read more / Contribute
by Discipulus
on Mar 19, 2019 at 17:34
    Hello monks,

    I have an array of arrays full of o and, given the coordinates of one element and a radius I want to change all elements around the center into x

    My AoA can be eventually big so I do not want to process every element of the AoA.

    The sub ( illuminate in the example below ) must not complain if a point is outside of the AoA (for example giving a corner as vertex).

    A pure perl solution will be the best, but also using modules will be ok.

    Sorry if I only have the below sketch but I'm very scarce in trig and until now I can only figure to hardcode series of coordinates relative to the vertex (that is silly..)

    use strict; use warnings; my $max = 19; my @aoa = map { [ ( 'o' ) x ($max + 1) ] } 0..$max ; display( @aoa ); # @to_change will contain [row1,col1],[row2,col2]... my @to_change = illuminate( 5, 4, 6 ); sub illuminate{ my $center_row = shift; my $center_col = shift; my $radius = shift; ... } sub display{ foreach my $row ( @_ ){ foreach my $col ( @$row ){ print $col; } print "\n" } }

    Thanks

    L*

    PS the above code displays the AoA before the change: I want to be able to select into @to_change the serie of elements falling inside the circle.

    minor changes to explain better

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
mod_perl sharing modules with other accounts when I don't want it to
1 direct reply — Read more / Contribute
by ultranerds
on Mar 19, 2019 at 11:05
    FYI I have already posted this on SO, but not had any replies yet:

    https://serverfault.com/questions/958937/mod-perl-seems-to-be-sharing-modules-across-user-accounts

    I'm hoping people here will be more knowledgeable :) Rather than repeating everything I posted on the other forum, I won't put too much.

    Basically the issue I'm having is that mod_perl is sharing modules from one <VirtualHost> to another (and I don't want this). I was reading that doing something like this should partition it:

    PerlOptions +Clone PerlSwitches -Mlib=/home/steampunkcom/web/uk.steampunkjunkies.com/lib


    ...but while mod_perl works, it still shares the modules :/ Am I missing something?

    Thanks

    Andy
Linux::usermod - user name ... already exists
1 direct reply — Read more / Contribute
by mldvx4
on Mar 19, 2019 at 03:06

    The following changes the account name just fine except that it also produces an error "user name ... already exists ... at line ...". Just to be clear, the old name changes to the new name in /etc/passwd when the script is run, but it produces an error while doing so. What is the correct way to use Linux::usermod so that no error is produce when changing the account name?

    sub rename_account { my ($name,$nname) = (@_); my $account = Linux::usermod->new($name); $account->set('name'=>$nname); }

    Perhaps it is a misunderstanding in how to use Linux::usermod. Looking at the manual page, that looks like the correct usage. So perhaps I have read and interpreted it incorrectly.

    Edit: The problem seems related to /etc/shadow which seems like it is not updated. So if one changes back and forth between the same two names, the first change goes through but leaves the old name in /etc/shadow so the second attempt fails because the old name is still their. I think...

Trouble opening telnet session
3 direct replies — Read more / Contribute
by BrianDStark
on Mar 18, 2019 at 22:01
    I have a script that I run on a Windows 10 Box that opens a telnet session in a CentOS 7 Linux box. I am trying to run the script connecting to Linux Fedora 27 (running on same Windows box), I verified that telnet works using the Windows Telnet Client and logging into Fedora, but when I run my script it times out trying to open a telnet session. Is there a known issue with the Fedora Kernel 4.20 Telnet server? The identical script runs fine when connecting to CentOS 3.10. Hoping its something simple like the login prompt is different. Brian
Calling a C++ subroutine from Perl
5 direct replies — Read more / Contribute
by ibm1620
on Mar 18, 2019 at 20:36
    Dear Monks,

    Our company obtained a C++ source code license for Metaphone3 and I want to test it using Perl as a driver. I'm wondering what my options are. In the past, when I needed to call a complex C subroutine from Perl, I've written a standalone C++ program that accepted command-line parameters, called the subroutine, and wrote the output to STDOUT, and then "called" it using qx//. That's certainly still a possibility, but on the offchance that the forking and IPC would pose a performance hit (I'm going to test 100M's of strings), I wondered if XS was a practical possibility.

    I have never written an XS module, and it looks daunting, but I thought there might be some XS stub code out there that I could easily tweak so that a Perl program could call one subroutine, passing in one string and getting two strings back; end of story.

    Any thoughts on the subject? Other approaches?

    (Metaphone3 is also available in Perl, but unfortunately that's a separate license.)

New Perl Poetry
obfupoetry II
No replies — Read more | Post response
by Discipulus
on Mar 21, 2019 at 04:46
    In occasion of first day of Spring* i give you the second issue of perl obfupoetry:

    How:to:forget:when:I:was:young: join ' had ', (glob'spr{a,e,i,o,u}ng')[2,4]; Now:I:get:in:the:middle:of: join ' and ', (glob'Y{i,a}n{,g}')[0,-1]; and:I:remember:it:was:a:bang: seek DATA,0,0;while(<DATA>){last if/DATA/; chomp&&print/:/?(join$",split':',$_):eval,$/} __DATA__

    L*

    (*)please do not reply with positivist comments about the exact first day of Spring: seasons always start in day 21: the notion is in my mind since..

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
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 exploiting the Monastery: (5)
As of 2019-03-25 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (115 votes). Check out past polls.

    Notices?