http://www.perlmonks.org?node_id=479

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

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

libmysql_.dll error
1 direct reply — Read more / Contribute
by zmichael
on Jun 16, 2018 at 12:18
    I am trying to install a forum software called Gossamer Threads on localhost, but I keep recieving this error: https://image.prntscr.com/image/UBMcfzRpRDGwzxhZxnBiBA.png I already have copied that file from strawberry folder to C:/xampp/perl/vendor/lib/auto/DBD/mysql/mysql.dll but it does not work. Could you help me with this?
Tcl::pTk Can't locate object method "findINC" via package "Tk"
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 15, 2018 at 23:32

    I am experimenting with Tcl::pTk (0.92) on a macOS to have access to Tcl/Tk and avoid the old and ugly Perl/Tk. Why on earth does this module complains about a missing Tk object "Can't locate object method "findINC" via package "Tk"? Why should I have Perl/Tk if I want to avoid its use? This is not a secondary problem, as I do not want Tk because it requires my to install X11, which I do not want to have installed. Any idea?

print problem
4 direct replies — Read more / Contribute
by yueli711
on Jun 15, 2018 at 19:01

    Hello, I just want to print a line which contains "Homo sapiens". Thanks in advance!

    open(IN1,"tmp01.txt") || die "Cannot open this file"; @lines1 = <IN1>; open(OUT,">tmp03") || die "Cannot open this file"; for $item1(@lines1){ chomp $item1; @tmp1=split(/\t+/, $item1); if ($tmp1[4] eq "Homo sapiens"){ print OUT @lines1; last } print OUT "\n"; } close(IN1); close(OUT);
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.

error at line 77
2 direct replies — Read more / Contribute
by Wremmurg
on Jun 15, 2018 at 12:13
    #!/usr/local/bin/perl # define the strings used in printing @digitword = ("", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"); @digit10word = ("", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"); @teenword = ("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"); @groupword = ("", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "novillion", "decillion"); # read a line of input and remove all blanks, commas and tabs; # complain about anything else $inputline = <STDIN>; chop ($inputline); $inputline =~ s/[, \t]+//g; if ($inputline =~ /[^\d]/) { die ("Input must be a number.\n"); } # remove leading zeroes $inputline =~ s/^0+//; $inputline =~ s/^$/0/; # put one back if they're all zero # split into digits: $grouping contains the number of groups # of digits, and $oddlot contains the number of digits in the # first group, which may be only 1 or 2 (e.g., the 1 in 1,000) @digits = split(//, $inputline); if (@digits > 36) { die ("Number too large for program to handle.\n"); } $oddlot = @digits % 3; $grouping = (@digits-1) / 3; # this loop iterates once for each grouping $count = 0; while ($grouping >= 0) { if ($oddlot == 2) { $digit1 = 0; $digit2 = $digits[0]; $digit3 = $digits[1]; $count += 2; $digit1 =0; $digit2 = 0; $digits = $digits[0]; $count += 1; } else { # regular group of three digits $digit1 = $digits[$count]; $digit2 = $digits[$count+1]; $digit3 = $digits[$count+2]; $count += 3; } $oddlot = 0; if ($digit1 != 0) { print ("$digitword[$digit1] hundred "); } if (($digit1 != 0 || ($grouping == 0 && $count > 3)) && ($digit2 != 0 || $digit3 != 0)) { print ("and "); } if ($digit2 == 1) { print ("$teenword[$digit3] "); } elsif ($digit2 != 0 && $digit3 != 0) { print ("$digit10word[$digit2]- $digitword[$digit3] "); } elsif ($digit2 != 0 || $digit3 != 0) { print ("$digit10word[$digit2]$digitword[$digit3] "); } if ($digit1 != 0 || $digit2 != 0 || $digit3 != 0) { print ("$groupword[$grouping]\n"); } elsif ($count <= 3 && $grouping == 0) { print ("zero\n"); } $grouping-; }

    I am getting an error message at line 77 near"-;" What am I missing?

    Thank you for any help.

    Wremmurg

[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

Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.