Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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
Perl in Shell
2 direct replies — Read more / Contribute
by HarshV
on Sep 23, 2014 at 11:13

    Hey guys, I have read many threads for this question, but couldn't succeed. I have a shell script in which I am trying to call a perl script. It's just going to be one liner command. The exit code this command generates is 2. Not sure, what's this. Any help is appreciated. I had tried few ways: 1) perl path/to/the/script/ABC.pl 2) path/to/the/script/ABC.pl 3) path//to//the//script//ABC.pl

Turning on regexp debugging at runtime
1 direct reply — Read more / Contribute
by SBECK
on Sep 23, 2014 at 10:55

    When tracking down a tricky regexp related problem, it's nice to turn on regexp debugging:

    use re 'debug';

    Ideally, I would like to be able to toggle it on and off (especially in the debugger) because there may be a lot of other regexp matching going on that works perfectly well, and I want to get to the portion that is broken. Unfortunately, I haven't figured out how to do that.

    I know this comes down to me not truly understanding how pragmas work, and maybe it's not possible, but is there a way to toggle debugging during runtime?

Where is $# fully documented?
5 direct replies — Read more / Contribute
by LanX
on Sep 23, 2014 at 10:08
    Hi

    I'm struggling to find the perldocs where the usage of $# (i.e. last-index) with array-references (not symbols) is documented. Can you help me?

    If not, what's the best place to augment the perldocs?

    Tested behavior (5.10):

    DB<107> @a = (1..5) # @array => (1, 2, 3, 4, 5) DB<108> $#a # classic usage => 4 DB<109> $a = [1..10] # arrref => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] DB<110> $#$a => 9 DB<111> $a = [ map [0..$_],0..3 ] # AoA => [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]] DB<112> $#{ $a->[2] } => 2

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Error while using packaged file using pp
2 direct replies — Read more / Contribute
by m_k_venkataraman
on Sep 23, 2014 at 10:07
    I have just started learning Perl. I am developing a small quality check module for JATS XML.

    While my Perl program is giving the desired output when executed as a standalone .pl program, it is giving me error as described below, if I execute the same program as package, packaged using PAR (pp -o file.exe file.pl).

    Can you please let me know what may be the mistake I am doing in compilation? How can I avoid the error so that the packaged exe gives the same output (given below) as .pl execution.


    -----------------Error
    Couldn't load LWP based external entity handler Switching to file-based external entity handler (To avoid this message, use NoLWP option to XML::Parser) Failed to open http://cats.informa.com/tfjats/1.0/dtd/tfjats1.dtd: Invalid argument Handler couldn't resolve external entity at line 2, column 153, byte 196 error in processing external entity reference at line 2, column 153, byte 196: <?xml version="1.0" encoding="US-ASCII"?> <!DOCTYPE article PUBLIC "-//TF//DTD JATS (Z39.96) v1.0 Taylor and Francis Journ al Content v1.0//EN" "http://cats.informa.com/tfjats/1.0/dtd/tfjats1.dtd"> <article article-type="Article" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns :tf="http://cats.informa.com/tfjats" xmlns="http://jats.nlm.nih.gov" tf:schema-v ersion="1.0" xml:lang="en"> <tf:article-status stage="just-accepted"> at XML/Parser.pm line 187.
    -----------------


    ------------Output from .pl file
    Journal Title-Id are equal in both xml files
    Supplemental-Material id are equal xml file
    Supplemental-Material xlink:href and Filename Are Equal
    Content types are equal in both XML Files
    Table Counts are correct.
    Figure Counts are Correct.
    References Counts are Correct.
    ----------------


    ----------------To invoke XML I am using
    use strict;
    use warnings;
    use XML::XPath;
    use XML::Parser;
    use XML::XPath::XMLParser;
    ----------------
sorting array of array references with multiple dereferenced array elements
3 direct replies — Read more / Contribute
by onlyIDleft
on Sep 23, 2014 at 08:07
    @a1 = qw (ID1 len1 dom1 domlen1 seq1); @a2 = qw (ID2 len2 dom2 domlen2 seq2); @a3 = qw (ID3 len3 dom3 domlen3 seq3); $line1 = /@a1; $line2 = /@a2; $line3 = /@a3; @array =qw ($line1 $line2 $line3); # an array of array references.

    I want to first sort @array by element 1 of @a1, @a2, @a3 i.e. $ID , then by element 2 - $dom, then by element 3 - $domlen, by element 4 - $seq (which would be an alphabetical sort) and finally by element [0] - $ID (another alphabetical sort).

    After this sort, I want to extract the 1st element - "ID" from 1st de-referenced array, in @sorted_array

    I tried something on the lines of the suggestion at : http://www.perlmonks.org/?node_id=674374 by jettero, but get an error message - I suppose because what I am trying is to sort by elements in the deferenced array of arrays- which is not what the post here is talking about, right?

    Thank you in advance!

    PS. Sorry about the long post title

Project Help: Mechanize::Firefox - Scraping Websites with Javascript
2 direct replies — Read more / Contribute
by jdlev
on Sep 23, 2014 at 07:49
    Hi Guys, I've been really struggling with this and could use some help. It's frustrating, because I can use google's dev tools, and using jQuery, go in and pick out the variables I need. That being said, I'm open to any suggestions on how to get this done - preferably using perl, but that's not entirely necessary. Here's where I'm at.

    Based on suggestions I've rec'd, I decided to make a go with Mechanize::Firefox. So far, I've been able to get my program to open firefox and go to the correct page. The problems come when I need to execute some actions. Once it gets to the page, here's what I'd like it to do:

    From the HTML source code, I've found that there is a javascript object variable that I'd like to parse. It appears to be setup as a hex with keys. Since it renders to html, I assume I should be able to parse it and pull the data I need? Here is the code from the site:

    <script type="text/javascript"> Realtime.setPusherDelay(0); var myContests = [{"n":"Fantasy +League","a":20.0000,"id":334455"}, </script>

    So basically, how would I go in there and pull the 'n:' variable or the 'id' variable? TIA :)

    I love it when a program comes together - jdhannibal
fork, but not waiting on children
1 direct reply — Read more / Contribute
by Monk::Thomas
on Sep 23, 2014 at 06:34

    Hello fellow monks

    I'd like to write a small wrapper script which receives a command and a url as input. It then should try to run the command and if that succeeds create a child/thread to wait for successful completion of the command and finally query the URL. The main program should return as quickly as possible signaling either successful command start or complete failure (e.g. command not found). (The actual command may run for something between a couple of minutes and some hours.)

    - try to run the command
    - success?
      - spawn child: wait for completion and call URL
      - terminate with exit == 0
    - failure?
      - terminate with exit != 0
    

    It's a bit like backgrounding a process, but I want immediate feedback if there is an error.

    I've read some tutorials about forking/threads and they did warn that if I'm not waiting on forked child processes to complete then I'll create zombie processes. So before I doing something stupid: How would you implement that? Is forking the correct solution? How to avoid creating zombies?

    code is not tested / verified

    my $ch = Proc::Background->new(@command); if (defined $ch->pid()) { my $pid = fork(); if (not defined $pid) { die "Cannot fork: $!"; } elsif ($pid == 0) { # child process my $exitcode = $ch->wait() << 8; if ($exitcode == 0) { # call URL } } else { # parent process #waitpid($kidpid, 0); <- do not wait for child exit 0; } } else { # failed to execute exit 127; }

    Target architecture is Linux, no need to care about Windows/UNIX pecularities.


    Update:

    Trying to get the backgrounding to work properly (stripped URL callback, no zombie-handling)

    #!/usr/bin/env perl use strict; use warnings; use 5.010_001; # essential libraries use English qw( -no_match_vars ); use Proc::Background; my @cmd = qw(sleep 62); my $cmd_str = join ' ', @cmd; say("Executing command '$cmd_str' in background."); my $ph = Proc::Background->new(@cmd); my $exitcode; if (defined $ph) { my $pid = fork(); if (not defined $pid) { # fork failed die "Cannot fork: $ERRNO\n"; } elsif ($pid == 0) { # child process say("[child] Process PID is " . $ph->pid() ); say("[child] Command is alive? " . $ph->alive() ); $exitcode = $ph->wait() << 8; my $elapsed = $ph->end_time() - $ph->start_time(); say("[child] Command '$cmd_str' completed after $elapsed secon +ds"); # conditionally perform URL callback } else { # parent process say("[parent] Process PID is " . $ph->pid() ); say("[parent] Command is alive? " . $ph->alive() ); $exitcode = 0; } } else { # failed to execute $exitcode = 127; } exit $exitcode;

    Result:

    % perl /tmp/scratch.pl
    Executing command 'sleep 62' in background.
    parent Process PID is 36296
    parent Command is alive? 1
    child Process PID is 36296
    child Command is alive? 
    child Command 'sleep 62' completed after 0 seconds
    

    Something fishy is going on. The child is able to determine the correct PID, but $ph->alive() returns nothing and the child terminates immediately. Hmph.

    parent
Encode - List ALL available encodings [SOLVED]
1 direct reply — Read more / Contribute
by kzwix
on Sep 23, 2014 at 04:15

    Greetings, O wise ones.

    I would like to get a list of all the available encodings installed on my machine. The Encode documentation (on the Perl website) clearly states:

    To get a list of all available encodings including those that have not yet been loaded, say:
    
        @all_encodings = Encode->encodings(":all");
    

    Now, executing this command and displaying the list contents, one result per row, gives me the following result:

    ascii
    ascii-ctrl
    iso-8859-1
    null
    utf-8-strict
    utf8
    

    However, when I type, in the very same program:

    my $canon = resolve_alias('Latin-9');
    print $canon ? "Found Alias for: $canon\n" : "Unknown\n";

    I get the result:

    Found Alias for: ISO-8859-15
    

    Now, this leaves me puzzled: The canonical name clearly wasn't stated in the list which was supposed to give me all available encodings... I have the same problem with other encodings, too.

    Would you know what I do wrong ?

Ant XML
1 direct reply — Read more / Contribute
by Ashwitha
on Sep 23, 2014 at 01:00
    Hi all, I need to know is there a way to generate Ant XML file using perl script.
Perl nested loop to print out two arrays n number of times in different patterns
3 direct replies — Read more / Contribute
by arty32l
on Sep 22, 2014 at 20:26

    I am looking to create a loop where the following data is printed n number of times. So given:"

    n = 9; @a = (1, 2 ,3); @b = (a, b, c);
    $a[0] = 1 $b[0] = a $a[0] = 1 $b[1] = b $a[1] = 2 $b[2] = c $a[1] = 2 $b[0] = a $a[2] = 3 $b[1] = b $a[2] = 3 $b[2] = c $a[0] = 1 $b[0] = a $a[0] = 1 $b[1] = b $a[1] = 2 $b[2] = c

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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2014-09-24 05:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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











      Results (246 votes), past polls