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
Benchmarks target in Makefile
3 direct replies — Read more / Contribute
by bliako
on Jun 19, 2018 at 07:50

    Dear Monks,

    I would like to add a target for running benchmarks on my code to the Makefile produced by ExtUtils::MakeMaker

    I already have a file which does everything and prints a report. I would like to have it executed whenever I do: make benchmarks . And only then because it is time consuming and should not be in tests.

Win32::OLE SaveAs Unicode
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 19, 2018 at 07:39


    I need to use Win32::OLE to extract the text from MsWord. The best solution I could find to maintain a bit of formatting is to use the SaveAs function (I would prefer to read directly into a variable, but I can leave with it). The problem is I can NOT find how to set the parameters to save the file in Unicode (something you get asked by Word after clicking on SaveAs...). I've read all I could, but could not find any substitution/completion of "wdFormatTextLineBreaks" to achieve this goal. On Microsoft specification page, they speak about "wdFormatUnicodeText" with value "7". But I can't find how to specify it in my script (just replacing "wdFormatTextLineBreaks" with "wdFormatUnicodeText" does not produce any effect). Maybe some of you know the answer.

    #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw( catfile ); use Cwd qw(cwd); use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; my $dir = cwd; my $word = get_word(); $word->{Visible} = 0; my $doc = $word->{Documents}->Open(catfile $dir, 'test.docx'); $doc->SaveAs( catfile($dir, 'test.txt'), wdFormatTextLineBreaks ); $doc->Close(0); sub get_word { my $word; eval { $word = Win32::OLE->GetActiveObject('Word.Application'); }; die "$@\n" if $@; unless(defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit +}) or die "Oops, cannot start Word: ", Win32::OLE->LastError, "\n"; } return $word; } __END__
Exact Regex for port Number
4 direct replies — Read more / Contribute
by theravadamonk
on Jun 19, 2018 at 03:15

    Hi, Monks, I am looking for an EXACT REGEX for a port number?

    a few e.g



    It DON'T want to MATCH my date Jun 19 08:48:50 where : is also present.

    I went to and wrote a bit, It WILL catch : of DATE data too

    this catches BOTH

    Can I have this?

    Your efforts r welcome.

74HC165 serial register reading with perl
2 direct replies — Read more / Contribute
by rkrasowski
on Jun 18, 2018 at 14:55
    Hi Perl Monks, that may be the silly question. I am trying to read data from 74HC165 which is PISO serial register. How effective (fast) would be to read it using Perl? Any code examples? Thanks like always Robert
How can I replace the pattern in the 6 th field?
2 direct replies — Read more / Contribute
by theravadamonk
on Jun 18, 2018 at 11:23

    Hi Perl Monks,

    I have DATA like this

    __DATA__ Jun 12 09 mail ( - ( 0.075 9387 Jun 12 10 mail ( - (,(recip2@domai -1.889 25623

    How can I replace the pattern ( and ) in the 6 th field?

    The OUTPUT I expect is

    __DATA__ Jun 12 09 mail ( - 0.075 9387 Jun 12 10 mail ( -,recip2@domain.c +om -1.889 25623

    the below code replaces all fields.

    $_ =~ s/\(|\)//g;

    Can I have something like this?

    $_ =~ s/\(|\)//$f[6]g;

    Your Ideas? Pls help me to slove this.

Combining "Reverse Ajax" aka "Comet" with Mojolicious?
No replies — Read more | Post response
by LanX
on Jun 18, 2018 at 09:12
    We use a "XMLHttpRequest long polling" approach at work to dynamically push JS and HTML actions via Ajax from the server to the browser, thus reducing the amount of JS coded and keeping application logic on the server side.

    I'd like to migrate to Mojolicious, and would prefer to hear of ready to use plugins before reinventing the wheel... :)

    Any recommendations?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

GLOB function
3 direct replies — Read more / Contribute
by rahu_6697
on Jun 18, 2018 at 09:05

    I am working on a database and i have to work on all .txt files present in different directories. Some file with same name are also present in different directories but i have to work on only those which are latest updated/created. I am using glob command but it is passing all files with .txt format into the array assigned to it. I just want to remove duplicate entries on basis of date of formation of file. Only latest file should present rest all files with same name should get eliminate.Only files with unique names should get stored in @filelist. Please help me out...

    my @filelist = glob"/user/*/ws/*/BLK_*/*.txt for my $file (@filelist) { . . . . . }
Algorithm inspiration required.
13 direct replies — Read more / Contribute
by BrowserUk
on Jun 18, 2018 at 07:16

    I'm looking to detect a repeating sequence in a stream of data.

    That is, given an essentially endless source of data that is known to eventually repeat; detect the start and length of the repeating sequence.

    In simplistic terms, I want to program the equivalent of the algorithm that solve this regex: $str =~ m[(.+).*?(\1)]g;

    Except that the values aren't characters or anything easily translated to characters and I cannot see the entire stream at any given time.

    And the data stream and the length of the repeated sequence can be big. Very big. Likely bigger than I can hold in memory, so all of those Liv-Zempel windowed and sliding buffer compression algorithms go out of the window; as do any complex tree structures (trees/tries/graphs et. al).

    The thought in my mind at the moment is (an adaption of) the rolling checksum algorithm used by rsync; but I'm not seeing how to apply it to my problem.

    Any thoughts on that; or any pointers to any other algorithms that might be adaptable to this problem gratefully received.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
PAR pp macOs set -mmacosx-version-min
1 direct reply — Read more / Contribute
by IB2017
on Jun 18, 2018 at 06:52

    Dear monks

    I am creating a binary of my script for macOS using PAR pp (latest version). Is there a way to set the value of -mmacosx-version-min? Reason: pp seems to set it automatically to the OS version used to compile the binary. In this way, however, there are problems when running the same binary on older OS versions. The binary created with pp contains the following info (read with  $ otool -l binary):

    Load command 9 cmd LC_VERSION_MIN_MACOSX cmdsize 16 version 10.13 sdk 10.13

    In some circumstances (for example if the binary is bundled in a .app), macOS will refuse to run the binary if the OS is older than the version indicated here (at least this is what I 've understood with my researches until now). The pp instructions do not seem to mention any option.

Use a CPAN module that is not installed! Is this a good idea?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 17, 2018 at 20:08
    This seems crazy but it actually works! Tests show this only happens if the module doesn't use other modules or compile. If dependencies can be handled then Perl could use even more CPAN modules that are not installed! Thoughts?
    #!/usr/bin/perl -l # Use a CPAN module that is *not* installed! Is this a good idea? # Posted to by an Anonymous Monk on Sun Jun 17 2018 # # In this example, if the Astro::MoonPhase module is not installed, # the use_cpan sub will parse the error, download the module page # from metacpan, parse that for the source code uri, download the # raw module source, eval that string, and proceed normally as if # the module was installed! It does not work with all modules and # the concept is presented to the Monastery for contemplation. #__QKB__ use strict; use warnings; eval { require Astro::MoonPhase }; use_cpan($@) if $@; my @phases = Astro::MoonPhase::phasehunt(); print q~LOCALTIME = ~, scalar localtime time; print q~New Moon = ~, scalar localtime $phases[0]; print q~First quarter = ~, scalar localtime $phases[1]; print q~Full moon = ~, scalar localtime $phases[2]; print q~Last quarter = ~, scalar localtime $phases[3]; print q~New Moon = ~, scalar localtime $phases[4]; sub use_cpan { my ($module) = @_; if ($module =~ /install the (\S+) module/) { my $modname = $1; require HTTP::Tiny; my $pm = HTTP::Tiny->new->get("$m +odname"); for ($pm->{content} =~ /Source<\/a>\s*\(<a href="([^"]+)"> +raw/) { $pm = HTTP::Tiny->new->get($1); eval $pm->{content}; last } } }

    Hack away...

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2018-06-23 12:46 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.