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
.dancer -- what's it for
1 direct reply — Read more / Contribute
by kcott
on Jan 22, 2021 at 11:28

    G'day All,

    I keep seeing the file '.dancer' when working with Dancer2. It's in every tutorial but never explained. I've no idea what this file is for.

    Any ideas?

    — Ken

Internal SSL error after Ubuntu update
3 direct replies — Read more / Contribute
by afoken
on Jan 22, 2021 at 08:04

    It's Friday, I've switched from developer to admin, as usual, and decided to upgrade some of our machines.

    There's a script to set permissions on a Linux machine based on what can be found in an LDAP server of the Samba NT4 domain controller. Not pretty, but it is working.

    After updating to Ubuntu 20.04.1 LTS, LDAP access does not work any more:

    SSL connect attempt failed error:14161044:SSL routines:state_machine:i +nternal error at myscript line 23

    Line 23 is:

    my $ldap=Company::LDAP->new();

    Company::LDAP inherits from Net::LDAP and reads all required options for Net::LDAP->new() from a global configuration file:

    my $conffile='/etc/ldap.conf'; sub my_conf { state %conf; my $key=shift; unless ($conf{'.read'}++) { open my $f,'<',$conffile or die "Can't open $conffile +for reading: $!"; while (<$f>) { next if /^\#/; next if /^\s+$/; s/^\s+//; s/\s+$//; my ($k,$v)=split /\s+/,$_,2; warn "Duplicate key $k in $conffile line $.\n" + if exists $conf{$k}; $conf{$k}=$v; } close $f; my $fn=$conffile; $fn=~s/\.conf$/.secret/; open my $f2,'<',$fn or die "Can't open $fn for reading +: $!"; $conf{'.secret'}=<$f2>; chomp $conf{'.secret'}; close $f2; } return $conf{$key}; } sub new { my $proto=shift; my $uri=URI->new(my_conf('uri') // die "Missing uri in $conffi +le\n"); my $host=$uri->host(); my $scheme=$uri->scheme(); my $path=$uri->path(); my $port=$uri->port(); my %opts=( onerror => 'die', host => $host, scheme => $scheme, port => $port, timeout => my_conf('timelimit')//120, version => my_conf('ldap_version')//3, inet4 => 1, inet6 => 0, ); my $ldap=$proto->SUPER::new($host,%opts) or die "Can't connect + to $host: $@"; if ((my_conf('ssl')//'') eq 'start_tls') { %opts=(); $opts{'verify'}='none'; $opts{'cafile'}=my_conf('tls_cacertfile') // die "Miss +ing tls_cacertfile in $conffile\n"; # $opts{'capath'}=$opts{'cafile'}=~s|/[^/]+$|/|; $opts{'sslversion'}='tlsv1_1'; $ldap->start_tls(%opts); } return $ldap; }

    /etc/ldap.conf is

    base dc=company,dc=de uri ldap://ldap.company.de/ ldap_version 3 rootbinddn cn=ldapadmin,dc=company,dc=de timelimit 5 bind_timelimit 3 pam_password crypt ssl start_tls tls_cacertfile /etc/ssl/certs/company-cacert.pem

    (plus a lot of comments)

    The problematic machine is actually a copy of another machine, still running the older Ubuntu 18.04.5 LTS. That one runs exactly the same code without problems.

    Google gave me tons of results, but with an SNR close to zero. I've no clue what is wrong here.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
psql splitter for php
5 direct replies — Read more / Contribute
by TheloniusMonk
on Jan 22, 2021 at 07:13
    Dear Monks,

    I am working on a migration project, where there will be parallel run for all the migrated servers for a period of time. One of these servers is a LAPP stack with cron jobs written in bash and php.

    During the parallel run, it is necessary to be able to turn off production scripts in the old environment as soon as the migrated versions are tested because there are as yet unmigrated shared storage devices in the landscape.

    So, although the storage will be updated by the new servers, instead of the old, for such scripts, a psql splitter is needed in the case of psql.

    For bash, I have a working solution: In the bash scripts I alias psql to ~/bin/psql.pl, and this script contacts two databases instead of one, transparently to the bash script (see below).

    My challenge for today is to do the same for php scripts. I.E., create a similar Perl script as the below, but find a way to alias calls like $link = pg_connect('host=localhost dbname=DBNAME user=USERNAME password=XYZ');
    So that subsequent references to $link will send the SQL to two databases similarly to the below solution for bash. I can't begin to write the phppsql.pl for this not knowing how the interface will look. Bear in mind there are a large number of such php scripts.

    #!/usr/bin/perl use strict; use warnings; my $g=main->new; $g->init; $g->parse; $g->enact; package main; sub new { return bless {}, shift; } sub init { my $this = shift; $this->{host1} = '<new-server>.com'; $this->{host2} = '<old-server>.com'; } sub parse { my $this = shift; my $cmd = join(' ', @ARGV); my ($host); if ($cmd =~ s/\-h\s*(\S*)//) { $host = $1; unless ($host eq $this->{host1}) { $this->{host1} = $host; $this->{host2} = ''; } } if ($cmd =~ s/\-c\s*(.*)$//) { $this->{sql} = $1; } else { $this->{sql} = ''; } $this->{cmd1} = '/bin/psql -h ' . $this->{host1} . ' ' . $cmd;; $this->{host2} and $this->{cmd2} = '/bin/psql -h ' . $this->{host2} . ' ' . $cm +d; } sub enact { my $this = shift; if ($this->{sql}) { my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; } } else { my @stdin = <STDIN>; my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph @stdin; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph @stdin; close $ph; waitpid $pid, 0; } } } 1; __END__
    Many thanks in advance!


Unpacking byte stream long/quad little/big endian fields
2 direct replies — Read more / Contribute
by GrandFather
on Jan 21, 2021 at 19:54

    I have a binary file (think ELF or Windows PE) that includes fields that may be long (32 bit) or quad (64) bit and little or big endien depending on the specific file. The script may be running on a big or little endian machine with a Perl built for 32 or 64 bits. I want to unpack the fields for later use that will include display using printf and file operations using read and seek so I need to convert from file representation to the running Perl's native representation for those fields. The following sample code does that, but the pack 'L2' ...; unpack 'Q' feels a bit clunky. Can it be tidied up?

    use warnings; use strict; use Config; use Fcntl; printf "Perl $^V %s ivsize %d byteorder %d\n", $Config{archname}, $Config{ivsize}, $Config{byteorder}; # Generate a "binary file" with a string of bytes likely to show up is +sues in # decoding my $binary = "\x91\x34\x33\x90\x81\x32\x31\x80"; for my $fileLE (0, 1) { my $fromFile = $fileLE ? 'V4' : 'N4'; open my $inFH, '<:raw', \$binary; read $inFH, my $raw1, 4; read $inFH, my $raw2, 4; my $long1 = unpack $fromFile, $raw1; my $long2 = unpack $fromFile, $raw2; my $packed = pack "L2", $fileLE ? ($long1, $long2) : ($long2, $lon +g1); my $longlong = unpack 'Q', $packed; printf "%s: %016x\n", ($fileLE ? "LE" : "BE"), $longlong; }

    A 32 bit Windows build prints:

    Perl v5.32.0 MSWin32-x86-multi-thread-64int ivsize 8 byteorder 1234567 +8 BE: 9134339081323180 LE: 8031328190333491

    A nice sanity check would be to run the code on a big endian system and see that the numbers generated are the same. Of course the long long processing will come unstuck where ivsize < 8.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
perl script to conda package
1 direct reply — Read more / Contribute
by jnarayan81
on Jan 21, 2021 at 11:51

    Hi Monks, I'm curious if someone was trying to build a conda package of perl scripts/tools?

    I tried to pursue this, but I failed ;( https://docs.conda.io/projects/conda-build/en/latest/user-guide/tutorials/build-pkgs.html

    Any suggestions or turorial links could help ...

Handling multiple output files simultaneously using arrays of filehandles and filenames
4 direct replies — Read more / Contribute
by Cosmic37
on Jan 21, 2021 at 11:20
    use strict; use warnings; my ($sector,$path); my @sectorOutputFiles; my @fh; $path="blah"; for($sector=0;$sector<12;$sector++){ $sectorOutputFiles[$sector]=">".$path.$filenamefirstpart.$sector." +.txt"; open($fh[$sector],'>',$sectorOutputFiles[$sector]) or die "Can't open sector filehandler for writing.\n"; }
    Hello Monkees! I try to open an array of filehandles associated with an array of filenames in the above loop. However, it dies... Can't open sector filehandler for writing. Any ideas why that might be? Thanks and good karma for any ideas...
Cannot get Marpa::R2 to prioritise one rule over another
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 20, 2021 at 23:58

    I have a Marpa::R2 parser that is attempting to differentiate between IP addresses and hostnames without a difference in leading keywords. The actual grammar I am using is complicated enough not to try and replicate it here, but a minimally-reproducable example of the same problem is below:

    #!/usr/bin/env perl use warnings; use strict; use Data::Dumper; use Term::ANSIColor qw(:constants); use Marpa::R2; my $rules = <<'END_OF_GRAMMAR'; lexeme default = latm => 1 :default ::= action => [name,values] :start ::= <entry> <entry> ::= <op> (SP) <hostaddr4> <op> ::= 'add' | 'remove' <ipv4> ::= NUMBER ('.') NUMBER ('.') NUMBER ('.') NUMBER <hostname> ::= NAME <hostaddr4> ::= <ipv4> | <hostname> SP ~ [\s]+ NAME ~ [\S]+ NUMBER ~ [\d]+ END_OF_GRAMMAR my $input = <<'END_OF_INPUT'; add add www.example.org remove END_OF_INPUT my $grammar = Marpa::R2::Scanless::G->new({source => \$rules}); for (split /^/m, $input) { chomp; if (length $_) { print "\n\n$_\n"; my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar, ranking_method => 'rule' }); eval { $recce->read(\$_ ) }; print ($@ ? (RED . "$@\n") : GREEN); print $recce->show_progress(), "\n"; print Dumper($recce->value), "\n\n", RESET; } }

    From what I can tell, Marpa always picks the <hostname> form of the grammar, even on lines that look more like IPs. I assume this is because the character class [\S]+ also includes the characters which make up an IP address.

    So far, in my grammar definition, I've tried:

    <hostaddr4> ::= <ipv4> | <hostname> <hostaddr4> ::= <ipv4> || <hostname> <hostaddr4> ::= <hostname> | <ipv4> <hostaddr4> ::= <hostname> || <ipv4> <hostaddr4> ::= <ipv4> rank => 2 | <hostname> rank => 1 <hostaddr4> ::= <ipv4> rank => 1 | <hostname> rank => 2 <hostaddr4> ::= <ipv4> rank => 1 <hostaddr4> ::= <hostname> rank => 2 <hostaddr4> ::= <hostname> rank => 1 <hostaddr4> ::= <ipv4> rank => 2

    ...and none seem to make a difference. They all yield the ['hostname', ''] array.

    The only thing that does it is removing the <hostname> alternate from <hostaddr4> (which does not match the grammar of the data I am parsing), and then the representation changes to ['ipv4', '192', '0', '2', '1']

    Can anyone advise the correct approach in this (seemingly) simple case?

How do I make a Perl install NOT use the test harness?
4 direct replies — Read more / Contribute
by mpersico
on Jan 20, 2021 at 16:23
    I've got a perl 5.32 build being executed by a CI system on a remote box. The log of the build abruptly halts about a third of the way through the tests. Is there any way to not use the test harness or make it spit out more than just foobar..................ok for tests? I don't even get sometest..............nook, so I am hoping that getting more output might lead to more insights. Thank you.
How to replace \t with \s
4 direct replies — Read more / Contribute
by Anonymous Monk
on Jan 20, 2021 at 09:14

    I have a many large documents with tabs in them. They are not all the same length. I want to do something like this: tr/\t/\s\s\s\s/ ## replace \t with 4 spaces There is a thing called Text::Tabs -- but that appears to work only for tables. My goal is to do: @text = split /\s/, $_; @text[0] = $string join /\s/, @text Any ideas?

Scrap shadow DOM website with Perl
2 direct replies — Read more / Contribute
by vpperl
on Jan 20, 2021 at 08:32

    Hi all, My first post here, so hope I'm following all rules, as well as I'm not asking stipid questions :)

    Guys, I'm trying to extract the content of a website, which is implemented with DOM objects, so the pure HTML parsing is not giving me any favor. For example, I'm trying to extract all player names from the following team:


    Any suggestions/ideas/examples where to start with? I have some Perl experience, but haven't been able to do find anything that would help me with. Thanks!

Add your question
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.