Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

If you're new here please read PerlMonks FAQ
and Create a new user.

Quests
poll ideas quest 2020
Starts at: Jan 01, 2020 at 00:00
Ends at: Dec 31, 2020 at 23:59
Current Status: Active
12 replies by pollsters
    First, read How do I create a Poll?. Then suggest your poll here. Complete ideas are more likely to be used.

    Note that links may be used in choices but not in the title.

Perl News
rt.cpan to close, 01/03/2021
on Dec 05, 2020 at 04:22
1 reply by marto
Perl advent calendar 2020
on Dec 01, 2020 at 03:17
0 replies by marto
Supplications
Convert LST dates to LT dates
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 03, 2020 at 19:13

    Hi,

    I have a list of dates in local standard time that I want to convert to local time.

    I came up with this, but I guess there's some smarter way...

    Thanks



    use strict; use DateTime::Duration; use DateTime; # These are local standard times in Europe/Rome, i.e. always UTC+0100 my @dates = ( [2020,12,2,1,0], [2021,7,2,1,0] ); my $timezone = '+0100'; # This is the duration to be added to go from LST to UTC my $seconds = -($timezone/100) * 3600; my $dur = DateTime::Duration->new( years => 0, months => 0, weeks => 0, days => 0, hours => 0, minutes => 0, seconds => $seconds, nanoseconds => 0 ); for my $date (@dates) { my $dt = DateTime->new( year => => $date->[0], month => $date->[1], day => $date->[2], hour => $date->[3], minute => $date->[4], second => 0, nanosecond => 0, time_zone => 'UTC' ); $dt->add($dur); # This is a DateTime object in LST print "LST: ",join("-",@{$date}),"\n"; print "UTC: ",$dt->set_time_zone("UTC"),"\n"; print "LT: ",$dt->set_time_zone("Europe/Rome"),"\n"; }
How to Split on specific occurrence of a comma
8 direct replies — Read more / Contribute
by dirtdog
on Dec 03, 2020 at 16:13

    Hi Monks, I'm a little stuck and was hoping one of you could help get me out of the mud :-). I had a script that worked until we added over 999 values to it recently. I now have about 3,000 values separated by a comma on 1 line. Unfortunately, I can't change the way the data is provided. Ultimately, I need to get a max of 999 of them into an array or some other structure so I can then perform a query from the db with those values. i.e, select * from table_name where values in ( ). If I can just get chunks of 999 stuffed into an array I could then do a "for loop" until all values have been queried and added as a new worksheet to my excel spreadsheet. It would need to be dynamic too since the values could increase or decrease over time.

    Below is a snippet of the code that used to work (until we broke the 999 threshold that Oracle allows in an "IN" clause by adding over 3k comma separated values in the input record.)

    open INPUT_FILE, @row[$cell_column] or die "Can't open @row[$cell_colu +mn]: $!\n"; while ( $line = <INPUT_FILE> ) { chomp $line; if ($line =~ /^#/ || $line =~ /^\s*$/) { next; } $line =~ s/^[^=]*\=//; @users = split /,/, $line; } close INPUT_FILE; $placeholders = join ( ',', ('?') x @users ); my $SQLLock=qq{ select user,name from teams where user in ( $placeholders ) }; my $sthL = $dbh->prepare($SQLLock); $sthL->execute(@users); while ( my @row = $sthL->fetchrow_array() ) { print ">@row<\n"; for($cell_column=0; $cell_column<=$#row; $cell_column+ ++){ $worksheet->write_string($i, $cell_column, @r +ow[$cell_column],$format1); } $worksheet->set_row($i, undef, undef, 0, 0, 1); $i++; } $sthL->finish;

    Here is my attempt to get the split function broken up by lines of 3. ( For the example I chose a small number because it's more managable. Just need to prove out the logic, but clearly the "limit" clause of the split isn't keeping 3 values per line. This does not work.

    #!/usr/bin/env perl use DBI; use POSIX 'strftime'; use strict; use Data::Dumper; my $line; my @teams; while ( $line = <DATA> ) { chomp $line; if ($line =~ /^#/ || $line =~ /^\s*$/) { next; } $line =~ s/^[^=]*\=//; @teams = map { [ split /,/, $_, 3 ] } $line; } print Dumper \@teams; __DATA__ Teams=PATRIOTS,BILLS,DOLPHINS,JETS,COWBOYS,GIANTS,EAGLES,REDSKINS,BENG +ALS,OILERS,STEELERS,BROWNS,SEAHAWKS,RAMS,49ERS,RAIDERS

    Would greatly appreciate any help or assistance that could be provided. Thanks!

Perlbrew on shared hosting
5 direct replies — Read more / Contribute
by Bod
on Dec 03, 2020 at 14:19

    I am trying to upgrade from Perl v5.16.3 supplied by my shared hosting provider to Perl v5.32.0 by using Perlbrew. Perlbrew has installed fine by following the instruction \curl -L https://install.perlbrew.pl | bash from PuTTY.

    Perl v5.32.0 has downloaded fine but it will not install. I run the command:

    perlbrew install perl-5.32.0
    and get the message
    Installation process failed. To spot any issues, check /home/shoples1/perl5/perlbrew/build.perl-5.32.0.log
    Looking at the logfile everything makes sense. It shows the questions for a guided installation with the answers to the questions already filled in. Then right at the end is this:
    Use which C compiler? [cc] ./trygcc: line 10: /usr/bin/cc: Permission denied Uh-oh, the C compiler 'cc' doesn't seem to be working. ./trygcc: line 25: /usr/bin/gcc: Permission denied ./checkcc: line 10: /usr/bin/cc: Permission denied Uh-oh, the C compiler 'cc' doesn't seem to be working. You need to find a working C compiler. Either (purchase and) install the C compiler supplied by your OS vendo +r, or for a free C compiler try http://gcc.gnu.org/ I cannot continue any further, aborting. ##### Brew Failed #####
    I have previously installed modules from CPAN using PuTTY and SSH thanks to help from the Monastery. I know that I do not have access to install modules if I attempt it through cPanel but it has worked recently from an SSH shell.

    Am I missing something obvious?

    Is it possible that installing , will installing Perlbrew will upset any of the scripts already running on the server using Perl v5.16.3?

Perl syntax checking without `perl -c`
6 direct replies — Read more / Contribute
by kcott
on Dec 02, 2020 at 20:55

    Very brief background: Some idiot dug a hole, cut through a main electrical cable, and killed the power to my $work (and many others in the vicinity). Most of our systems are back up and running but not all of those that I need. To continue working, I've download various files from out git repostory to my home computer. I'm making code changes here but do not have the full environment available to me. Getting that environment is problematic at best, perhaps not possible at all, while "recovery" procedures are in progress.

    After making changes, typing 'perl -c ...' invariably gives me something like: "Can't locate SomeMod.pm in @INC ...".

    I've checked perlrun, under "-c" it has:

    "causes Perl to check the syntax of the program and then exit without executing it. Actually, it will execute any BEGIN, UNITCHECK, or CHECK blocks and any use statements: these are considered as occurring outside the execution of your program. INIT and END blocks, however, will be skipped."

    The use statements will be my biggest problem; although, others may crop up here and there.

    Does anyone know of something simple, like 'perl -c ...', that will just check the syntax without executing any part of the code?

    I'm hoping the system will be fully operational within the next day or two. Anything that involves a substantial amount of work — e.g. setting up and configuring a framework; installing lots of modules; etc. — is probably not an option for me at the present; however, that could still be useful to know and have available for some future, similar situation.

    — Ken

How is Perl for automation?
10 direct replies — Read more / Contribute
by fidodido
on Dec 02, 2020 at 14:05

    Hi

    I'm planning to learn a scripting language for automation. Perl has been a default choice in this scenario, but with other languages coming up, how's Perl doing? I wanna automate some stuff in my current job,start with simple automation, then may be move on to excel reports with graphs (The management here is big time into it. They just like everything "graphisized" :). I'll then use that experience to sort of "break" into automation focused jobs. So how's Perl demand in automation?

    I know this is like walking into a Harley Davidson showroom and asking folks which is a better thing, a cruiser or a sports bike (or vice versa)? But hope you guys would be able to give me some clarity.

    Another reason for asking here, is, there's a lot of articles on the net that seem to indicate that Perl is on it's way out....and then there's an article about Perl 7 (and many more on Perl 6).

    Which brings me to the next question. I'm planning to start with Perl 5, and then may be move to Perl 6. Perl 5 seems to be default installed on the linux servers here, and then there's some Windows friendly version installed on some other servers.

    That's one too many questions, but hope you folks would clear it up for me.

Perl 5.32.0 Too many open files ... Runtime.pm
4 direct replies — Read more / Contribute
by skendric
on Dec 02, 2020 at 08:54

    I tried upgrading from 5.30.0 to 5.32.0 and hit the following with some of my scripts

    Can't locate DateTime/TimeZone/Local/Unix.pm: /opt/local/lib/perl5/site_perl/5.32.0/DateTime/TimeZone/Local/ +Unix.pm: T oo many open files at /opt/local/lib/perl5/site_perl/5.32.0/Module/Run +time.pm li ne 314 (#1) (F) You said to do (or require, or use) a file that couldn't be fo +und. Perl looks for the file in all the locations mentioned in @INC, un +less the file name included the full path to the file. Perhaps you nee +d to set the PERL5LIB or PERL5OPT environment variable to say where +the extra library is, or maybe the script needs to add the library nam +e to @INC. Or maybe you just misspelled the name of the file. See "require" in perlfunc and lib.

    The file '/opt/local/lib/perl5/site_perl/5.32.0/DateTime/TimeZone/Local/Unix.pm' exists ... so I suppose that Perl isn't opening it because it has run out of ... available file handles? What is the name of the resource which has been exhausted and whose exhaustion leads to the 'too many open files' message?

    Looking at Runtime.pm .... Line #314 is the 'return scalar ...' line

    sub require_module($) { # Localise %^H to work around [perl #68590], where the bug exi +sts # and this is a satisfactory workaround. The bug consists of # %^H state leaking into each required module, polluting the # module's lexical state. local %^H if _WORK_AROUND_HINT_LEAKAGE; if(_WORK_AROUND_BROKEN_MODULE_STATE) { my $notional_filename = &module_notional_filename; my $guard = bless([ $notional_filename ], "Module::Runtime::__GUARD__"); my $result = CORE::require($notional_filename); pop @$guard; return $result; } else { return scalar(CORE::require(&module_notional_filename) +); } }

    What is 'sub module_notional_filename'? Well, Runtime.pm contains the following:

    [...] The notional filename for the named module is generated and returned. This filename is always in Unix style, with C</> directory separators and a C<.pm> suffix. This kind of filename can be used as an argument + to C<require>, and is the key that appears in C<%INC> to identify a modul +e, regardless of actual local filename syntax. =cut sub module_notional_filename($) { &check_module_name; my($name) = @_; $name =~ s!::!/!g; return $name.".pm"; } [...]

    Hmm, I'm running into my own lack of expertise here ... I don't see how 'sub require_module' and 'sub module_notional_filename' relate to counting how many open files I have

    Anyone have pointers on what I am seeing?

    I rolled back to perl 5.30.0, which restores the original behavior (i.e. none of these 'Too many open files' messages

    I am running on Ubuntu, 5.4.0-54-generic

    --sk

Concatenation with empty string -- good enough to force physical copy?
3 direct replies — Read more / Contribute
by vr
on Dec 01, 2020 at 16:32

    Is such side-effect a known, yet more obscure idiom among Perl's many, is it "exploited" by any CPAN distribution, so there's hope of guarantee it won't be optimized away in the future? Quick googling reveals it did happen to another language (SO link) and, apparently, was praised as a good thing.

    With current Perl, it performs as expected: no-op, with side-effect I'm after, and I'd be content to leave it in my code, with proper comment, of course. Alternatives that I checked seem to endure "sub call is expensive in Perl" penalty, for small and medium size buffers, or introduce either dependency (Storable), or even further penalty (PDL), when memory for shorter types seems to be allocated as for larger types and then truncated. (Replace "byte" with "double" in code, to see PDL is not intrinsically slow. This bug is only tangentially related, just to show that simple and built-in tools are to be preferred, I think. Unless they are in danger to become extinct.)

    Background: function (1) in a 3d party DLL accepts pointers to several buffers (image channels), my Inline::C glue (2) extracts pointers from references and calls (1), my Perl code (3) does PDL arithmetics and then calls (2). For particular input, shortcuts are possible which greatly optimize (3), but then Perl references happen to point to the same data, and (1) misbehaves: looks like it expects physically separate buffers. I need to supply (2) with references to really distinct scalars. I understand it's probably better to be addressed at C side, but I'm not good with C and just lazy, therefore I'm assuming it'd be easier to make a physical string copy in pure Perl.

    use strict; use warnings; use PDL; use Storable 'dclone'; use Benchmark 'cmpthese'; for my $size ( 4 .. 8 ) { print "*** string size is 1E$size bytes\n"; my $data = byte( 256 * random( 10 ** $size )); my $buf = $data-> get_dataref; cmpthese( -2, { copy => sub { $data-> copy-> get_dataref }, dclone => sub { dclone $buf }, concat => sub { \( ${ $buf } . '' )}, }); print "\n"; } __END__ Perl executable: C:\berrybrew\strawberry-perl-5.32.0.1-64bit-PDL\perl\ +bin\perl.exe Perl version : 5.32.0 / MSWin32-x64-multi-thread PDL version : 2.021 *** string size is 1E4 bytes Rate copy dclone concat copy 56875/s -- -72% -95% dclone 201992/s 255% -- -83% concat 1182933/s 1980% 486% -- *** string size is 1E5 bytes Rate copy dclone concat copy 7703/s -- -90% -94% dclone 76109/s 888% -- -43% concat 134245/s 1643% 76% -- *** string size is 1E6 bytes Rate copy dclone concat copy 808/s -- -82% -83% dclone 4538/s 462% -- -4% concat 4713/s 484% 4% -- *** string size is 1E7 bytes Rate copy concat dclone copy 67.8/s -- -70% -71% concat 228/s 236% -- -1% dclone 230/s 240% 1% -- *** string size is 1E8 bytes Rate copy dclone concat copy 6.84/s -- -69% -69% dclone 21.8/s 219% -- -1% concat 22.1/s 223% 1% --
fastcgi without the module
3 direct replies — Read more / Contribute
by morgon
on Dec 01, 2020 at 14:57
    Hi,

    I have a little openwrt-box with lighthttpd that can do fastcgi.

    Unfortunately the way perl is packaged on openwrt is a bit strange (eg. when you only install the perl-package you won't get the strict-module) and I would like to confine myself to the perl-modules that are packaged for openwrt if possible and neither FCGI nor CGI::Fast are.

    So my question is:

    Is it possible to write a fast-cgi application in perl when all you have is the CGI.pm? What would you need to adapt from a pure cgi-script?

    Many thanks!

Organizing data from a hash
6 direct replies — Read more / Contribute
by audioboxer
on Dec 01, 2020 at 12:07

    Hi Everyone - I am iterating through data to build this below hash. Now using the keys and "cat_parent_id" I need to create a string from this looks like "Category1,Category2,Category3,Category4". I am at a loss and hoping someone can help. Thank you.

    $VAR1 = { '986172' => { 'cat_parent_id' => '', 'cat_name' => 'Category1' }, '986178' => { 'cat_parent_id' => '986177', 'cat_name' => 'Category4' }, '986177' => { 'cat_parent_id' => '986176', 'cat_name' => 'Category3' }, '986176' => { 'cat_name' => 'Category2', 'cat_parent_id' => '986172' } };
How can I sort my array numerically on part of the string?
10 direct replies — Read more / Contribute
by misterperl
on Dec 01, 2020 at 11:59
    I have this array:

    my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mink');

    that I'd like to sort numerically based on the number in front of the comma, so I tried:

    print join "\n", sort { ($a=~s/,.+//) <=> ( $b =~ s/,.+// ) } @list;

    But I seem to get an alpha, non-numeric result, which has the added detriment of dropping off my animals...

    1 2 22 11 001
    I also tried approaches like  "\A\d+$a" <=> "\A\d+$b" which the interpreter REALLY hated!

    TY Wise ones.

Experimental warnings with given/when
3 direct replies — Read more / Contribute
by Bod
on Nov 30, 2020 at 17:54

    given and when were introduced back in Perl v5.10 I believe. But they have not been deprecated...I know deprecation of an accepted feature is very, very rare in Perl but these are marked as experimental. I've been looking at them more for curiosity than any need to use them.

    Even with use experimental "switch"; they give a warning. I wasn't sure if this was on STDOUT or STDERR so I redirected STDERR and found that the warnings are not on there! I thought that use feature ... gave the warnings and use experimental ... didn't but in both cases I am getting the warnings. So, presumably, use warnings; overrides the experimental part.

    #use v5.10; #use feature "switch"; use experimental "switch"; use strict; use warnings; # Redirect STDERR open my $fh, '>', 'error.log'; *STDERR = $fh; my $i = 3; given($i) { print "$i\n" when $i < 4; } # Prove STDERR is redirected with runtime error my $error = 10 / 0;

    Given that deprecation is so very rare, is it safe to suppress the warnings and use some of the experimental features?

Understanding Devel::Peek output
3 direct replies — Read more / Contribute
by syphilis
on Nov 29, 2020 at 07:32
    Hi,

    On my 64-bit Windows 7 OS, I have:
    1) perl-5.32.0 built using a 32-bit gcc-8.3.0 compiler;
    2) perl-5.32.0 built using a 64-bit gcc-8.3.0 compiler.

    On both of those perls, the ivtype is the 8-byte "long long".

    On the 32-bit build I get:
    C:\>perl -MDevel::Peek -e "Dump 1 << 62;" SV = IV(0x1dc8d64) at 0x1dc8d64 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 4611686018427387904
    Note that the first line contains two occurrences of the same hex value - 0x1dc8d64 in this instance. Every time I run that command on that 32-bit build of perl, the two hex values are always equivalent - though, of course, they're not always 0x1dc8d64.

    When I run the same command on the 64-bit build, I get:
    C:\>perl -MDevel::Peek -le "Dump 1 << 62;" SV = IV(0x33d430) at 0x33d440 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,PROTECT,pIOK) IV = 4611686018427387904
    For this build of perl, it is always the case that the first hex value is less than the second value by 0x10.

    How is this differing behaviour between the 32-bit build and the 64-bit build explained ?

    IIUC, on the 64-bit build, the amount of memory being allocated for the IV is 0x10 (16) bytes more than is necessary ... and I wonder what is responsible for that extravagance.
    Is it perl ? ... or the OS ? ... or the compiler ? ...

    Cheers,
    Rob
Meditations
A readline + -e oddity: readline opens files even with -e --- need "readline STDIN" to read from console
4 direct replies — Read more / Contribute
by cxw
on Nov 30, 2020 at 10:32

    (This is my first meditation. I am posting in hopes it will help someone down the line! I can't find another node about this, but that doesn't mean there isn't one :) .)

    I needed to run a program and then wait for the user to hit Enter. I tried this:

    $ perl -e 'system(@ARGV); readline' echo foo foo Can't open echo: No such file or directory at -e line 1. Can't open foo: No such file or directory at -e line 1.
    Not what I expected!

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 browsing the Monastery: (5)
As of 2020-12-06 01:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (65 votes). Check out past polls.

    Notices?