Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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
CAM::PDF cleanoutput Error
2 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 10:51

    when I save a PDF document with this code:

    print "open the original document \n"; my $pdf1 = CAM::PDF->new($doc1); print "save it to the new location \n"; $pdf1->output($newdoc1);

    I get no errors. However if I use this code:

    print "open the original document \n"; my $pdf1 = CAM::PDF->new($doc1); print "save it to the new location \n"; $pdf1->cleanoutput($newdoc1);

    I get this error:

    Bad request for object 1121 at position 0 in the file Use of uninitialized value in string eq at C:/Dwimperl/perl/site/lib/C +AM/ line 4898. Not a ref at C:/Dwimperl/perl/site/lib/CAM/ line 5273.

    In case it is helpful, the getPrefs method returns this list

    [ [0] undef, [1] undef, [2] 1, [3] 1, [4] 1, [5] 1 ]

    Doc1 is a PDF document that opens OK in Adobe reader and professional.

File size discrepancy
2 direct replies — Read more / Contribute
by wrkrbeee
on Nov 25, 2014 at 10:47
    Hi everyone, I downloaded a file from the SEC.GOV website, then used Perl's test operator "-s" to determine the size of the file, and received a result of 6,324,458 bytes. The same file is posted on the SEC's website with a size of 6,255,650 bytes. Why is my file size slightly larger? Tags? Other formatting? You can see the SEC's file at: Thanks!! Rick
Can't install Text::CSV:Encode in strawberry perl
1 direct reply — Read more / Contribute
by Skeeve
on Nov 25, 2014 at 08:59
    Hi! I'm trying to get Text::CSV::Encoded installed on strawberry perl but it fails and I have no clue how to fix it. Can anyone please have a look and give a hint?
    CPAN: CPAN::SQLite loaded ok (v0.204) Database was generated on Tue, 25 Nov 2014 13:16:47 GMT Running install for module 'Text::CSV::Encoded' CPAN: Digest::SHA loaded ok (v5.92) CPAN: Compress::Zlib loaded ok (v2.064) Checksum for C:\Users\skeeve\Desktop\Strawberry\cpan\sources\authors\i +d\M\MA\MAKAMAKA\Text-CSV-Encoded-0.22.tar.gz ok CPAN: Archive::Tar loaded ok (v2.02) CPAN: File::Temp loaded ok (v0.2304) CPAN: YAML::XS loaded ok (v0.52) CPAN: Parse::CPAN::Meta loaded ok (v1.4414) CPAN: CPAN::Meta loaded ok (v2.142060) CPAN: Module::CoreList loaded ok (v5.20140914) Configuring M/MA/MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz with Makefile.P +L Checking if your kit is complete... Looks good Generating a dmake-style Makefile Writing Makefile for Text::CSV::Encoded Writing MYMETA.yml and MYMETA.json MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\perl\bin\perl.exe Makefile.PL -- +OK Running make for M/MA/MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz cp lib/Text/CSV/Encoded/Coder/ blib\lib\Text\CSV\Encoded\Coder\ cp lib/Text/CSV/Encoded/Coder/ blib\lib\Text\CSV\Encoded\Code +r\ cp lib/Text/CSV/ blib\lib\Text\CSV\ cp lib/Text/CSV/Encoded/Coder/ blib\lib\Text\CSV\Encoded +\Coder\ MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\c\bin\dmake.exe -- OK Running make test C:\Users\skeeve\Desktop\Strawberry\perl\bin\perl.exe "-MExtUtils::Comm +and::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test +_harness(0, 'blib\lib', 'blib\arch')" t/*.t t/00_pod.t ................... skipped: Test::Pod 1.00 required for te +sting POD t/01_use.t ................... ok t/02_undef.t ................. ok t/03_attr_err.t .............. ok t/pp_01_base.t ............... ok t/pp_02_code.t ............... ok t/pp_03_file.t ............... 1/20 Cannot decode string with wide cha +racters at C:/Users/skeeve/Desktop/Strawberry/perl/site/lib/ + line 241, <$fh2> line 2. Compilation failed in require at t/pp_03_file.t line 14. # Looks like you planned 20 tests but ran 12. # Looks like your test exited with 25 just after 12. t/pp_03_file.t ............... Dubious, test returned 25 (wstat 6400, +0x1900) Failed 8/20 subtests t/pp_04_uni_but_utf8.t ....... ok t/pp_05_guess.t .............. ok t/pp_06_coder_class.t ........ ok t/pp_07_coder_check_value.t .. ok t/xs_01_base.t ............... ok t/xs_02_code.t ............... ok t/xs_03_file.t ............... 1/20 Cannot decode string with wide cha +racters at C:/Users/skeeve/Desktop/Strawberry/perl/site/lib/ + line 241, <$fh2> line 2. Compilation failed in require at t/xs_03_file.t line 16. # Looks like you planned 20 tests but ran 12. # Looks like your test exited with 25 just after 12. t/xs_03_file.t ............... Dubious, test returned 25 (wstat 6400, +0x1900) Failed 8/20 subtests Test Summary Report ------------------- t/pp_03_file.t (Wstat: 6400 Tests: 12 Failed: 0) Non-zero exit status: 25 Parse errors: Bad plan. You planned 20 tests but ran 12. t/xs_03_file.t (Wstat: 6400 Tests: 12 Failed: 0) Non-zero exit status: 25 Parse errors: Bad plan. You planned 20 tests but ran 12. Files=14, Tests=235, 3 wallclock secs ( 0.08 usr + 0.03 sys = 0.11 +CPU) Result: FAIL Failed 2/14 test programs. 0/235 subtests failed. dmake.exe: Error code 153, while making 'test_dynamic' MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz C:\Users\skeeve\Desktop\Strawberry\c\bin\dmake.exe test -- NOT OK //hint// to see the cpan-testers results for installing this module, t +ry: reports MAKAMAKA/Text-CSV-Encoded-0.22.tar.gz Stopping: 'install' failed for 'Text::CSV::Encoded'.
    I'm currently using Strawberry Perl Sep 2014 / / 32bit P.S. I also opened an issue on github and just now noticed there are already replies

Perl structure to Javacript-ready JSON
4 direct replies — Read more / Contribute
by Jeppe
on Nov 25, 2014 at 08:21
    Esteemed monks, I'm essentially trying to escape the contents of a JSON structure.
    my $hashref = { abc => "abc'abc" };
    This should become
    , and it must work for blessed references as well. I'm currently using the JSON module, but I'm not married to it. Ideally, this would have been resolved by a hook in the JSON module - but that does not seem to be available. What other options do I have?
Avoid recursive replacement using regex
4 direct replies — Read more / Contribute
by anirudhkumar_r
on Nov 25, 2014 at 06:21

    Hi Monks,
    How are you doing today? I am in a little trouble and need your help with regex.
    I am replacing some strings in JavaScript. However, I believe the same regex will work for both JavaScript and Perl.

    With my method of replacing, I am getting into the problem of recursive replacement.
    I want to do the following.
    1. Replace &amp; with &
    2. Replace &amp;lt; with &lt;
    3. Replace &amp;gt; with &gt;
    4. Replace &lt; with <
    5. Replace &gt; with >

    I tried to include the following code snippets in my JS code.

    new_json_string = new_json_string.replace(/&amp;/g, '&'); new_json_string = new_json_string.replace(/&lt;/g, '<'); new_json_string = new_json_string.replace(/&gt;/g, '>');
    But the problem with the above code snippet is, both text segments like [&lt;] and [&amp;lt;] will be replaced ultimately with [<] symbol. Is it possible to include all the three replacements in a single statement to avoid recursive replacement?
    Your help is highly appreciated, Thank you very much in advance.

Regex hyphen
4 direct replies — Read more / Contribute
by Anonymous Monk
on Nov 25, 2014 at 05:31

    Dear Monks

    I need to match entire words in strings. Words may or may not include an hyphen. Why is the following not working?

    $mystring =~/\b($query)\b/i
session problem
2 direct replies — Read more / Contribute
by bigup401
on Nov 25, 2014 at 04:53

    i have this in my

    $id = $session->id(); $session->param('logged_in', $username); $session->expire('+10m'); print "Location:$id\n\n";

    i have this in my

    $sid = $cgi->param('sid'); $session = CGI::Session->load($sid); if($session->is_expired) { print $cgi->redirect(""); print $cgi->header(); } elsif($session->is_empty) { print $cgi->redirect(""); print $cgi->header(); }

    both and works very well but when i try to place same session code to other page inside, it just redirects me to login, it doesn't read session sid like eg like if i place session code to when i click to, it just redirects me to login and the session is still valid not expired

Executing perl program from another perl program and capturing the output
4 direct replies — Read more / Contribute
by rgren925
on Nov 24, 2014 at 21:01
    Hi Monks.

    I have some code I'm trying to port from *nix to Windows and am stumbling over Windows long names. I've scoured the web and see many references to this, but none have helped.

    Here's what I'm trying to do...

    my $progPath = cwd(); my $perlPath = $^X; my $args = "-c abc.conf -r"; # then build the command and capture the output... my $output = `$perlPath $progPath/ $args`;
    Both the perl and program paths are in "Program Files", so it's tripping over the spaces.

    I need to capture the output. I've tried backticks, capture(), qx{}. I've tried many permutations of quotes, escaped quotes, q{}, qq{}, etc. with no luck.

    I know I could make it work by converting the paths to Windows 8.3 names.

    I'm asking:

    1) is it wise to just go ahead and use the 8.3 name and be done with it?

    2) is there a way to make this work without doing that and is that the wiser route?

    Thanks, Rick

multiline in while loop and regular expression
3 direct replies — Read more / Contribute
by newtoperlprog
on Nov 24, 2014 at 16:21

    Dear All,

    I am trying to parse a file in a while loop and printing some matched regular expression parameters.

    Below is my code and data file

    my $filename = test.summary"; open (IN, "<", $filename) or die "Check the summary file. $!\n"; while (my $line = <IN>) { chomp $line; if ($line =~/^LOCUS\s+\w+\d+\s+(\d+)\sbp/) { $gene_length = $1; } if ($line =~/^DEFINITION\s+(.*)/s) { $definition = $1; } if ($line =~/^ACCESSION\s+(.*?)\s+/) { $accession = $1; } if ($line =~ /\s+\/db_xref="GI\:(\d+)\"/) { $gi_number = $1; } if ($line =~ /\s+\/db_xref=\"GeneID\:(\d+)\"/) { $gene_id = $1; } }
    Data file: LOCUS NM_001098209 3415 bp mRNA linear PRI 27 +-APR-2014 DEFINITION Homo sapiens catenin (cadherin-associated protein), beta 1 +, 88kDa (CTNNB1), transcript variant 2, mRNA. ACCESSION NM_001098209 XM_001133660 XM_001133664 XM_001133673 XM_001 +133675 VERSION NM_001098209.1 GI:148233337 KEYWORDS RefSeq. SOURCE Homo sapiens (human) ORGANISM Homo sapiens Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Eutele +ostomi; Mammalia; Eutheria; Euarchontoglires; Primates; Haplorrhin +i; Catarrhini; Hominidae; Homo. CDS 269..2614 /gene="CTNNB1" /gene_synonym="armadillo; CTNNB; MRD19" /codon_start=1 /product="catenin beta-1" /protein_id="NP_001091679.1" /db_xref="GI:148233338" /db_xref="CCDS:CCDS2694.1" /db_xref="GeneID:1499" /db_xref="HGNC:HGNC:2514" /db_xref="MIM:116806" /translation="MATQADLMELDMAMEPDRKAAVSHWQQQSYLDSGI +HSGATTTAP SLSGKGNPEEEDVDTSQVLYEWEQGFSQSFTQEQVADIDGQYAMTRAQR +VRAAMFPET LDEGMQIPSTQFDAAHPTNVQRLAEPSQMLKHAVVNLINYQDDAELATR +AIPELTKLL //

    My questions:

    a) How can I parse the multiline DEFINITION in the while loop as the regular expression captures only the first line .

    b) Could I get some help in capuring the content of CDS block and then parse individual entries one by one( like GI, GeneID etc.).

    I am trying to learn using Perl only so I am not using the BioPerl module for the above purpose.

Test::Trap + Log::Dispatch under Cron
1 direct reply — Read more / Contribute
by Solo
on Nov 24, 2014 at 13:02

    I have a bunch of utility scripts I'd like to test in part by scraping their log output. The scripts use Log::Dispatch with the default screen output and a file output for logging. Presently, the tests use Test::Trap to check log output on STDOUT.

    This approach works well when proving tests directly from the command line. However, we run automated tests nightly via cron and under these conditions either Dispatch::Log doesn't write log entries (likely because STDOUT goes nowhere) or Test::Trap doesn't get them. I see the same results when running the tests with nohup prove ... --html

    I'd rather not completely change the test approach (to reading the file, for example, instead of using Test::Trap) just because the logging tests fail under cron. On the other hand, we rely on the nightly test output to alert us to trouble and don't want a bunch of "known" failing results.

    Supposing I can detect when running under cron (probably with ENV injection in the cron script that launches the tests--I've found checking PS1 is not sufficient to know) is there some UNIX or Perl hack I can use to persuade Log::Dispatch to log such that Test::Trap gets the lines?

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2014-11-26 17:40 GMT
    Find Nodes?
      Voting Booth?

      My preferred Perl binaries come from:

      Results (172 votes), past polls