Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Pathologically Eclectic Rubbish Lister

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
HTML static output
4 direct replies — Read more / Contribute
by unixdisk
on Apr 16, 2014 at 02:45

    I collect data from 'n' servers and redirect the output to TXT file & email my Manager however now requriment is to attach the output in HTML as it's easy to read rather than TXT file.

    Please let me know how to store the data in html & send it via email. Thanks.

How to get line ranges of subroutines from Perl source code
3 direct replies — Read more / Contribute
by rockyb
on Apr 16, 2014 at 01:42

    I'd like to get the start and end ranges of subroutines in a file much the same way that Perl does for the %DB:sub hash when debugging is turned on.

    I can approximate this using B:Utils::all_starts by looking for minimum and maximum COP lines, but that really isn't the same thing since the first executable line might not be on the same line as the sub declaration. For example:

      sub five {
      sub six { 6; }

    Note that the start and end lines of five() are one less and one greater than the single returned expression, 5; For six() though everything is on the same line.

Multiline string and one line comments
3 direct replies — Read more / Contribute
by AskandLearn
on Apr 16, 2014 at 01:17
    Hi everyone

    Another Perl regex question to match comments and strings

    Requirement/regex spec/what should be matched and what should not

    double or single quotes strings are not strings if inside comments

    # inside strings are not comments

    Here is an example, strings and comments needs to be captured and HTML style tags will added to highlight them later

    # this is a comment, should be matched. # "I am not a string" . 'because I am inside a comment' my $string = " #I am not a comment, because I am quoted"; my $another_string = "I am a multiline string with # on each line #, have fun!";

    I tried few things, could not work out a solution to cover all the situations.

better way to escape escapes
5 direct replies — Read more / Contribute
by RonW
on Apr 15, 2014 at 19:48
    I am processing an input format that can use either @ or \ to introduce inline directives. when a literal @ or \ is in the input, either character can be used to escape either character. IE: @@ or \@ or \\ or @\

    Currently, I replace the escape sequences with place holders, then extract directives, then replace the placeholders with the intended literal occurrences of @ and \ in the string.

    s/(?<![\\\@])[\\\@]\@/\x11/g; s/(?<![\\\@])[\\\@]\\/\x12/g; while (/[\\\@]([_A-Za-z]+)/) { print "Extracted code '$1'\n"; s/[\\\@]$1//; } s/\x11/\@/g; s/\x12/\\/g;

    I'm sure there's a better way, but my search-foo is lacking. And so is my regex-foo. (And there's likely an input file that will break this.)

    (and no, it's not LaTeX, despite the similarities)

Perl Directory file extension stats.
3 direct replies — Read more / Contribute
by sidsinha
on Apr 15, 2014 at 17:32

    I had a question regarding Perl directory statistics. Are there any perl packages that I can use to list the number of unique file extentions in a given directory?

    For ex, it should parse through an input directory (including subdir) and return the number of unique file types (for ex, 200 .bmp , 50 jpg ,20 txt) etc.

    Please guide me . thanks.
Lingua: Transform "wasn't" into "was not"
1 direct reply — Read more / Contribute
by Anonymous Monk
on Apr 15, 2014 at 14:09


    As the title suggests, I'm looking for a module which converts various English shortcuts like we're into we are, I'm into I am, etc...

    I couldn't find any module in CPAN which does this. Maybe I haven't looked for the right name.
    So my question is: did anyone worked with something similar before? What module would you recommend for doing this?


Optimizing a naive clustering algorithm
3 direct replies — Read more / Contribute
by BUU
on Apr 15, 2014 at 12:09

    I'm attempting to implement a basic hierarchical agglomerative clustering algorithm to be used to create an arbitrary number of clusters from an existing dataset. More reading about the general concept can be found at at or your friendly neighborhood google.

    A word about the dataset.

    My data consists of some 1500-5000 "items" each of which contains a set of "words". These words are 5-30 character strings. Each set of words contains no duplicates. There are between 5-100 "words" in a set.

    Some words about the existing code.

    The theoretical complexity of such an algorithm is something like O(cn2d2) but I suspect my implementation is considerably worse since I ran it for over 11 hours and it only managed to consolidate 500 of the 1600 items.

    The "merge" function is obviously very silly, I wrote it without thinking very hard and it doesn't do much. On the other hand I don't think it impacts the performance.

    The vast majority of the time spent is going to be in the max_diff function, which appears to get exponentially slower as the program continues to run.

    The datastructure being produced is necessary, that is it should be a binary tree made of array-refs where each leaf is either another tree or an actual item. (Its necessary because we don't know how many clusters we want to produce).

    Suggestions for optimizations or even different algorithms gratefully received.

Perl formatting beginner
5 direct replies — Read more / Contribute
by Anonymous Monk
on Apr 15, 2014 at 11:23

    Hi all i am new to perl just trying to figure out how I would format my output to look a certain way if possible. I want it be in 3 columns 10 rows Avery 5160 format, but without the use of PostScript. but right now it only show all in one column all the way down.

    #!/usr/bin/perl # Program name: # Purpose: Open disk file. Read and display the records in # the file. Count the number of records in the file. format LABEL = @<<<<<<<<<<<<<<<< $CUSTID @<<<<<<<<<<<<<<<< $fname @<<<<<<<<<<<<<<<< $lname @<<<<<<<<<<<<<<<< $phone @<<<<<<<<<<<<<<<< $address @<<<<<<<<<<<<<<<< $city @<<<<<<<<<<<<<<<< $state @<<<<<<<<<<<<<<<< $zip @<<<<<<<<<<<<<<<< $email . #$topmargin = 0.60; #$poleft = 0.4; $pomiddle = 3.20; $poright = 5.95; $lheight = 1; $row = int($opt{r}) || 1; # chop off any fractional parts and $col = int($opt{c}) || 1; if ($col == 1) { $po = $poleft; } elsif ($col == 2) { $po = $pomiddle; } else { $po = $poright; } # $sp = ($topmargin + ($row - 1)*$lheight); # required terminator for format definition open (FILEIN, "cust.txt") || warn "Could not open passwd file\n"; open (LABEL, ">labels-to-print") || warn "Can't create labels file\n"; while (<FILEIN>) { #print "$_"; ($CUSTID,$fname,$lname,$phone,$address,$city,$state,$zip,$email) += split(/,/,$_); # Or use array: @fields = split(/,/,$_); write(LABEL); # send to output ++$line_count; } print ("File \"passwd\" has $line_count lines. \n"); close (FILEIN); close (LABEL);
Extracting a chapter from text file
5 direct replies — Read more / Contribute
by jwkuo87
on Apr 15, 2014 at 10:28
    Hi everyone. I'm fairly new to Perl and am trying to extract a specific chapter from a text file. In the example below, I would like to retrieve the text from Chapter 2.
    Table of Contents Chapter 1. Introduction Chapter 2. Main Chapter 3. Conclusion ============================== Chapter 1. Introduction This is the introduction preceding Chapter 2. Chapter 2. Main This is the text contained in Chapter 2 and will contain a lot of text + with at least 100 words and probably somewhere around 1000-5000. Chapter 3. Conclusion This is the conclusion.
    The Perl script should extract "This is the text contained in Chapter 2 and will contain a lot of text with at least 100 words and probably somewhere around 1000-5000." from the file and write the output to a new file. Unfortunately, the code below only gives me the first matches, i.e. the text from the table of contents.
    #!/usr/bin/perl -w #use strict; my $startstring='Chapter\s2\.\sMain'; my $endstring='Chapter\s3\.\sConclusion'; { local $/; open (SLURP, "C:\\Text\\1.txt") or die $!; $data = <SLURP>; close SLURP or die $!; { @finds=$data=~m/($startstring.*?$endstring)/ismo; } open my $OUTFILE, ">", "C:\\Text\\Chapter2\\1.txt" or die $!; print $OUTFILE "@finds"; close $OUTFILE; }
    Is there a way to refine my search function so it works as I would like it to? Like including a rule where the startstring must be skipped if the preceding 5 strings contains "Chapter 1. Introduction" and/or the output should contain at least 100 words? Thanks in advance! :)
Delay running Perl execs in Windows
4 direct replies — Read more / Contribute
by Raineer
on Apr 15, 2014 at 10:19

    Hello all,

    I use Perl to write trace and log parsers for my engineering group. They are a Windows crowd and most are not savvy enough to use native Perl.

    I use `pp` to package the scripts into executables which they can run on Windows, and I've had great success doing so.

    My question is - Why do freshly-packed execs have such a long delay the first time they are run in Windows on a given machine? I even see this on my own machine - I pack with `pp`, then launching the file gives me a black screen hang for 30-60 seconds and the program proceeds as desired. Subsequent launches have no delay at all.

    My colleagues see this same behavior as well. It's a nuisance at worst, but I would really like to eliminate this. I can see the behavior on the simplest of programs, at most my typical programs only import Archive::Tar.

    Are there further settings I can use in `pp` to eliminate this delay? I have searched and searched but haven't found a report that matches this behavior, though I can't believe I'm the only one.

    I'm using Strawberry 5.16, but I have seen the same behavior on every distro and version I have used. Thank you.

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 taking refuge in the Monastery: (12)
    As of 2014-04-16 20:16 GMT
    Find Nodes?
      Voting Booth?

      April first is:

      Results (433 votes), past polls