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
Trouble skipping lines using Perl
3 direct replies — Read more / Contribute
by LeBran
on Nov 21, 2017 at 10:55

    Hello all, I'm struggling to get Perl to work in skipping lines containing certain features which means I don't need to use them, basically I'm just unsure of the syntax, though it appears to work in some places and not in others

    Looking at this following table;

    chrM 2928 A G 17 0 0% A 46 105 69.54% + R Somatic 1 1.23052720566294E-008 17 29 50 55 + 4 13 0 0 chr1 108310 T C 9 0 0% T 3 5 62.5% Y + Somatic 1 0.0090497738 2 1 2 3 7 2 0 + 0 chr1 726958 T A 11 0 0% T 9 4 30.77% + W Somatic 1 0.0672877847 6 3 1 3 5 6 0 + 0 chr1 1412720 C A 33 0 0% C 22 6 21.43% + M Somatic 1 0.0067850063 10 12 3 3 14 1 +9 0 0 chr1 1822396 C G 6 0 0% C 4 4 50% S + Somatic 1 0.0699300699 3 1 2 2 4 2 0 +0 chr1 1822457 C T 10 0 0% C 4 4 50% Y + Somatic 1 0.022875817 3 1 2 2 6 4 0 + 0

    I'm trying to skip the lines which are chrM rather than chr1, the following code is what I came up with (I'm also extracting different columns)

    while (my $line = <FILE>) { next if ($. == 1); chomp $line; my @sepline = split ("\t", $line); my $chromosome = $sepline[0]; my $chrpos = $sepline[1]; my $nmreads = $sepline[8]; my $mutants = $sepline[9]; my $totalreads = $nmreads + $mutants; next if ($chromosome = /^chrM/); print ("$nmreads $mutants $totalreads\n"); }

    So the script works fine if I # out the lower next if function towards the bottom of the script, I've also tried "next if ($chromosome = "chrM");" and "if ($chromosome = "chrM") { next;} but neither will work. Is there something incorrect about my syntax or am I simply going about it completely the wrong way? Appreciate any help, cheers

Converting hash into js object properties
1 direct reply — Read more / Contribute
by nysus
on Nov 21, 2017 at 10:52

    What module exists where I can take a hash like this:

    my %hash = ( rules => {name => '"required"', email => { required => true, email => true } }, message => { name => '"Please specify your name"', email => { required => '"We need your email address to +contact you"', email => '"Your email address must be in the format +of name@domain.com"', } } );
    </code>

    And convert it to something like this for use in a js function:

    rules: { name: "required", email: { required: true, email: true } }, messages: { name: "Please specify your name", email: { required: "We need your email address to contact you", email: "Your email address must be in the format of name@domain. +com" } }

    Ideally, the module would be smart enough to know when to double quote the values and when not to.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

5.26 and prebuilt libraries on Windows
1 direct reply — Read more / Contribute
by Marc-Philip
on Nov 21, 2017 at 10:29
    Hi, I'm trying to build perl 5.26.1 from source on Windows using the Visual Studio 2010 on a 64bit machine:
    1. edit win32/Makefile to set CCTYPE=MSVC100
    2. run vcvarsall.bat amd64 to open a command shell with the development environment
    3. cwd with that shell to the win32 folder
    4. run nmake

    This succeeds. Now I want to build the Compress::Raw:Zlib extension, that comes with the perl tarball, against my prebuilt zlib. To do this, one needs to set the environment variables ZLIB_LIB and ZLIB_INCLUDE to the folders that contain zlib.lib resp. zlib.h and BUILD_ZLIB to "False". When I run nmake now, I get this error:

    link -out:..\..\lib\auto\Compress\Raw\Zlib\Zlib.dll -dll -nolo +go -nodefaultlib -debug -opt:ref,icf -ltcg -libpath:"c:\perl\lib\COR +E" -machine:AMD64 Zlib.obj "..\..\lib\CORE\perl526.lib" "C:\SAPDev +elop\hmexternals\perl\import\content\src_zlib-1.2.11-sap7-ntamd64.tar +.gz\zlib-1.2.11-sap7-ntamd64-release-msvc2010\lib\zlib.lib" "c:\Progr +am Files (x86)\Microsoft Visual Studio 10.0\VC\\lib\oldnames.lib" "c: +\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\kernel32.li +b" "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\user3 +2.lib" "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\g +di32.lib" "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x6 +4\winspool.lib" "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\ +lib\x64\comdlg32.lib" "c:\Program Files(x86)\Microsoft SDKs\Windows\v +7.0A\lib\x64\advapi32.lib" "c:\Program Files (x86)\Microsoft SDKs\Win +dows\v7.0A\lib\x64\shell32.lib" "c:\Program Files (x86)\Microsoft SDK +s\Windows\v7.0A\lib\x64\ole32.lib" "c:\Program Files (x86)\Microsoft +SDKs\Windows\v7.0A\lib\x64\oleaut32.lib" "c:\Program Files (x86)\Micr +osoft SDKs\Windows\v7.0A\lib\x64\netapi32.lib" "c:\Program Files (x86 +)\Microsoft SDKs\Windows\v7.0A\lib\x64\uuid.lib" "c:\Program Files (x +86)\Microsoft SDKs\Windows\v7.0A\lib\x64\ws2_32.lib" "c:\Program File +s (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\mpr.lib" "c:\Program Fil +es (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\winmm.lib" "c:\Program +Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\version.lib" "c:\Pro +gram Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\odbc32.lib" "c: +\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\odbccp32.li +b" "c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\lib\x64\comct +l32.lib" "c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\lib +\msvcrt.lib" -def:Zlib.def Creating library ..\..\lib\auto\Compress\Raw\Zlib\Zlib.lib and obje +ct ..\..\lib\auto\Compress\Raw\Zlib\Zlib.exp Zlib.obj : error LNK2001: unresolved external symbol __security_check_ +cookie Zlib.obj : error LNK2001: unresolved external symbol memcpy zlib.lib(inflate.obj) : error LNK2001: unresolved external symbol memc +py zlib.lib(deflate.obj) : error LNK2001: unresolved external symbol memc +py zlib.lib(trees.obj) : error LNK2001: unresolved external symbol memcpy Zlib.obj : error LNK2001: unresolved external symbol __imp_memmove Zlib.obj : error LNK2001: unresolved external symbol memcmp Zlib.obj : error LNK2001: unresolved external symbol strlen Zlib.obj : error LNK2001: unresolved external symbol __imp__errno Zlib.obj : error LNK2001: unresolved external symbol __imp_printf Zlib.obj : error LNK2001: unresolved external symbol memset zlib.lib(deflate.obj) : error LNK2001: unresolved external symbol mems +et LINK : error LNK2001: unresolved external symbol _DllMainCRTStartup zlib.lib(zutil.obj) : error LNK2001: unresolved external symbol __imp_ +malloc zlib.lib(zutil.obj) : error LNK2001: unresolved external symbol __imp_ +free ..\..\lib\auto\Compress\Raw\Zlib\Zlib.dll : fatal error LNK1120: 11 un +resolved externals NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual S +tudio 10.0\VC\BIN\amd64\link.EXE"' : return code '0x460' Stop.

    The problem is that the path to msvcrt.lib is wrong. It uses c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\lib\msvcrt.lib, but there's the amd64 part missing. The correct path is C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\msvcrt.lib. Please note the missing amd64 is NOT where there's a double backslash.

    Compiling against the very same prebuilt zlib with the very same procedure works in perl 5.24.1. I suspect a problem in perl 5.26.1. I've noticed that libpth in Config_heavy.pl in 5.26.1 is libpth='"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\\lib"', where it's libpth='\lib' in perl 5.24.1.

    Does anyone have an idea? Please note that

    • this is not a problem of the Compress:Raw:Zlib extensions, but of the perl itself.
    • Compress::Raw::Zlib has a problem on Windows as well (https://rt.cpan.org/Ticket/Display.html?id=123699)

    Thanks and Regards Marc-Philip

[tk] passing a variable to -value field in radio button
1 direct reply — Read more / Contribute
by Anonymous Monk
on Nov 21, 2017 at 09:51

    Hi Monks, I am building a graphical interface for my program, using radio Buttons

    I would like to have 3 buttons linked to a same variable, 2 of them have a fixed -value field. I want the last one to be configurable by the user

    I would like to configure this value using a ttk_entry, here's an extract of my code

    $Mode="Mode1"; my $entry26 = $lf21->new_ttk__radiobutton(-text => "Mode1", -variable +=> \$Mode, -value => "Mode1"); my $entry27 = $lf21->new_ttk__radiobutton(-text => "Mode2", -variable +=> \$Mode, -value => "Mode2"); my $entry28 = $lf21->new_ttk__radiobutton(-text => "Other:", -variable + => \$Mode, -value => $OtherMode ); my $entry29 = $lf21->new_ttk__entry( -validate => 'focusout', -textvariable => \$OtherMode, );

    From the tests i did, i can say that $OtherMode takes the value of the entry but, if I select the last button, the $Mode variable contains an empty string

    do you have any idea of what i did wrong?

    thanks in advance, have a great day :)

Loading a part of the file to array using Tie::File
8 direct replies — Read more / Contribute
by ansh007
on Nov 21, 2017 at 09:44

    I am extremely new to perl and working on my 1st perl code. I am stuck at a point, where I need to read a part of a file using Tie::File;

    My Old code:
    open(LOG_READ,"cat $InLogFilePath|tail -n +$InStartLineNumber|") || di +e "can not open file :$!"; my @all_lines = <LOG_READ> ; close (LOG_READ); for (@all_lines) {.. }

    But this could eat a lot of memory, as the files are huge. So I found Tie::File online, that happens not to load arrays in memory. 1st question, is that correct ?

    If yes, I get:

     tie @array, 'Tie::File', $filename or die ...;

    But I do not want to read the whole file. Let's say I want to read from line number 100 till end. How do I do it, using Tie::File ? something similar to:

    tie @array, 'Tie::File', "cat $InLogFilePath|tail -n +$InStartLineNumber" or die ...;

    Looking forward to your help monks :)

"indir" in shebang line
3 direct replies — Read more / Contribute
by ForgotPasswordAgain
on Nov 21, 2017 at 04:28
    In perlrun, it mentions:
    If the #! line does not contain the word "perl" nor the word "indir", the program named after the #! is executed instead of the Perl interpreter.
    I found with git that "indir" support was added in a cleanup commit for Perl 5.0 in 1994 and was mentioned in perlrun in 2012. Just out of curiousity, what is "indir" referring to?
perl6 rational number problem
2 direct replies — Read more / Contribute
by freakcoco
on Nov 20, 2017 at 23:14
    Hi Monk, I love perl6 number, it use rationals number by defulat.
    But how about large number? can I use it by default?
    some code like that:
    my Rat @example =  1 , * ** 0.9999 ... Inf;
    This work fine on  @example[^4]
    But when the number going bigger , it will stored float point by default and type check fail.
    How can i fix that?



    fixed question: Addition 、 Subtraction 、Multiplication 、 Division 、 Modulo
    my Rat @ex01 = 1.0 , * + 0.9999 ... Inf; my Rat @ex02 = 1.0 , * - 0.9999 ... Inf; my Rat @ex03 = 1.0 , * * 0.9999 ... Inf; my Rat @ex04 = 1.0 , * / 0.9999 ... Inf; my Rat @ex05 = 1.0 , * % 0.9999 ... Inf; my %ex = :add(@ex01), :substrat(@ex02), :mult(@ex03), div(@ex04), mod(@ex05); try { for < add substrat mult div mod > -> $op { for 1 .. 20 -> $count { .say; (%ex{$op})[$count] } } }
    This try will fail in Multiplication and Division
    Why?
    How to fix it?
Perl, Android web app, AJAX, JSON
3 direct replies — Read more / Contribute
by monx663
on Nov 20, 2017 at 17:49
    Hi all

    This is not a 100% Perl question but I am Perl programmer and my question touches on Perl heavily. There is a fully functional web based CMS that is written in Perl for which I would like to make certain functionality available to an Android app being written. The requirements are not that "heavy".

    The Android App needs to do the following few things:

    * Send a username/password combination to gain access to the CMS backoffice.

    * Call certain URLs which will enable the app to get a list of objects (images,names, other table contents) available in the CMS's database.

    * Probably POST to certain URLs in order to be able to upload images and create content.

    My question is: What methodology/APIs should I be looking at to complete this Perl CMS/Android app "integration". I was thinking of writing an AJAX layer that would expose certain methods to the Android app, which will be calling them via HTTP requests. I was also thinking of modifying certain of the CMS's packages to perhaps implement methods for JSON based communication of textual and binary data (images). I have also heard of Mojolistic, which supposedly is intended for writing RESTful APIs but find this a bit of an overkill for my scenario.

    Does anybody want to offer any of his thoughts about which route to pursue?

Extracting embedded file from PDF
1 direct reply — Read more / Contribute
by staszeko
on Nov 20, 2017 at 17:36
    I need to extract XML file embedded in PDF. I looked into module 'CAM::PDF' by Chris Dolan, but I could not find suitable examples. I need to do the extraction as part of a larger Perl program, without calls to third party tools like 'pdfdetach' or 'pdftk'. I would much appreciate any suggestion that could help me to achieve this task. Below is fragment of dictionary, returned by method 'getRootDict()'; you can see the name of XML file referenced there:
    $VAR1 = { 'Type' => bless( { 'gennum' => 0, 'value' => 'Catalog', 'type' => 'label', 'objnum' => 83 }, 'CAM::PDF::Node' ), 'Names' => bless( { 'gennum' => 0, 'value' => { 'EmbeddedFiles' => bless( { + 'gennum' => 0, + 'value' => { + 'Names' => bless( { + 'gennum' => 0, + 'value' => [ + bless( { + 'gennum' => 0 +, + 'value' => 'Z +UGFeRD-invoice.xml', + 'type' => 'st +ring', + 'objnum' => 8 +3 + }, 'CAM::PDF::N +ode' ),
hashref with(out) arrays
8 direct replies — Read more / Contribute
by bfdi533
on Nov 20, 2017 at 17:36

    I have some XML that is being returned from a REST API and sometimes there is a single result and sometime there are multiple results.

    I am having a hard time determining how to get the data that I need and know if there is an array or not. Example:

    $item->{result}->[0]->{value} $item->{result}->[1]->{value}
    versus
    $item->{result}->{value}

    I really need to get the value if there is one or know there are more than one value so I can collect them into an array.

    What is the best practice for this sort of thing?


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.