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
Overriding Global Variables
4 direct replies — Read more / Contribute
by Mano_Man
on Nov 19, 2017 at 08:41

    Hi Monks,

    I'm looking for a way to have identical global variables in every package, and a single module that utilizes the data from them


    package my_first; use Debug; # Imports in sub debug our $DEBUG_FLAG =1; debug("Print me"); #prints: Print me 1; package my_second; use Debug; our $DEBUG_FLAG =0; debug("Print me"); #prints none 1; package Debug; our $DEBUG_FLAG =0; #Default value sub debug($) { if(DEBUG_FLAG){ say $_[0]; } } 1;

    Hope this is clear, appreciate any help.Looking for smart suggestions, better ways to do this (not OOP wise), and any other tidbit you can think of.

How to read a GEDCOM file
2 direct replies — Read more / Contribute
by JillB
on Nov 19, 2017 at 07:20

    Hello, this is my first ever go at Perl. For starters, I want to open a GEDCOM text file and count the number of lines in the file, and read a line. The script below does not work for me.It shows the count as 1, and no record is shown for the 10th record Can anyone please assist?

    #!/usr/bin/perl open(MYFILE, "C:/Users/Jill/Documents/Genealogy/birdt.ged") || die; @MyGed=birdt.ged; $count=@MyGed; print "10th record : $MyGed[10]\n"; print "No. of records : $count \n";
Mixing OR with conditional operator
2 direct replies — Read more / Contribute
by jockel
on Nov 19, 2017 at 05:12


    Can someone please explain this behavior. I was surprised with result of the following code. (And please help me come up with a better Title for this question)

    I think the variable $test should be set to $val1 (1) but instead it gets set to $val2 (undef)

    #/usr/bin/perl use strict; my $test; $test = 1 || (undef && undef != 0) ? undef : 0; print "test = $test\n";
    OUTPUT: test =
    -----BEGIN PERL GEEK CODE BLOCK----- Version: 0.01 P++>*$c--->---P6 > R >++++$M+>+++$O+++>+++$MA->+++$E > PU->++BD->-C+>+$D+>+$S->+++X >+WP >+++MO!PP n?CO--PO!>!(!)o?G!A--OLC--OLCC--OLJ--Ee !Ev-Eon-uL++>*uB!uS!uH-uo!w->!m+ ------END PERL GEEK CODE BLOCK------
How can i prevent perltidy from adding a semicolon after an ellipsis?
1 direct reply — Read more / Contribute
by karlgoethebier
on Nov 18, 2017 at 13:47

    Unfortunately... results in ...;. Thanks for any hint.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

template toolkit vs handlebars
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 18, 2017 at 11:52
    What is their difference? is TT rendered server side while handlebars client side?
    then if TT works with perl structures,what kind of data works handlebars with?json?
    and anyway why go for one rather the other?
Parsing DXF files for dimensional information
2 direct replies — Read more / Contribute
by talexb
on Nov 17, 2017 at 13:29

    It pains me that I know little about this area of technology, but I guess it's not possible to know everything. A colleague has a bunch of AutoCAD files that have to do with urban planning, and he wants to extract the following information from the equivalent DXF files:

    • Each corner point of each building lot with a latitude & longitude. Note that the lat. & long. may not be embedded in the file, but I am assuming the points are referred to on a relative scale so that they could be translated into lat & long data
    • The colour of each corner point (this is relevant information, added by the planner)
    • The area of each building lot - I believe that AutoCAD is calculating this.
    I have had a look at CPAN and haven't really found anything that will help me. My research on DXF tells me that it's a very low-level file, so I'm guessing that the information required may not even be available in the DXF file.

    If some monks have comments, thoughts, suggestions about where to begin, I'd appreciate it. Thanks!

    Alex / talexb / Toronto

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

Need a second set of eyes: Very odd failure
5 direct replies — Read more / Contribute
by blue_cowdawg
on Nov 17, 2017 at 11:57

    I must be missing something here so I'm asking for more eyes on something.

    I built a fairly simple straightforward module here and yet it is failing to work. This module does curls to an API (The Foreman) to extract some data from it. Nothing fancy here.... Here are the most important bits:

    package NCS::PuppetDB::Data; use 5.010000; use strict; use warnings; use JSON; our $curl="/usr/bin/curl -G 'http://localhost:8080/v3"; our $VERSION = '0.5.2'; sub new { my $self={}; bless $self,"NCS::PupptDB::Data"; return $self; } sub getHosts { my $self = shift; my $cmd=sprintf("%s/nodes 2>/dev/null",$curl); my $json = JSON->new->allow_nonref; my $jsonData=`$cmd`; my $decoded = $json->decode($jsonData); my $retval=[]; for my $entry(@$decoded){ push @$retval,$entry->{name}; } return sort $retval; } sub getHostFacts { my $self = shift; my $host = shift; my $cmd=sprintf("%s/nodes/%s/facts' 2>/dev/null",$curl,$host); my $json = JSON->new->allow_nonref; my $jsonData=`$cmd`; my $decoded = $json->decode($jsonData); my $retval = {}; for my $fact(@$decoded){ $retval->{$fact->{name}} = $fact->{value}; } return $retval; } sub getFact { my ($self,$host,$factname)=@_; my $facts=getHostFacts($host); return $facts->{$factname}; } 1;
    The one and only script using it (so far) is complaining:
    Can't locate object method "getHosts" via package "NCS::PupptDB::Data"
    What am I missing?

    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; Blog: http://blog.berghold.net Warning: No political correctness allowed.
How can increase number sequence in a variable
4 direct replies — Read more / Contribute
by yyy
on Nov 17, 2017 at 11:15
    Hi everyone,
    I have a few arrays naming @A_1, @A_2, @A_3.... I want to do a loop to for them, e.g.

    for(my $i =0; $i<10; $i++)
    { $A_$i[0] = 1234;

    I know $A_$i doesn't work. My question is how can I put $i into the array variable to make the sequence $A_1[0]=1234, $A_2[0]=1234,.......
Can I have a hard stop on a match
6 direct replies — Read more / Contribute
by BillB
on Nov 17, 2017 at 08:44
    I am fairly new to using regular expressions in perl and I am trying to do the following task.

    I am using the following match to search a sql script

    if $ScanString =~ m/insert\s.+doc.+[\(|values|select|;]?/is;

    It is returning true on the following code fragment. What I want is that if the table name of DOC follows the INSERT, but if it gets to any of "(", SELECT or ";" it will return false. In other words if the DOC is not there before I hit "(", SELECT or ";" it will always return false. The perl match from above is returning a false positive because the string "DOC_NUM" is in the attached select. However the full script can have many inserts. If any of them match the pattern I want it to be true. If none of them match I want the test to be false. Any help would be appreciated. The script fragment follows

    INSERT INTO gl_trn ( CO_CD, GL_ACCT_CD, JRNL_CD, EMP_CD_OP, POST_DT, DES, REF, DC_CD, AMT, TRN_DT, GL_TRN_ORIGIN_CD, FINAL_DT, CO_UNIT, TRN_TIME, CMNT, BNK_ACCT_CD, BNK_TRN_TP_CD, DOC_NUM, POST_ID_NUM, BNK_CO_CD) SELECT 'XXX', GL_ACCT_CD, NULL, EMP_CD_OP, to_date('&to_post_dt', 'MMD +DRR'), 'REVERSAL'||' '||substr(des, 1, 30), 'R'||substr(REF, 1, 18), decode(dc_cd, 'C', 'D', 'D', 'C'), amt, TRUNC(SYSDATE), gl_trn_origin_cd, NULL, co_unit, floor((sysdate-trunc(sysdate))*86400), 'Auto-generated by DEV using ACREV', BNK_ACCT_CD , BNK_TRN_TP_CD, DOC_NUM, POST_ID_NUM , BNK_CO_CD FROM gl_trn WHERE post_dt = to_date('&from_post_dt', 'MMDDRR') AND jrnl_cd = 'A' AND co_cd = 'RAY' AND NOT EXISTS (SELECT 'X' FROM gl_reversed g WHERE g.GL_TRN_SEQ_NUM = gl_trn.gl_trn_seq_num);
Removing a test script from perl source
2 direct replies — Read more / Contribute
by syphilis
on Nov 17, 2017 at 06:59
    This question relates to a transient, ongoing problem that I have running 'make test' when building perl from source on Windows using mingw-w64 64-bit ports of gcc.
    (Never any such problem with the mingw-w64 32-bit ports of gcc.)

    In a nutshell, I often (but not always, depending upon the version of gcc) strike the problem that an IO-Compress test script will hang during 'make test'.
    My only means of escape from this hang (AFAIK) is to hit Ctrl-C, which kills the entire 'make test' process - meaning that none of the other ensuing tests are run.

    Currently, the offender is cpan/IO-Compress/t/105oneshot-rawdeflate.t when building 64-bit blead.
    No problem with 'make test' when blead is built using 64-bit gcc-7.1.0, but that script hangs when blead is built with 64-bit gcc-7.2.0.

    My question:
    How do I re-arrange the source so that 'make test' will skip cpan/IO-Compress/t/105oneshot-rawdeflate.t, and yet run all of the other test scripts ?

    I should add that AFAICS there's really no problem with either the actual hanging test script, or the perl that has been built.
    These hangs only ever occur when running the script under 'make test'.


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.