Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Seekers of Perl Wisdom

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

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
The behavior when assigning an array to scalar?
2 direct replies — Read more / Contribute
by fgg1991
on Sep 26, 2017 at 05:49
    my code is like: my $r = '111','222','aaa','bbb'; print $r; my $s = ('111','222','aaa','bbb'); print $s; I understand that in this situation $r woulb be a ref and print $r would give me sth like ARRAY(0x35423542), but I don't understant the second example, why it would give me bbb, the last element ?
using "require" problem
3 direct replies — Read more / Contribute
by gauss76
on Sep 26, 2017 at 03:51

    Hi All

    I have the following problem and don't know how to resolve it.

    Basically, I have a perl .pl file ( that requires another .pl file ( in order to work correctly (everything running on a Linux machine). The files are both in the same folder and I currently use the following syntax in the file

    require "./";

    This works fine if I run from the folder that contians the two files. However if I run the program from a different folder with, say, the command

    perl /home/user/code/

    Then I get an error saying "can't locate ./

    I know that I can change the "require" statement to:

    require "/home/user/code/"

    However, for me, this is not an option as I need to copy the code to another Linux system where the containing folder will be different (the two files will still be in the same folder)

    So, my question: Is there any way to use the same "require" statement so that I can run my program from any folder with the command

    perl /home/user/code/

    where the folder for the code (/home/user/code/ can change?

    Please let me know if anything is not clear.

    Many thanks for any help on this


String tags
2 direct replies — Read more / Contribute
by rajaman
on Sep 25, 2017 at 19:24
    Dear Monks,

    I have a list of tags with their index locations with respect to a given string. For example:

    String='Titles consisting of a single word are discouraged, and in most cases are disallowed outright.'

    tag: tag category: tag-id: start index location: end index location
    consisting of: cat1: id1: 7: 20
    discouraged: cat1: id2: 39: 50
    most cases: cat2: id3: 59: 69
    Using the given tag index locations, I want to tag the string as:

    Titles (cat1: id1)consisting of(cat1) a single word are (cat1: id2)discouraged(cat1), and in (cat3: id1)most cases(cat3) are disallowed outright.

    I tried to attempt this problem by iterating over tag index positions and using 'substr' function over the string for each iteration. However, the problem is that after each iteration the indexes of the string characters change. Please suggest any efficient way of doing this.

CPAN Catch-22
1 direct reply — Read more / Contribute
by snax
on Sep 25, 2017 at 16:37

    So I've managed to shoot myself in the foot. Earlier today I did an update of my CPAN module, via a perl -MCPAN -e shell session. This was kind of a reflex because I hadn't used CPAN for a while. That turned out OK (upgraded to v2.14 of CPAN), but ever since, any time I use perl -MCPAN -e shell and try to install or update anything, I get an annoying error:

    Will not use CPAN::Meta::Requirements, need version 2.120920

    Naturally that requirement is a literal specification in the v2.14 module code. It's not "need >=". I have v2.125 of CPAN::Meta::Requirements.

    Any clues on how best to sort this out? I grabbed source for CPAN v 2.16 but building that shows I need to upgrade other things, and I want to be able to rely on the CPAN module for managing packages. It's great when it works right :)

Run multiple alert monitoring perl scripts on several Linux machines hosting multiple oracle DB
3 direct replies — Read more / Contribute
by aravind.kalla
on Sep 25, 2017 at 16:06

    Hi Monks,

    I am looking for Perl solutions where I need to read JSON configuration file containing linux server details and connected oracle DB information,
    read this and run several Alert scripts on these DB's...say 100 Linux servers each hosting 4 oracle DB...The process is currently working in a serial manner.

    i.e. reads the json file for {1st Linux server->1st DB-> Run Alert scripts.}
    1st Linux server->2nd DB-> Run Alert scripts.
    ...3rd DB
    ...4th DB

    Then reads the json file for {2nd Linux server ->1st DB-> Run Alert scripts}
    2nd Linux server->2nd DB-> Run Alert scripts.
    ...3rd DB
    ...4th DB

    and so on for next 100 (linux servers) * 4 (DBS) = 400 (hosts to monitor.)

    This is again connected to a scheduling tool crontab for every 60 mins... but since its running serially for all 100 servers its taking nearly 90 mins ....and hence the second run overlapping the first run. Looking for robust parallel solutions!!!!

If statement seems to ignore elsif and skips to else
3 direct replies — Read more / Contribute
by edgreenberg
on Sep 25, 2017 at 14:31

    This little gem comes from the logwatch script that reads the fail2ban log. I'm not getting any output, and put the script under the debugger.

    while (defined(my $ThisLine = <STDIN>)) { if ( $Debug >= 5 ) { print STDERR "DEBUG($DebugCounter): $ThisLine"; $DebugCounter++; } chomp($ThisLine); if ( ($ThisLine =~ /..,... DEBUG: /) or ($ThisLine =~ /..,... \S*\s*: DEBUG /) or # syntax of 0.7.? f +ail2ban ($ThisLine =~ /..,... INFO: (Fail2Ban v.* is running|Exiting| +Enabled sections:)/) or ($ThisLine =~ /INFO\s+Log rotation detected for/) or ($ThisLine =~ /INFO\s+Jail.+(?:stopped|started|uses poller)/) + or ($ThisLine =~ /INFO\s+Changed logging target to/) or ($ThisLine =~ /INFO\s+Creating new jail/) or ($ThisLine =~ /..,... \S+\s*: INFO\s+(Set |Socket|Exiting|Gam +in|Created|Added|Using)/) or # syntax of 0.7.? fail2ban ($ThisLine =~ /..,... WARNING: Verbose level is /) or ($ThisLine =~ /..,... WARNING: Restoring firewall rules/) ) { if ( $Debug >= 6 ) { print STDERR "DEBUG($DebugCounter): line ignored\n"; } } elsif ( my ($Service,$Host,$NumFailures) = ($ThisLine =~ m/INFO: + (\S+): (.+) has (\d+) login failure\(s\). Banned./)) { if ($Debug >= 4) { print STDERR "DEBUG: Found host $Host trying to access $Se +rvice - failed $NumFailures times\n"; } push @{$ServicesBans{$Service}{$Host}{'Failures'}}, $NumFailur +es; } elsif ( my ($Service,$Host) = ($ThisLine =~ m/ ERROR:\s(.*):\s(\ +S+)\salready in ban list/)) { $ServicesBans{$Service}{$Host}{'AlreadyInTheList'}++; } elsif ( my ($Service,$Host) = ($ThisLine =~ m/WARNING\s*\[(.*)\] +\s*(\S+)\s*already banned/)) { $ServicesBans{$Service}{$Host}{'AlreadyInTheList'}++; } elsif ( my ($Service,$Host) = ($ThisLine =~ m/ WARNING:\s(.*):\s +ReBan (\S+)/)) { $ServicesBans{$Service}{$Host}{'ReBan'}++; } elsif ($ThisLine =~ / ERROR:?\s*(Execution of command )?\'?iptab +les/) { push @IptablesErrors, "$ThisLine\n"; } elsif ($ThisLine =~ /ERROR.*returned \d+$/) { push @ActionErrors, "$ThisLine\n"; } elsif (($ThisLine =~ /..,... WARNING: \#\S+ reinitialization of +firewalls/) or ($ThisLine =~ / ERROR\s*Invariant check failed. Trying to +restore a sane environment/)) { $ReInitializations++; } elsif ($ThisLine =~ /..,... WARNING: is not a valid IP address/ +) { # just ignore - this will be fixed within fail2ban and is harm +less warning } else { # Report any unmatched entries... push @OtherList, "$ThisLine\n"; } }

    When I run this under the debugger, and I advance using the "s" command, should I expect the debugger to stop on each of the elsif statements before evaluating it, or should it only stop if the argument is true?

    Many Thanks, Ed Greenberg undefined symbol & symbol lookup error
1 direct reply — Read more / Contribute
by daniel20091
on Sep 25, 2017 at 11:09
    Hi All,

    I am running a perl script on a web server. I am getting the error in the httpd error log file:

    /usr/bin/perl: symbol lookup error: /usr/local/lib64/perl5/auto/Bio/Ex +t/Align/ undefined symbol: bp_sw_read_Blast_file_CompMat, re +ferer: http://XX/

    I checked some of the forums and did this:


    cpanp -i /usr/local/lib64/perl5/auto/Bio/Ext/Align/ [ERROR] '/usr/local/lib64/perl5/auto/Bio/Ext/Align/' does not +contain an author part [ERROR] Cannot find '/usr/local/lib64/perl5/auto/Bio/Ext/Align/Align.s +o' in the module tree No such module: /usr/local/lib64/perl5/auto/Bio/Ext/Align/ No modules found to operate on! Nothing done


    I checked, "" exists in the path shown in the error message

    Can someone please help me solve the problem. Thanks much, Dany

When modules install in perl5
6 direct replies — Read more / Contribute
by cristofayre
on Sep 25, 2017 at 08:00

    My shared server has the usual "!#/usr/bin/perl" for use by everyone, but personal installations appear to go into "perl/usr/lib/perl5" folder.

    So if my scripts access the former, all is well. But to access the latter ...??? Have I got to change all my scripts to the new path of "!# perl/usr/lib/perl5" or is there a way to 'force' the new path into @inc. How would you do it if some "use xxx" are in /usr/bin/perl, and some in the second folder?

    Did my last host use a "symlink" for this, and if so, where did he put it.

    Finally, if the "use" entry could not be found, would this generate a "suexec violation" error? A script using "File::Find::Rule", permission 755 designed to update all the file permissions came back with the above for a 500 error. I asked host, and their response: "Please set all your perl files to 755" (In otherwords, they had no idea!)

    This is the script:

    #!/usr/bin/perl print "content-type: text/html\n\n"; # use CGI::Carp qw( fatalsToBrowser ); use File::Find::Rule; my @files = File::Find::Rule->file()->name('*.pl')->in('/home/cristofa +/public_html'); # set ->in('.') to start from current directory for ($x=0; $x<@files; $x++){ chmod (0755, $files[$x]); print "$files[$x]&lt;br&gt;"; } print "All done";

    # CARP is commented out because - whilst CGI is installed - it doesn't specifically list CGI::CARP. I installed the latter ... which comes back to the initial "perl5" path query! Script works on my Windows / Strawberry perl version.

    And yes, I know you CAN use a foreach / while loop ... but I still find that a bit 'symbolic' and prefer to 'see' what's happening each loop.

Looping trough Array Key/Value Pairs
6 direct replies — Read more / Contribute
by maikelnight
on Sep 25, 2017 at 06:45

    Dear Monks, i am new to perl...I have an array which Dumps to this:

    $VAR1 = { '_shards' => { 'skipped' => 0, 'successful' => 5, 'total' => 5, 'failed' => 0 }, 'hits' => { 'hits' => [ { '_id' => 'AV6SrwuTv7sBjjRqMiW1', '_source' => { 'request' => '/inde +x.php', 'clientip' => '192. +168.1.1' }, '_type' => 'nginx', '_index' => 'nginx-2017.09.18', '_score' => '4.238926' }, { '_id' => 'AV6UL-DOv7sBjjRqMidb', '_source' => { 'clientip' => '192. +168.1.1', 'request' => '/' }, '_score' => '4.189655', '_type' => 'nginx', '_index' => 'nginx-2017.09.18' } ], 'total' => 2, 'max_score' => '4.238926' }, 'took' => 0, 'timed_out' => undef };

    I try to get the key/values from '_source' but i dont know how. I can dump for example with: "print Dumper $_->{'hits'}{'hits'}->[0]->{'_source'};" and foreach, and get the pair from [0] and i can alter [0] to 1 and get the other pair.

    foreach $_(@testarray) { print Dumper $_->{'hits'}{'hits'}->[0]->{'_source'}; }

    But how can i loop through to get all the values? I tried to loop with foreach and a counter but it didnt work, it always gives [0] (indeed i might make a mistake). May i ask one to get me some help how to? Thanks a lot.

Why is @flds not imported?
3 direct replies — Read more / Contribute
by perl-diddler
on Sep 24, 2017 at 17:38
    I was getting an error in one of my progs and distilled the problem. I only tested it on 5.16 and 5.20, but I don't remember any changes in more recent perls that might indicate it's fixed.
    # # note: may need to install "mem" ("cpan -i mem") # use strict; use warnings; ###################################################################### +########## { package Fields; use strict; use warnings; use mem; our (@flds, @EXPORT); @EXPORT=qw(@flds); use Exporter 'import'; } ###################################################################### +########## package main; use strict; use warnings; use Fields; my $nfld = scalar @flds;

    I get error:

    > perl t02-array.t Global symbol "@flds" requires explicit package name at t02-array.t li +ne 19. Execution of t02-array.t aborted due to compilation errors.
    If I put qw(@flds) after the use Fields, I get:
    "@flds" is not exported by the Fields module
    I think I'm missing something obvious.... :-(.


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.
  • Log In?

    What's my password?
    Create A New User
    [ambrus]: Corion: the solution depends on who for. Some non-math presentations need to show lots of graphics or photos, with fine details and color. For those, blackboard or overhead transparencies aren't so good (yes, you can print on transparencies, but there's s
    [ambrus]: ome quality limits), but film projection or computer projector is fine.
    [ambrus]: A minority of math presenters are traditional and don't want to operate a computer. For them, handwritten overhead transparencies and blackboard are fine.
    [Corion]: BTW, has anybody seen the Microsoft Surface Studio thing live?
    [ambrus]: Some will operate a computer, but want to operate crazy unusable android-like GUIs. For those, printed overhead transparencies or simple slide editors and maybe even tablets are fine.
    [ambrus]: Some people like me are fine in learning how to use some custom user interface. For me, I might want to make some keyboard- controlled interactive live slide writer program, like I said in the hypothetical case when I have lots of years of free time for th
    [Corion]: ambrus: Some of my math professors delegated TeX-ing of the exercises etc. to their secretaries :) But they all had very neat handwriting and good blackboard operation anyway ;)
    [ambrus]: is.

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (8)
    As of 2017-09-26 10:36 GMT
    Find Nodes?
      Voting Booth?
      During the recent solar eclipse, I:

      Results (293 votes). Check out past polls.