Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
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
CPAN PAUSE Accounts for Organisations
1 direct reply — Read more / Contribute
by kcott
on Jun 17, 2018 at 19:44

    The company I work for is considering putting some of its non-commercially-sensitive Perl modules on CPAN for general use. I have been asked to investigate this.

    I already know about PAUSE and how to "Request PAUSE account" (I have such an account myself). The wisdom I'm seeking in this instance regards accounts for (commercial) organisations and whether there are any special considerations.

    It looks like the process should be straightforward; but, of course, looks can be deceptive. If anyone has done this previously and encountered any stumbling blocks, gotchas, or such-like, a description of your experiences and how they were overcome (or, indeed, how to avoid them in the first place) would be much appreciated.

    Thanks in advance.

    — Ken

How should I manage CPAN when using two versions of Perl in my computer?
4 direct replies — Read more / Contribute
by hda
on Jun 17, 2018 at 13:16
    Dear monks!

    I normally use two different versions of Perl on my computer: one that comes with the Linux distro (openSUSE) I use and another one that I use to write scripts and mess with, and that I normally compile myself from source. The reason I do this is to not introduce changes in the system's installation and potentially brake things beyond my knowledge on how to repair.

    Until now I have done the quick & dirty procedure of just relying on my (usually more up to date) installation and neglect the system's Perl. However I realize that this is possibly not the soundest strategy. My question then is how should I best manage module installation and update using CPAN in these different installations?

    The CPAN executables live in different folders and (I believe) are compiled to point to different sets of libraries. These executables can be called separately (/usr/bin/cpan; or /usr/local/bin/cpan). Now, CPAN requires a sort of environment (i.e. /root/.cpan/...). Both executables seem to use this same environment, but apparently these two CPAN realize the different upgrading needs (for example when calling "r").

    How does this impact in the update of modules? Should I aim to create different CPAN work directories? Are there any recommendations out there on how to proceed with this?

    Any hint will be appreciated.

    Thanks in advance.

Hobo with a bit of recursion
1 direct reply — Read more / Contribute
by Veltro
on Jun 16, 2018 at 14:31

    Hello everyone,

    I have constructed this piece of code to illustrate three problems and I would like to know: How would you write this code so that it works?

    Problem 1: The join is located at a problematic position due to the nature of how hashes work. Sometimes the code takes 6 seconds to execute and sometimes 2 + 6 seconds.

    Problem 2: L2_counter1 => 3 is not incremented

    Problem 3: (Is actually a question) Why do I need to use a share? The data is incremented independently, so I would think there should be no problems regards synchronicity. However if I don't use the share, nothing gets incremented at all...

    Here is the code:

    use strict ; use warnings ; use MCE::Hobo ; use MCE::Shared ; use Data::Dumper ; sub task1 { print "Starting task 1 for $_[0]\n" ; sleep(2) ; print "Finished task 1 for $_[0]\n" ; } sub task2 { print "Starting task 2 for $_[0]\n" ; sleep(4) ; print "Finished task 2 for $_[0]\n" ; } sub task3 { print "Starting task 3 for $_[0]\n" ; sleep(6) ; print "Finished task 3 for $_[0]\n" ; } MCE::Hobo->init( max_workers => 2, # hobo_timeout => 10, # posix_exit => 1, ) ; my $mutex = MCE::Mutex->new; my $_test = { L1_counter1 => 1, # L1_counter2 => 2, # L1_counter3 => 3, nested1 => { L2_counter1 => 3, # L2_counter2 => 2, # L2_counter3 => 1, }, } ; my $test ; tie %{$test}, 'MCE::Shared', { module => 'MCE::Shared::Hash' }, %{$_te +st} ; print Dumper( $test ) ; sub executeTasks { my $in = $_[0] ; my $hobo ; foreach( keys %{$in} ) { if ( ref $in->{ $_ } eq 'HASH' ) { executeTasks( $in->{ $_ } ) ; } else { if ( $in->{ $_ } == 1 ) { $hobo = mce_async { task1( $_ ) ; ++$in->{ $_ } ; } ; } elsif ( $in->{ $_ } == 2 ) { $hobo = mce_async { task2( $_ ) ; ++$in->{ $_ } ; } ; } elsif ( $in->{ $_ } == 3 ) { $hobo = mce_async { task3( $_ ) ; ++$in->{ $_ } ; } ; } ; } ; } ; $hobo->join() ; } ; executeTasks( $test ) ; print "\n" ; print Dumper( $test ) ;
dbi: moving big data among databases (prepared statements)
4 direct replies — Read more / Contribute
by leostereo
on Jun 16, 2018 at 13:43

    Hi guys , I need to move big databa among databases. I have been reading about improving performance with mysql prepared statements. So far I dont understand why it is better neither how to use it.
    Lets suppose I have the following "bring and insert data" procedure:

    #!/usr/bin/perl use strict; use warnings; require DBI; ### #variables here my @big_data_array; ### foreach $id (@big_data_array){ $query1 = "SELECT * from database1.table WHERE id = '$ +id' my $sth1 = $dbh1->prepare($query1); $sth2->execute(); while (my @row = $sth1->fetchrow_array ) { $val1 = $row[0]; $val2=$row[1]; } $query2 = "insert into database2.table ('value1','valu +e2')". " VALUES('$val1','$val2')"; my $sth2 = $dbh2->prepare($query2); $sth2->execute(); }

    Question is how to achieve same result using prepared statements ?
    And why is it better ?
    I will try to find more info about it. Regards, Leo.

Possible scoping issue with Log::Log4perl logger
3 direct replies — Read more / Contribute
by talexb
on Jun 15, 2018 at 17:52

    I hope this isn't a stupid question, but at least I've discovered the solution to my problem, and I'm happy to share this story in the event that it helps someone else.

    I've developed a pile of scripts that use Log::Log4perl, a module that I love to bits. Wanting to implement a system that would E-Mail me when there was a problem, I decided to add an E-Mail appender to my configuration. It worked fine in my test scripts, but didn't work in my work scripts, and I couldn't understand why.

    I finally figured it out this afternoon (which will explain why I'm pausing to sip from my tall can of Waterloo Amber "Ontario's First Craft Brewery"). My test script looks like this:

    { Log::Log4perl->init ( $log4perl_config ); my $log = Log::Log4perl->get_logger(); .. $log->info('Some stuff'); }
    This works fine. My work scripts are bigger and have subroutines in which I want to do logging, so they look like this:
    my $log; { Log::Log4perl->init ( $log4perl_config ); $log = Log::Log4perl->get_logger(); .. $log->info('Some stuff'); do_something(); } sub do_something { $log->info('Some other stuff'); .. }

    See the difference? I'm using what I call a file scoped variable in the work script so that I don't have to pass the logger into the subroutine (something MSCHILLI goes 'Brrrr!' to here). I believe Log::Log4perl collects log entries to go out by E-Mail until the logger goes out of scope.

    So the test script works fine. The work script's logger (I thought) would also go out of scope and send the E-Mail when the script finishes -- but no E-Mail is sent. Why not?

    Anyway, my workaround is going to be to call get_logger in whichever scope I need to do logging. This works fine, but I'm left wondering why doesn't Log::Log4perl see that the logger has gone out of scope in the work script and send the E-Mail? Is there a 'finalize' or 'terminate' method I could have called to trigger this? Is this an error in the behaviour? (I find that hard to believe, for a module that's been around since 2002).

    Alex / talexb / Toronto

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

[OT] Python to Perl.
3 direct replies — Read more / Contribute
by BrowserUk
on Jun 15, 2018 at 10:31

    Does the Python syntax a = [0] * n; mean the same as Perl: @a = (0) x $n;?


    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". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Inline::C on Windows: how to improve performance of compiled code?
5 direct replies — Read more / Contribute
by vr
on Jun 14, 2018 at 19:22

    Dual boot (i.e. same hardware), Ubuntu 17.10, Win10, threaded Perl 5.26 both. However, it seems some (default) flags (compiler options?) are set wrong for Inline::C on Windows. Here's output of no-op example for both systems:

    $ perl -MTime::HiRes=time -MInline=C,'void foo(){}' -wE'$t=time;foo()f +or 1..1e8;say time-$t' 5.57836294174194 $ perl -MTime::HiRes=time -wE'$t=time;sub foo(){}foo()for 1..1e8;say t +ime-$t' 5.003093957901 >perl -MTime::HiRes=time -MInline=C,"void foo(){}" -wE"$t=time;foo()fo +r 1..1e8;say time-$t" 11.325471162796 >perl -MTime::HiRes=time -wE"$t=time;sub foo(){}foo()for 1..1e8;say ti +me-$t" 5.04568600654602

    The use of Inline::C for particular case, where I noticed that, is exactly for huge number of calls of very simple sub (but not no-op, of course). I understand sub calls in Perl are expensive, I'll live with that, but I don't understand the result above.

    To counter "it's because Linux is better", the script was moved from very old 32-bit Windows machine, and I noticed (though can't provide any solid numbers right now), that benchmarks for pure-Perl parts showed perhaps 6-fold speed improvement, while the main part, in C, no more than 2-fold. Therefore I have a suspicion about sub-optimal Inline::C defaults there, and i hope for hints of what could be changed.

Why do I get random numbers?
5 direct replies — Read more / Contribute
by Cleggy
on Jun 14, 2018 at 18:16

    So, I wanted to understand the printf command and how to manipulate the formatting of a number (yes I am that new to this), namely how to insert a variable into %.f it would seem %.{$var}f does the trick.

    And In this small script I thought I had it worked out

    If I enter 1.111111111111111111111111111111 (1 with 30 1s after the decimal point) as an experimental value, when asked 'How many digits would you like after the decimal point ? ' if I answer 2 I get 1.11 as expected.

    If I answer 10 I get 1.1111111111 as I would expect.

    However if I answer 20 I get 1.11111111111111116045 (?)

    And if I answer 30 I get 1.111111111111111160454356650007 (?)

    Can someone tell me why please.

    #! /usr/bin/perl -w # experiment with printf use strict; my $num = 0; my $dig = 0; print 'Input a number :'; chomp( $num = <STDIN> ); print 'How many digits would you like after the decimal point ? '; chomp( $dig = <STDIN> ); print "\n"; print 'Original number was '; print "$num \n"; printf("And to $dig, points is "); printf( "%.${dig}f", $num ); print "\n\n";
lying about program name
5 direct replies — Read more / Contribute
by japl
on Jun 13, 2018 at 22:35
    perldoc on exec/system states that one can lie about name of the program being executed via indirect object syntax; but it doesn't appear so:

    a simple test,
    $ cat foo #!/usr/bin/perl -w use strict; print "I am $0 (@{[join ' ', @ARGV]})\n"; $ cat bar #!/usr/bin/perl -w use strict system { './foo' } 'bar', @ARGV; $ ./bar 1 2 3 I am ./foo (1 2 3) $
    shouldn't it be "I am bar (1 2 3"?
how to resolve IP's in an HTTPd that doesn't resolve them?
3 direct replies — Read more / Contribute
by taint
on Jun 13, 2018 at 15:09
    Greetings,

    I'm experimenting with a couple of HTTPd's (names removed to protect the guilty innocent) that are fairly light, but don't resolve the connecting IP addresses (provide the HOST name) in the logs. I've contacted the devs of both of them asking why not, or if when. But the answers were basically, too heavy && too slow. Which, to me, seemed more an excuse. As the facility could have simply been implemented as an option. But they're not my HTTPd's.

    So, I did some experimentation, and was able to get my web pages to return the HOST names of the connecting IP's in the following manner (all my pages are CGI/Perl):

    ... use strict; use feature qw(say); use Socket; ... my $remote_ip = $ENV{REMOTE_ADDR}; # note: don't try this at home kids, unless you have a fast local DNS +(server) my $remote_host = gethostbyaddr(inet_aton($remote_ip), AF_INET);
    Which, as you might imagine; allows me to return the HOST name contained inside $remote_host.

    So, Perl will give me what the HTTPD won't -- I love Perl! So how might I best achieve the same simultaneously within my HTTPd logs? I'm guessing pipe is probably involved here. But I'd really rather run this whole thing past the many brilliant minds, and monks here. In hopes of getting the best solution. :-)

    Thanks!

    Evil is good, for without it, Good would have no value
    λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Print n lines after a matched line
7 direct replies — Read more / Contribute
by sreek3502
on Jun 13, 2018 at 14:14

    I have an input file having following contents

    SCHEDULE "TEST"

    DESCRIPTION "Do Some stuff"

    MINUTE "53"

    HOUR "21"

    SCHEDULE "DUMMY CHECK"

    DESCRIPTION "Do some stuff"

    Check something

    INTERVAL "10m"

    MINUTE "50"

    HOUR "21"

    I need to match the 3rd line after the matched line SCHEDULE "DUMMY CHECK" which is INTERVAL "10m". I have written the below code for that purpose, however i'm not sure if this is the exact way of doing it, or do we have any simple other logics.

    use strict; use warnings; my $file = "input.txt"; my @data; open (IN,"<","$file"); my $count = 0; while (<IN>) { $count = 1 if /SCHEDULE\s"(DUMMY\sCHECK)".*/; if ($count >= 1 and $count <= 6) { @data = $_; print @data; $count++; } }
Guidance on choosing suitable license
4 direct replies — Read more / Contribute
by hda
on Jun 13, 2018 at 08:21
    Dear monks,

    During many years I have written simple (crude?) code for use within the limited realms of research projects. Those code snippets rarely left the relatively secluded circles for which they were developed.

    There is now some interest in a couple of scripts that I have written. Those are nothing advanced nor extraordinary, but there might be some value in them. I would gladly like to share them upfront, but I want to limit the capabilities of the parties that get the code to make them proprietary or closed: I want my humble code to remain open and free, while not harming anyone's ability to modify or build upon them to make better code that may or may not be sold or be used commercially.

    I am not initiated into licenses for free and open source software and would need some pointers and guidance. I have seen a few sites (like this one: https://choosealicense.com/) but I am a bit stunned by the variety and complexity of the issue. I am leaning towards a LGPL license, but fear that there may be something better. I would appreciate your guidance in this respect.

    Thanks in advance

New Cool Uses for Perl
Why is it so easy to make Perl apps for Apple Mac?
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jun 17, 2018 at 13:40
    Compile your Perl to a portable binary application for Apple Mac (99k):
    echo 'display alert (do shell script "perl -v")' | osacompile -o perl. +app
    Write apps for Apple Mac in Perl: Pt.1, Pt.2, Pt.3
How to write apps for macOS/OSX in Perl! Part 2
No replies — Read more | Post response
by Anonymous Monk
on Jun 14, 2018 at 17:00
    Welcome to Part 2 of How to write apps for macOS/OSX in Perl! See Part 1 to get started with the built-in macOS devtool Automator. This edition demonstrates how to:

    • Process choices with Perl from an Applescript dialog to:
      1. Display output from Perl to an Applescript dialog.
      2. Execute Perl in Terminal to display its output.
      3. Send output from Perl to an application (TextExit).
    Applescript is to the operating system what Javascript is the the web browser. It can do many things and what it can't do can always be handled by shell commands and especially Perl! When the code below is saved by Automator as something like PerlVersionTool.app you will have a 1.3MB portable binary application! Double click and ENJOY!
    • Other techniques covered here include:
      1. Visiting websites (Perlmonks of course!)
      2. Displaying notifications
      3. Application control
      4. Applescript subroutines
      5. Abusing buttons to widen dialogs
      6. How to rule your world with Perl!
    Source:
    (* Demonstration macOS/OSX app in AppleScript and Perl *) (* Posted to perlmonks.org by Anonymous Monk 6/14/2018 *) (* Node: How to write apps for macOS/OSX in Perl! Part 2 *) set TITLE to "Perl Version Tool" set PROMPT to "Make a selection" set _1 to "Perl version, patchlevel and license" set _2 to "Perl configuration summary" set _3 to "Perl command line help" set _4 to "Visit Perlmonks.org!" repeat set what to choose from list {_1, _2, _3, _4} with title TITLE wit +h prompt PROMPT OK button name {"View"} cancel button name {"Exit"} d +efault items _1 set what to what as string if what is _1 then set CMD to "perl -v" # ONE LINERS OR PROGRAMS OF ANY SIZE! else if what is _2 then set CMD to "perl -V" else if what is _3 then set CMD to "perl -h" else if what is _4 then display notification "Opening The Monastery Gates!" set CMD to "open https://perlmonks.org" else if what is "false" then return # EXIT end if if what is _2 then # SEND PERL CODE TO TERMINAL AND EXECUTE doShell(CMD) else if what is _3 then # CAPTURE PERL STDOUT set CMD to do shell script CMD # SEND PERL STDOUT TO TEXTEDIT textEdit(CMD) else # CAPTURE PERL STDOUT set RES to do shell script CMD # MAKE DIALOG WIDE set SPC to " + " # PRINT PERL STDOUT TO APPLESCRIPT ALERT display alert TITLE message RES buttons {SPC & "Cool" & SPC} d +efault button 1 end if end repeat # APPLESCRIPT SUBS: on doShell(CMD) try tell application "Terminal" activate tell application "System Events" to keystroke "n" using {c +ommand down} end tell tell application "System Events" tell application process "Terminal" set frontmost to true keystroke CMD keystroke return end tell end tell on error oops display alert oops as critical end try end doShell on textEdit(CMD) try tell application "TextEdit" activate tell application "System Events" to keystroke "n" using {c +ommand down} end tell tell application "System Events" tell application process "TextEdit" set frontmost to true keystroke CMD end tell end tell on error oops display alert oops as critical end try end textEdit
New Obfuscated Code
A quick one-liner
No replies — Read more | Post response
by liverpole
on Jun 11, 2018 at 20:19
    Should work on most *nix systems:
    perl -E'say`$^Xdoc -qj`=~m`"(.*)"`'

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-06-18 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (109 votes). Check out past polls.

    Notices?