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

The Monastery Gates

by gods
on Mar 23, 1999 at 10:47 UTC ( #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
eval system timeout child process
2 direct replies — Read more / Contribute
by dasibre
on Sep 29, 2016 at 15:22

    New to perl trying to fix zombie process bug.I have a script that makes a timed system call in an eval block.

    my $timeout = timed_system_call("/subprocess_a.rb", 3); if ($timeout == 0) { print "success\n"; } else { print "timed out\n"; } sub timed_system_call { my $command = shift; my $timeout = shift; # seconds my $alarm = 0; eval { local $SIG{ALRM} = sub {$alarm = 1; kill -15, $child_pid;}; alarm $timeout; system($command); alarm 0; }; die $command . " failed with " . $@ if $@ && $alarm != 1; #if alar +m is not 1, then something else caused exit e.g(ctrl-c) alarm 0; return $alarm; }
    I need to, ensure the system() call subprocess is killed after timeout, without killing the parent process. On timeout the program should continue to the else block.
Loop through array or filehandle
3 direct replies — Read more / Contribute
by markdibley
on Sep 29, 2016 at 09:57
    Hello

    I have a subroutine that loops through some data in a file until it has found the correct outcome and then stops. I simply pass a filehandle reference to the subroutine to do it.

    However, I now have a new source of data that gives it to me in a smaller string. To apply the some routine I just need to split the data by \n and could pass the array reference to the subroutine.

    But is there a way to write a subroutine that takes either a filehandle ref or array ref to loop through?

    I am looking for a way that does not involve reading the whole file into a string (although I admit this may because I am old and started programming computers that had never heard of GB memory).

    Is it possible?
MCE -- how to know which function to use
1 direct reply — Read more / Contribute
by 1nickt
on Sep 28, 2016 at 17:57

    Hello all,

    I've read through the docs and made some experiments with basic usage of MCE, but I'm not sure if I'm barking up the wrong tree.

    I'm unclear on:

    • When to use mce_loop() vs. mce_map() vs. MCE::Shared
    • How to know how many workers to set as max

    I have an arrayref of hashrefs, and am outputting an arrayref of hashrefs. Processing each hashref is quite slow: takes about 0.1s. There are 7,500 hashes in the arrayref; that could grow to some tens of thousands.

    The code is running on an Ubuntu AWS instance whose lscpu outputs:

    Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 2 Core(s) per socket: 1 Socket(s): 1

    The MCE manager is splitting the array into 25 chunks using 'auto'.

    I am seeing almost no difference in time taken to execute using MCE versus a sequential foreach loop, in fact the sequential loop appears faster, which I would not have expected.

    Although the CPU usage looks quite different:

    Benchmark: timing 5 iterations of MCE loop , MCE map , Se +quential loop... MCE loop : 83 wallclock secs ( 3.28 usr 0.19 sys + 30.94 cusr 2 +4.89 csys = 59.30 CPU) @ 0.08/s (n=5) MCE map : 75 wallclock secs ( 4.48 usr 0.28 sys + 41.29 cusr 3 +7.93 csys = 83.98 CPU) @ 0.06/s (n=5) Sequential loop: 76 wallclock secs (37.79 usr + 28.91 sys = 66.70 CPU) + @ 0.07/s (n=5)

    Am I missing something obvious? Or non-obvious? Doing something wrong? Can anyone shed any light please?


    The way forward always starts with a minimal test.
Parsing NE Command Line Arguments
2 direct replies — Read more / Contribute
by Scully4Ever
on Sep 28, 2016 at 10:45

    I wrote a script that uses Getopt::Long to get the command line arguments. Is there a way to allow the user to use != in addition to "= so the user could for example request data not equal to year 2016 or data equal to year 2016?

One-liner's quoting and subroutine
3 direct replies — Read more / Contribute
by reisinge
on Sep 27, 2016 at 01:42

    Dear Monks :-), I have two questions related to the following one-liner:

    find /opt/splunk/syslog/ -iname "*log*" -type f -mtime +30 | perl -wne + 'BEGIN { $sum = 0 }; chomp; $sum += (stat)[7]; END { print "$sum\n" +}'

    1) How should I quote it (escape the single quotes) when I want to execute it on a remote machine via SSH? Ex. ssh root@HOST 'ONE-LINER'

    2) Is there is a (smart) way to incorporate the following subroutine into the one-liner?

    sub scaleIt { my $size_in_bytes = shift; return unless defined $size_in_bytes; my ( $size, $n ) = ( $size_in_bytes, 0 ); ++$n and $size /= 1024 until $size < 1024; return sprintf "%.0f%s", $size, (qw[ B KB MB GB TB ])[$n]; }

    You can talk and talk and have great ideas, but if you don't have a way of sharing those ideas then you are just saying it in you own bubble. -- Tom Limoncelli
Putting an SQLite DB file into memory
3 direct replies — Read more / Contribute
by stevieb
on Sep 25, 2016 at 18:56

    Perlmonks is usually slow on weekends, so I thought I'd fire off another question.

    I have an SQLite database that will be used to store data every 3-10 seconds. This will be happening on a Raspberry Pi, so the 'disk' is an SD card. What I'm hoping to do is load that file into memory, and use it there, then create an event to write it to disk every X minutes, making it permanent (if data is somehow lost in the meantime, it isn't critical).

    Everything writing to the DB will be in Perl, and all processes will be within a single process umbrella.

    Is this a Linux question, or can this somehow be done (copy the db file to memory) within Perl, at the onset of my application load? If so, can I please get some recommendations on a practical way of doing this, or perhaps pointers to modules that may already do this?

shared array while use ForkManager
1 direct reply — Read more / Contribute
by mlin
on Sep 25, 2016 at 05:21
    Hi all,

    I am writing a script using fork to speed up my process of N files. It works well now. However, I need output information pushed in an array in my serial process, which will be used later. Will there be some conflict while I use fork? It looks like this:
    foreach my $i (@group) { $pm->start and next; foreach my $term (@$i) { warn "No such file. - $term\n" and next if ! -e $term; ... $IsGood = ...; our @quality; push @quality $IsGood; } $pm->finish; } $pm->wait_all_children;
calculate length of day as function of space at onset of fall
3 direct replies — Read more / Contribute
by Datz_cozee75
on Sep 24, 2016 at 03:20

    Hello Monks,

    When did fall begin? Is there only one answer to that question? I'm honestly not sure. Let me describe the scene in terms of the dynamics of gilligan's island, because if we can't generalize to the arbitrary location in the pacific, then this isn't much of a script. 'MaryAnn' and I found ourselves on an island with a west wind coming in that made it hard to stand. It was 9/11, and our phones were gone. The Gilligan in me savors being lost, but we were up against the elements, and knowing west was important. In such conditions, we hunkered down, stayed close, estimated waves, and watched events along the ecliptic.

    Several days after the ordeal, we're looking at the sunset, talking about the onset of fall, and this time with perl at hand...didn't want to donate my computer to the Columbia. I looked at the sky maps and realized that fall occurs exactly when the sun sets in the west. Maryann's question was "how long was the day today?" I'd like to be informed by my compiler. Output precedes code.

    C:\Users\Fred\Desktop>perl fall1.pl cos tau = - tan phi * tan delta onset of fall ==> delta equal zero delta is 0 phi is latitude: 45 in portland (close enough) phi is 0.785398163397448 rhs is 0 tau is 1.5707963267949 degrees is 90
    C:\Users\Fred\Desktop>type fall1.pl #!/usr/bin/perl -w use strict; use 5.010; use Math::Trig; use Math::Trig ':pi'; say "cos tau = - tan phi * tan delta"; say "onset of fall ==> delta equal zero"; my $delta = tan( 0); say "delta is $delta"; say "phi is latitude: 45 in portland (close enough)"; my $phi = deg2rad(45); say "phi is $phi"; my $rhs = - tan ($phi) * tan ($delta); say "rhs is $rhs"; my $tau = acos($rhs); say "tau is $tau"; my $degrees = rad2deg($tau); say "degrees is $degrees";

    I'm given to believe that the output in degrees is a measurement of time. I've read two differing treatments on how it is *exactly* so, both of which differ, and are wrong. The better one gets to 1.3%. That's not good enough for an oblate spheroid, unless I can't come up with better. What's more, the entire scenario makes me think of re-creating giant wheels, and history is replete with able people who have been making this a thing for much longer than I have.

    My question is: when did fall hit you?

    Счастливая осень!

GUI Design/Organization - Recommended Practices?
2 direct replies — Read more / Contribute
by atcroft
on Sep 23, 2016 at 15:26

    Are there currently any recommended common or "best" practices when creating a GUI for a script? (In terms of organization, etc.?)

Are these Perl Textbooks Good?
7 direct replies — Read more / Contribute
by Svetlana
on Sep 23, 2016 at 14:44
    Hello

    New to the forum. Have a simple question. I going to make an effort and learn Perl. I tend toward using text books rather than the internet. Are these books any good? I'm aware you have to be careful as some books/websites teach bad practices.

    It's a series teaching Perl. Perl is now at 5.24, but the book covers 5.14. Is there that much of a difference? Would purchasing the book be a bad choice?
How to disable:Odd number of elements in hash assignment warning?
2 direct replies — Read more / Contribute
by BrowserUk
on Sep 23, 2016 at 12:04

    I'm splitting some data into a hash, and occasionally, the last value may be missing.

    This is known and unimportant, so I'd like to locally disable that warning; but what category to give to no warnings ...?

    (And for bonus points, how to find it?)


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Problem using POE::Wheel::ReadWrite with pipes.
3 direct replies — Read more / Contribute
by ombibulous
on Sep 23, 2016 at 10:20

    Hello,

    I'm playing with a toy POE program that implements two sessions connected by a pipe that should simply cat an inupt file. Something like this:

    +- read side of pipe / ---------- ---------- STDIN --> | reader | -pipe-> | writer | --> STDOUT ---------- ---------- \ +- write side of pipe

    I started by using rcaputo's example that he posted here at perlmonks.org/?node_id=949005 and came up with this non-working code:

    use strict; use warnings; use IO::Pipely qw(pipely); use POE qw(Wheel::ReadWrite); my ($read_pipe, $write_pipe) = pipely(); # reader sends output to the write pipe my $reader = POE::Session->create( args => [ $write_pipe ], inline_states => { _start => sub { $_[HEAP]->{wheel} = POE::Wheel::ReadWrite->new( InputHandle => \*STDIN, OutputHandle => $_[ARG0], InputEvent => 'got_input', ErrorEvent => 'got_input_error', ); }, got_input => \&handle_input, got_input_error => \&handle_input_error, process_next_input => \&process_next_input, } ); # writer reads input from the read pipe my $writer = POE::Session->create( args => [$read_pipe], inline_states => { _start => sub { $_[HEAP]->{wheel} = POE::Wheel::ReadWrite->new( InputHandle => $_[ARG0], OutputHandle => \*STDOUT, InputEvent => 'got_input', ErrorEvent => 'got_input_error', ); }, got_input => \&handle_input, got_input_error => \&handle_input_error, process_next_input => \&process_next_input, } ); POE::Kernel->run(); exit; sub handle_input { my ( $kernel, $heap, $input ) = @_[KERNEL, HEAP, ARG0]; print $heap->{wheel}->put( $input ); $heap->{wheel}->flush(); $heap->{wheel}->pause_input(); $kernel->yield( 'process_next_input' ); } sub handle_input_error { my ($kernel, $heap) = @_[KERNEL, HEAP]; $kernel->yield( 'process_next_input' ); delete $heap->{wheel}; } sub process_next_input { my ($kernel, $heap) = @_[KERNEL, HEAP]; $heap->{wheel}->resume_input() if $heap->{wheel}; }

    On my Mac using Perl 5.20 I get this as the output when I feed the program a three line CSV file:

    ombibulous> perl cat.pl < test.csv 00001,2,3,4 0a,b,c,d 0foo,bar,baz ^C ombibulous>

    The ^C is me yanking the rope on my hung program. The lines should not begin with '0's, I don't know how they are creeping in.

    I thought (hoped) that after the $reader's handle_input() subroutine does a put(), pauses_input(), and yield(), then POE would see that there is an input for the $writer session and call its input handler. WRONG! I'm not getting how one session cedes processing to another session. I've seen the examples using posts() but I'm trying pipes instead. Can anyone tell me what I'm doing wrong?

    Eventually I'd like to build up a chain of sessions, connected by pipes, where each session does a particular transformation on input data; e.g., reader -> parser -> writer. I've already done this with a single process, parsing and writing within a read loop, and subprocesses, forked process for each step. I'd like to see how it works using POE. Is POE::Wheel::ReadWrite the best package to use if I'm going to use a single process?

    Thanks for any help, insights, and/or solutions.

New Meditations
Don't post bad code!
5 direct replies — Read more / Contribute
by afoken
on Sep 26, 2016 at 16:46

    The thread Declaring and checking content of variables with consecutive names and especially the answer Re: Declaring and checking content of variables with consecutive names triggered this meditation.

    There is nothing special about this thread or this answer, it's just one of those FAQs (in this case "How can I get variable variable names"). People explain how to do the job properly, and some other people can't resist showing that there is a way how to force perl into using the stupid variant. I vagely remember threads where people tried to find even more stupid ways to "solve" a cleanly solveable problem.

    Yes, we can force perl into doing the most stupid things. Yes, it's cool to know how to mess with the inner workings of perl. But no, we should not show beginners the most dirty ways first. Not even with warnings not to use the dirty ways in production code. "Just do as I say, don't do as I do" is no good motto, not for teaching beginners.

    Why? Because there are lots of beginners out there, who either don't have time to or are unwilling to learn how to use perl. They just want fast results, they don't care about maintainability or improving their skills. Imagine what happens when they get ten answers linking to FAQs, HOWTOs, documentation, or showing examples of the right way, but require a little bit of thinking; and one or two answers showing how to abuse perl into the way they are currently thinking.

    "There is nothing humans would not do to avoid thinking."
    -- Found on a pinboard in a computer laboratory in my univerity

    This way, bad practices propagate, resulting in crappy perl scripts.

    Let's make it hard for the unwilling and the people in a hurry to find bad code, bad examples. Posting sane examples is good, improving a code example posted is even better.


    But what about golf?

    Perl golf is fun, true. Replacing pages and pages of code with 20 characters of "line noise" with the same result is deeply impressive. But do we have to show our golf skills in beginner threads? I don't think so. Create a new thread, link to the beginner's thread, and name it "Golf Challenge". Or at least start a golf posting by explaining that this is not a real answer, but a golf challenge for the experts.


    Are one-liners bad?

    (A meditation in a meditation)

    It depends. Short one-liners for one-time use are ok. But for anything more complex than one or two, maybe three explicit instructions (not counting the implicit loops in perl -p and perl -n) should be in a script. And if the one-liner is to be reused, it should instead be a script, too.

    Why?

    • One-liners require that you remember quite complex "line noise". So you very likely store them in a file. What's the difference to using a real script? It's just harder to use, as you need to copy-and-paste.
    • Quoting rules differ with different shells (and different operating systems), so you need to adapt the one-liners, especially the arguments to perl -e and perl -E, to the shell currently in use. Scripts don't have this problem.
    • Scripts can have nice names. cleanup-foo is easier to remember, easier to type, and shows the intention more clearly than perl + 10 perl command line options + -e plus 80 characters of "line noise" in quotes differing from shell to shell.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
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: (7)
As of 2016-09-29 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (562 votes). Check out past polls.