Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
pdftk encryption function
No replies — Read more | Post response
by Anonymous Monk
on Sep 18, 2014 at 06:03

    Hi all, i'am new to Perl programming. I'was tring the functionalities to perform pdf file encryption and decryption. working on linux environment, installed PDF::Tk and corresponding binary file. Please find below the sorce code for tring to encryption (apps.pdf file).

    sorce code:

    use PDF::Tk; my $doc=PDF::Tk->new(pdftk=>'/apps/free/pdftk/1.44/bin/pdftk'); $doc->call_pdftk('apps.pdf', 'test.pdf','owner_pw','abc12345');

    getting error as below:

    Error: Unexpected command-line data: owner_pw where we were expecting an input PDF filename, operation (e.g. "cat") or "input_pw". Exiting. Errors encountered. No output created. Done. Input errors, so no output created. pdftk apps.pdf owner_pw abc12345 test.pdf failed: 256 at /usr/lib/perl5/site_perl/5.10.0/PDF/ line 73. note: created a new pdf 'apps.pdf' with Document Open Password as 'abc12345' and permission Password as 'abcd'. Please help me in resolving the above issue.

Load XML issues
2 direct replies — Read more / Contribute
by Elegant
on Sep 18, 2014 at 03:39
    use warnings; use strict; use XML::LibXML; my $mkvext = 'mkvextract'; my ($self) = {}; $self->{xml} = XML::LibXML->new(); my $item = "F:\\My Folder\\split-001.mkv"; my $cmd = "$mkvext chapters $item"; open my $H, "$cmd |"; binmode $H; my $xml = $self->{xml}->load_xml(IO => $H);
    The above is an example where I am able to reproduce the issue. I realize many people here will not be able to recreate the scenario (as you'll need MKVExtract and a MKV file with chapters) so I'll summarize. The above code is extracting an XML from the MKV then immediately loading it up with load_xml. The issue is that load_xml in this type of situation does not like that there is a space in the directory path ("My Folder"). I'm not sure how to work around the issue apart from using folders that contain no spaces. Below is the error. Any help would be greatly appreciated!
    L:> Entity: line 1: parser error : Document is empty Error: Unrecognized command line option 'Folder\split-001.mkv'. ^
Catalyst Framework - target tt2 view
1 direct reply — Read more / Contribute
by solegaonkar
on Sep 18, 2014 at 03:15
    Hi Monks! Need some help with Catalyst framework I have an application that uses Catalyst. The controller performs some checks before passing it over to the tt2. I need to skip this for some pages. But, I am not able to identify the target view tt2 in the root controller. Can you please help!
XS: how to destroy HV* that was used internally?
1 direct reply — Read more / Contribute
by OlegG
on Sep 18, 2014 at 03:10
    Suppose I have following class implemented with XS
    #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" typedef struct { HV* set; char foo; } My_Class; MODULE = My::Class PACKAGE = My::Class SV* new(char* class) PREINIT: My_Class *self; CODE: Newx(self, 1, My_Class); self->set = newHV(); self->foo = 0; RETVAL = newSV(0); sv_setref_pv(RETVAL, class, (void *)self); OUTPUT: RETVAL void DESTROY(My_Class *self) CODE: /* HOW TO DESTROY self->set HERE*/ Safefree(self);
    Where I'll use self->set internally, which type is HV*. In the constructor (new) newHV() creates HV with reference count 1. So, as I understand, to be destroyed reference count should be decreased to 0. But as I found we have only SvREFCNT_dec() which argument may be SV* and not HV*.

    So, what is a solution here?
    May be I just need to call Safefree(self->set)?
    Or get reference to self->set into SV* variable and call SvREFCNT_dec on it?
fetchrow_array loop in perl 5.10
3 direct replies — Read more / Contribute
by Anonymous Monk
on Sep 18, 2014 at 03:06
    We recently upgraded perl from 5.8 to 5.10

    we discovered after the upgrade that the fetchrow_array loop ends abruptly

    code snippet:
    my $searchSQL = $dbh->prepare($sql1);
    my $insertSQL = $dbh->prepare($sql2);

    while (my ($id,$name) = $searchSQL->fetchrow_array()) {

    if I comment out the "$insertSQL->execute($id,$name);" it loops thru all the records.

    the code perfectly works in 5.8 but not in 5.10

    any ideas?

FileParse Issue
2 direct replies — Read more / Contribute
by Elegant
on Sep 18, 2014 at 01:58
    I'm currently using the following code snippet:
    my($parent, $dir, $suffix) = fileparse($item, qr/\.[mM][kK][vV]/);
    Unfortunately, this fails every single time $item contains a dash (-), IE: "L:\My-Folder". It promptly ends giving a parser error saying the document is empty.
Qualified module variables and arrays
3 direct replies — Read more / Contribute
by DarrenSol
on Sep 17, 2014 at 21:21

    Thanks for the advice and comments on my previous question on arrays in modules. I have that working, but of course, I have another question :) I'm wondering just how necessary it is to use a qualifier on a module-declared variable or array.

    I understand (to some extent) qualifying the names of variables and arrays declared in modules, when accessed in a script. It seemed awfully cumbersome to me to use the qualified names, but I went at it.

    While making those changes to a script, I used a module variable reference to access a module array, but forgot to add the qualifier. I didn't notice the missing qualifier until going over the script to add comments. I checked through the script and module and didn't find it malfunctioning.

    So I got to thinking : would it work without the qualifier in other situations ? So far, it does.

    As a test case, I modified a short script without the qualifiers, instead using an abbreviation of the subroutine name, like this:




    So far, in testing, it works. The abbreviation-prefixed name tells me right off this isn't a local variable - don't modify it or the array it references. This type of name also takes up half the space of the qualified name. And I find it easier to read the script, without the cumbersome qualified name.

    But I know enough to know I don't know what I don't know.

    So I'm asking here: Am I letting myself in for considerable grief down the road using this naming convention for module variables and arrays ?

How to convert PHP code to Perl?
2 direct replies — Read more / Contribute
by Roxanne
on Sep 17, 2014 at 20:06

    Hi, I would like to ask how to convert this PHP code to Perl (a code that can post json file to a url). I'm newbie in Perl that's I still have trouble. Also, I can't use PHP since there is no PHP installed in our Linux environment and Perl is the only available.

    Below is my the php code

    <?php require_once ('lib/nusoap.php'); //Give it value at parameter $param = array( 'path' => '../WebServiceSOAP/data.json'); //Create object that referer a web services $client = new nusoap_client('https://host/WebServiceSOAP/server.php'); //Call a function at server and send parameters too $response = $client->call('getData',$param); //Process result if($client->fault) { echo "FAULT: <p>Code: (".$client->faultcode."</p>"; echo "String: ".$client->faultstring; } else { print($response); } ?>
perl - File::Find::Rule to exclude a single dir
4 direct replies — Read more / Contribute
by sravs448
on Sep 17, 2014 at 16:13
    Update: Updated the code to a compilable verison as suggested
    I am trying to get the list of latest files in each dir(for each project) under a specific path ($output) , excluding a single dir OLD
    How can I frame a rule to achieve this?
    use strict; use warnings; use Data::Dump; use File::Find::Rule; my $output = "/abc/def/ghi"; my @exclude_dirs = qw(OLD); my $rule = File::Find::Rule->new; $rule->or($rule->new ->file() ->name(@exclude_dirs) ->prune ->discard, $rule->new); my @files = $rule->in("$output"); dd \@files;
    Directory Structure
      |--- Project1
             |-- 2013
                     |-- foobar_2013_0812_154.txt
             |-- 2014
                     |-- foobar__2014_0912_255.txt
                     |-- foobar__2014_0916_248.txt
    |--- Project2
             |-- 2013
                     |-- fbaz_2013_0812_154.txt
             |-- 2014
                     |-- fbaz__2014_0912_255.txt
                     |-- fbaz__2014_0916_248.txt
    |--- OLD
             |-- projectX

    Current Output:
    /abc/def/ghi/Project1/ /abc/def/ghi/Project1/2013 /abc/def/ghi/Project1/2013/file1_project1.txt /abc/def/ghi/Project1/20l4 /abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt /abc/def/ghi/Project1/2014/foobar_2014_0916_248.txt /abc/def/ghi/Project2 /abc/def/ghi/Project2/2013 /abc/def/ghi/Project1/2013/file2_project1.txt /abc/def/ghi/Project2/2014 /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_255.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
    Desired Output:
    /abc/def/ghi/Project1/2014/foobar_2014_0912_255.txt /abc/def/ghi/Project2/2014/foobarbaz_2014_0912_248.txt
Adding floats
2 direct replies — Read more / Contribute
by perldiverx
on Sep 17, 2014 at 15:16
    I have a subroutine that's outputting a float correctly, however when added to a value the last return gives a Euler's number for some reason. Does anyone know why this is and how can I just get the 0 that I'm expecting? Below is my code:
    use strict; use warnings; my @vals = ( '00000000{', '00000000{', '00000000{', '00000369I', '00000020{', '00000000{', '00000100}', '00000289R',); my $total; foreach (@vals) { $total += calc_total($_); print "\$total: $total\n"; } sub calc_total { my $val = shift; my %pos_map = ('A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, ' +F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, '{' => 0); my %neg_map = ('J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5, ' +O' => 6, 'P' => 7, 'Q' => 8, 'R' => 9, '}' => 0); my $outval = substr($val, 0, 8); if (exists($pos_map{substr($val, -1, 1)})) { $outval .= $pos_map{substr($val, -1, 1)}; } elsif (exists($neg_map{substr($val, -1, 1)})) { $outval .= $neg_map{substr($val, -1, 1)}; $outval *= -1; } else { print "Could not map total for: $val\n"; } print "in the sub: " . sprintf("%.2f", $outval/100) . "\n"; return sprintf("%.2f", $outval/100); }
    I'm using Perl 5.16, too.

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: (6)
    As of 2014-09-18 10:22 GMT
    Find Nodes?
      Voting Booth?

      How do you remember the number of days in each month?

      Results (111 votes), past polls