Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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
New version of Mod_perl
2 direct replies — Read more / Contribute
by adamarc
on Jan 29, 2015 at 11:02
    Dear respected Perl monks,

    I have a mod-perl site that I want to continue using, but my distro doesn't distribute the mod-perl package anymore because there is none available that works with Apache 2.4. It turns out that the loyal mod_perl development team cannot release a compatible version due to a lack of testers.

    Can somebody help by assisting them in submitting a bug report?
Module to get domain name from hostname
1 direct reply — Read more / Contribute
by gatkins
on Jan 29, 2015 at 10:47
    Hello, dear monks,
    I'm searching for a module which can find a domain name starting from a hostname. Example:
 >> >> >> >>
    It would be easy if the TLD was just .com / .net / .org. This can be done just by cutting the hostname. The problem is when the TLD is or or for example.
    If you know any module which can do this, please share.
Find nonnumeric scalars
4 direct replies — Read more / Contribute
by urbs33
on Jan 29, 2015 at 09:59

    Guys I am working with millions of lines of data that "should" all be in the same format, however I am finding hundreds of lines that are not. A line may have ~10 fields space delimited. Suppose I am splitting the line into scalars that I can work with, and perform math on. Suppose fields 6-8 are supposed to be numeric and available for math. I am getting non-numeric warnings on several of them and just want to write the line out to an "errors" file so that I can resolve the formatting.

    How can I do something to test if scalar 6, 7, or 8 is not numeric (or even empty), write the line to a file and move to the next line. I can handle the "else". I'd prefer to use standard perl as it is very difficult at my company to pull in additional packages.

How can one create an array of the indices at which a given character appears in a string?
4 direct replies — Read more / Contribute
by tkguifan
on Jan 29, 2015 at 07:50
    The naive solution:
    my $input='rnbqkbnr'; my $search='n'; my $index=0; my @result; foreach(split //,$input) { if($_ eq $search) { push(@result,$index); } $index++; } print "Result: ",join(' , ',@result);
    Is there a shortcut to do it more simply?
Rendering POD to (not with!) templated files
1 direct reply — Read more / Contribute
by pokki
on Jan 29, 2015 at 05:54

    Esteemed monks,

    I wrote a small darkpan browser for $work and I have the following issue...

    The app is built as a Dancer web app. One of the features is that module PODs (ours, or dependencies from CPAN's) should be rendered to HTML and displayed in a page. I use Pod::Simple::XHTML to do the heavy lifting here. Since I also want the app to be relocatable without breaking all the inter-POD links, I have done this:

    my $pod_renderer = Pod::Simple::XHTML->new; # this is going to be inserted in a larger document $pod_renderer->html_header('[% TAGS [- -] %]'); $pod_renderer->html_footer(''); # http://localhost:5000/mirror/integration/module/Acme::Foo $pod_renderer->perldoc_url_prefix('[- request.uri_base -]/mirror/[ +- selected_mirror -]/module/'); $pod_renderer->output_string(\my $html); $pod_renderer->parse_file($module); # spew $html into a file on disk

    (ignore the [- selected_mirror -] part, that's just because we have multiple Pinto instances and the app can generate links e.g. from the integration to preproduction releases of a module)

    When rendering the full module page, I have a total of three (!) template rendering passes: once to turn the POD-rendered-as-template into HTML; once to render the regular .tt file, which includes the previously rendered HTML directly; and finally once because the .tt file has a layout and Dancer's template engine works like this.

    The first pass (POD to .tt) uses custom TAGS because otherwise the PODs would not render properly (they of course include lots of [% %] everywhere). [- -] turned out to be a very bad idea (USAGE: foobar [-optional]) and I need to fix this.

    The second and third pass are just Dancer's standard template to HTML rendering. The module page has other things besides the rendered POD, so it needs to be its own template that somehow includes the templated POD...

    I feel like the way I wrote it is now a cluster[beep] of badly thought-out fixes upon badly thought-out fixes. Have you guys done something like this? How did you do it? Alternatively, do you see a simpler/better solution?

Use of uninitialized value $selector in split at /usr/local/share/perl/5.18.2/Net/OpenSSH/ line 141.
2 direct replies — Read more / Contribute
by thanos1983
on Jan 28, 2015 at 21:27

    Hello Monks,

    I am getting this error from a script that I have created. Initially I script with many subroutines inside, and I decide to split it on *.pm files so it can be easier readable/understandable.

    Since I tried to modify it, although that I am at the final steps of my script I am getting this error:

    Use of uninitialized value $selector in split at /usr/local/share/perl +/5.18.2/Net/OpenSSH/ line 141.

    I opened the Net::OpenSSH::Parallel module at the specific line:

    my @parts = split /\s*,\s*/, $selector;

    My script is pasted underneath but I do not know if it can provide much of assistance:

    In case you need me to post all my modules please feel free to ask me to.

    Update: Adding all modules.

    The conf.ini file:

    The directories.ini file:

    Thanks in advance everyone for their time and effort to assist me.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
[SOAP::Lite] what happens when I call $client->call('Method', \%params)?
1 direct reply — Read more / Contribute
by three18ti
on Jan 28, 2015 at 20:53

    Hello Monks

    This is a continuation of last nights question [SOLVED][SOAP::Lite] Obtain request body before request is sent? but I think I need to step back and understand what's going on before moving forward.

    First of all, I think what I'm really after is the request envelope. I'd like to be able to print or otherwise store in a variable the XML markup in the envelope. Second of all, I need to modify the HTTP request header (not the XML header as I proposed in the above linked thread). I had my terms mixed up last night but I think I'm starting to understand better.

    Anyway. What I'm really after is what happens when you call $client->my_api_method(\%params); where is the XML actually built?

    Here is the code I'm working with

    <#!/usr/bin/env perl use strict; use warnings; use 5.010; use LWP::UserAgent; use SOAP::Lite; use LWP::Debug; LWP::Debug::level('+'); SOAP::Lite->import(+trace => ' +all'); my $client = SOAP::Lite->proxy($proxy) ->ns($namespace, 'foo') # I'm not sure I understand this. ->uri($uri) ->on_action(sub { sprintf '%s', $_[0] }) ->on_fault(sub { my($soap, $result) = @_; die ref $result ? "Fault Code: " . $result->faultcode . "\n" . "Fault String: " . $result->faultstring . "\n" : $soap->transport->status, "\n"; }); my $params = { foo => 'bar', biz => 'baz'}; #my $data = SOAP::Data->name($params); #my $serial = $client->serializer; #my $xml = $serial->envelope($data); #print Dumper $xml; #my $result = $client->my_api_method($params); my $result = $client->call('my_api_method' => $params);

    I've figured out that $client->my_api_method(\%params) is the same as $client->call('my_api_method' => \%params)

    When running with trace, it looks like SOAP::Serializer is getting a hash, but when I call $serial->envelope($data) I get an error that it's "Wrong type of envelope (SOAP::Data=HASH(0x8feb78)) for SOAP call". (Of course all the docs point to the SOAP::Serialize docs say that it's used by SOAP::Lite and all the SOAP ::Lite docs say to see SOAP::Serialize... :/ I've looked at the code for SOAP::Serializer, I'm not convinced it's what's building the XML.)

    What modules used by SOAP::Lite actually generate the XML here? I can generate a SOAP::Data hash, but then what do I do with it? I know I can pass it to $client->call so something behind that is generating the xml.

    Thanks for the assistance.

Create a hash for each unique captured regex variable
5 direct replies — Read more / Contribute
by hkates
on Jan 28, 2015 at 19:11

    Here is my input:

    foo_1-a foo_2-b foo_3-b foo_4-b bar_1-a bar_2-a bar_3-b bar_4-a bar_5-b

    And my desired output:

    foo 4 foo_1-a foo_2-b foo_3-b foo_4-b bar 5 bar_1-a bar_2-a bar_3-b bar_4-a bar_5_b

    I wish that I had code to show you, but I don't know where to start and am thinking perl may not be the best thing for the job. I want to build a hash for each foo or bar, where foo/bar are the values and the hash keys are the full word. e.g.:

    %hash_bar =() bar_1-a => bar bar_2-a => bar bar_3-b => bar bar_4-a => bar bar_5-b => bar %hash_foo =() foo_1-a => foo foo_2-b => foo foo_3-b => foo foo_4-b => foo

    Then I want to print any value in the hash (since they are all the same) followed by the number of keys in the hash, followed by the keys for each hash

    It's a stretch to call this pseudo code, but just to clarify my question:

    open FH, "<file.txt"; while (<FH>) { if (/((\S+)_\S+-\S+)) { #for each unique $1; %hash_$1 =(); # populate hash with keys $2 and values $1 $hash_$1($2)=$1; } }

    I'm not expecting anyone to do this for me, but any direction to the function needed for this would be much appreciated.

    I know that I wouldn't be able to create the hashes in that if statement. I would need to create a hash of all the unique $1 first (so that I could use the exists function) and then for each key in that hash, read through the file again, creating a new hash for each key in the original hash.

    But that seems very inelegant, and I didn't know how to even write the pseudo code. Am I just totally on the wrong track?


hash symbolic references
2 direct replies — Read more / Contribute
by numele
on Jan 28, 2015 at 18:46

    I have a piece of code that works, but when I add use strict I get an error "Can't use string ("0") as a HASH ref while "strict refs" in use"

    I comment out use strict and its fine.

    #!/usr/local/bin/perl use strict; #use warnings; my %input; $input{"1"} = 0; $input{"2"} = 1; $input{"3"}->{a} = "ah"; $input{"3"}->{b} = "bee"; $input{"4"} = 'string'; jscript(%input); sub jscript { my %input = @_; my $total = scalar(keys(%input)); my $subtotal = $total--; my $cnt = 0; my $count = 0; my ($j,$NEST); print "Javascript literal\n\[ "; foreach my $number (sort keys %input) { $count++; unless ($input{$number} =~ /HASH/) { if ($count < $total) { print "\"$input{$number}\"\, "; } else { print "\"$input{$number}\""; } } foreach my $subject (keys %{ $input{$number} }) { if ($cnt == 0 ) { $j = '"'; } if ($cnt > 0 ) { $j = ', "';} $NEST .= "${j}${subject}\" : \"$input{$number}{$subject}\" +"; $cnt++; } if ($cnt > 0 && $count < $subtotal) { print "\{ $NEST \}\, "; $cnt = 0; $total++; $NEST = ""; } elsif ($cnt > 0 && $count == $subtotal) { print "\{ $NEST \} "; $cnt = 0; $NEST = ""; } } print " \]\n"; }

    Script output without strict, which is perfect

    Javascript literal [ "0", "1", { "a" : "ah", "b" : "bee" }, "string" ]

    Script output with strict

    Javascript literal Can't use string ("0") as a HASH ref while "strict refs" in use at ./r line 37. [ "0",
Converting tesseract box data into 2d grid
1 direct reply — Read more / Contribute
by Anonymous Monk
on Jan 28, 2015 at 18:43
    I'm using Tesseract to OCR images to read puzzle data. The problem is that Tesseract squashes whitespace so messes with the positions of the found characters. It does have an option to output the bounding box coordinates for each of the characters, but I'm not sure how to convert that back to a row/column to produce the original puzzle positions. An example input image: and the resulting box data is:

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 contemplating the Monastery: (6)
    As of 2015-01-30 06:48 GMT
    Find Nodes?
      Voting Booth?

      My top resolution in 2015 is:

      Results (248 votes), past polls