Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

The Monastery Gates

( #131=superdoc: print w/replies, xml ) Need Help??

Donations gladly accepted

If you're new here please read PerlMonks FAQ
and Create a new user.

New Questions
Using a thread to provide visual feedback.
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 21, 2018 at 10:54

    I have been working on a command line script that makes several system calls, some of which take a few seconds (minutes?) to return. The long pauses give the impression that the script has hung, which isn't great. I figured I could address this by adding some kind of visual "still processing" feedback, like an hourglass or pinwheel. Because it's just a command line script, I do it with dots.
    My code is below. I would appreciate any feedback or suggestions for improvement. Thank you.

Extracting text from MS Word files on a Linux box
3 direct replies — Read more / Contribute
by Laurent_R
on Jun 21, 2018 at 06:30
    Dear fellow monks,

    I need to extract some textual information from a large bunch of MS Word files, mostly at the .doc format (i.e. Office 2003 version or before, if I remember correctly); there also may be some .docx files but let's concentrate on the .doc format for now.

    This is quite new to me, I've never had before to do that, and I am usually not working on Windows platform and Microsoft Office document formats, except for a few cases with Excel documents. So I am not sure how to handle this requirement.

    I also found that there seem to be surprisingly few CPAN solutions for extracting data from MS Word documents (especially in the pre-2003 format).

    The Text::Extract::Word module may be doing what I need, but it has a dependency on the OLE::Storage_Lite module (and perhaps some others). My understanding is the OLE::... domain modules will only work on Windows boxes (because they are presumably using Windows and/or MS Office libraries). Is this a correct assumption? If such is the case, then it would be a problem for us, because, ultimately, the whole much larger process should run on a Linux server.

    Does any monk here have some experience on this type of needs, i.e., to summarize, how to extract textual data from MS Word files (pre-2003 format) on a Linux box, where OLE modules would presumably not run? Any suggestions? Any other module that I overlooked? Thank you very much for any help in this respect.

Searching a pattern in a xlsx
2 direct replies — Read more / Contribute
by kaushik9918
on Jun 21, 2018 at 01:50
    Hi Can anyone here point me to an existing thread or suggest me a good short routine/function to find a user defined pattern in an xlsx and return its row and col number? thanks in advance
Path parsing from full to relative
7 direct replies — Read more / Contribute
by Kaplah
on Jun 20, 2018 at 15:56
    Greetings all. I was wondering how I would accomplish the following. Given a full file path how can I trim it down to the last folder? For instance:


    C:\somedirectory\anotherDirectory\WorkingDirectory\myFile.txt
    C:\somedirectory\anotherDirectory\WorkingDirectory\Logs\myLogs.txt
    C:\somedirectory\anotherDirectory\WorkingDirectory\Stuff\myStuff.txt


    needs to become:
    \WorkingDirectory\myFile.txt
    \WorkingDirectory\Logs\myLogs.txt
    \WorkingDirectory\Stuff\myStuff.txt


    The trick here is that the directories are not hard coded. My only life saver is that the "WorkingDirectory" has a naming standard and starts with the same three characters (for argument "ABC"). Is there a way to grab this folder once and then build the other strings? Or can I pull the reverse and subtract the unwanted beginning portion out of my path string? How would I do this?
list of four digit lock combinations without repeated digits
5 direct replies — Read more / Contribute
by Lotus1
on Jun 20, 2018 at 14:58

    I have a spare key lock for my front door that takes four digits without repeated digits and the order doesn't matter. I've forgotten the combination so I wrote a script to give me a list of the possible combinations. I'm planning to replace this since I don't like that it only has 210 possible combinations but I have to open it before I can remove it. Here is my solution. I found the text at the top in one of the answers at https://math.stackexchange.com/questions/156928/number-of-4-digit-numbers-with-no-repeated-digit.

    I'm wondering if there is a way to do this with some modules or if anyone else has interesting solutions.

    use strict; use warnings; # Four digit mechanical lock: no repeated digits, order doesn't matter +. # #https://math.stackexchange.com/questions/156928/number-of-4-digit-num +bers-with-no-repeated-digit # Start by finding the permutations: For the first choice, you have 10 + possible digits to choose from. #For the second choice, you have 9 digits because you used one for the + first choice. #The third choice comes from 8 possibilities and the fourth from 7 pos +sibilities. #Now we multiply these together: 10 x 9 x 8 x 7 = 90 x 56 = 5040. That +'s the number of permutations. #No digits repeat, but 0123 is different from 0321. # Now to find the number of combinations, I have to know how many diff +erent ways there are of arranging four digits. #That's the same kind of problem: the first position could be from 4 p +ossibilities, the second from 3 possiblities, #the third from 2 choices and the last has to be the 1 left. So there +are 4 x 3 x 2 x 1 = 24 possible ways of #arranging 4 items. # Therefore I divide 5040 / 24 = 210. So there are 210 different combi +nations of four digits chosen #from 0-9 where the digits don't repeat. my %output; foreach(123..9876){ my $num = sprintf "%04d", $_; next if $num =~ /(\d).*\1/; my @digits = sort split //, $num; my $num_sorted = join '', @digits; #print "$num: @digits - $num_sorted\n"; if (not exists $output{$num_sorted} ) { #print "adding $num_sorted\n"; $output{$num_sorted}=1; } } print "found ", scalar keys %output, " combinations.\n"; print "$_\n" foreach sort keys %output;

    Here are the results:

Replacing CGI.pm
5 direct replies — Read more / Contribute
by horace
on Jun 20, 2018 at 13:22

    I have several Perl, use CGI qw(:standard) - scripts that are fine and well-tested, uses function style programming. I would like to upgrade our servers OS from Wheezy to Debian Stretch which does not have CGI.pm in the Perl core. I believe the upgrade to Stretch will leave CGI.pm intact (which the scripts need), but if not, what do I do? Should I consider PSGI/Plack? I'm happy with everything now but eventually something will have to be done. Is there an easy work-around?

remove a comma
5 direct replies — Read more / Contribute
by luupski
on Jun 20, 2018 at 13:22

    I have the following list in a notepad file called CUSTOMER.txt

    --- this is CUSTOMER.txt (input) ---
    0001 20000001 john CA
    0002 30000002 neill WI
    0003 40000003 joe GA
    0004 50000004 will IL
    0005 60000005 mike IN
    0006 70000006 bill AK

    I feed this into the script and get the following output which is placed in the CUSTOMER_NEW.txt file
    All good for except 1 thing.
    I want the last ' , ' (comma)to be removed before the closing bracket.
    Tried several things after googling, but no success

    So i was wandering if anybody has an idea how to add to the code given that will remove the last ' , '


    --- this is CUSTOMER_NEW.txt (output) ---
    SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION
    FROM DB.CUSTOMER_DATA
    WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN (
    (0001,20000001,john,CA),
    (0002,30000002,neill,WI),
    (0003,40000003,joe,GA),
    (0004,50000004,will,IL),
    (0005,60000005,mike,IN),
    (0006,70000006,bill,AK),
    )

    --- script used ---
    sub customer {system ("notepad ./CUSTOMER.txt"); $dir="./"; $custom=$dir."CUSTOMER.txt"; $dir="./CUSTOMER_DIR/"; $ofile=$dir."CUSTOMER_NEW.txt"; open (IN,"$custom") || die "Cannot open CUSTOMER.txt!!!\n"; open (OUT,">$ofile") || die "Cannot open CUSTOMER_NEW.txt!!!\n"; print OUT "SELECT CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION FROM DB.CUSTOMER_DATA WHERE (CUSTOMERID, ORDERID, CUSTOMERNAME, CUSTOMERLOCATION) IN (\n"; @CUST=<IN>;close IN; foreach $infile (@CUST){ chomp($infile); $i=0; @a=split(/\|/,$infile); @BAGO=$a[$i]; foreach $infile1 (@BAGO){ @b=split(/\s+/,$infile1); print OUT "($b[0],$b[1],$b[2],$b[3])\,\n"; $i++;}} print OUT ")\n"; system ("notepad ./CUSTOMER_DIR/CUSTOMER_NEW.txt"); close OUT; goto START; }
Input Validation and pattern matching in Template Toolkit
5 direct replies — Read more / Contribute
by dipit
on Jun 20, 2018 at 03:57
    [% META title = 'Add xyz' %] [% attack = params.taskid_login %] [% IF attack.search('<script>') || attack.search('</script>') || attac +k.search('alert') %] [% attack = params.taskid_login | uri | html %] [% ELSE %][% attack = params.taskid_login %] <p> To add a new task ID member to <b>[% attack %]</b>, select one or +more users from the table below. </p> [% WRAPPER filter.tt filterTitle='Users filter' filterAction='add_task +id_member' %] [% INCLUDE form_textfield.tt fieldName='filter_login' fieldValue=p +arams.filter_login maxLength=100 %] [% END %] [%# Show global message is one or more members have been added... %] [% IF NumberOfAddedRecords %] [% INCLUDE global_message.tt globalMessage=NumberOfAddedRecords _ +' users have been added.' %] [% END %] [% INCLUDE form_table.tt table=PossibleMembersTable %]

    Hello All. Here i have created a varibale named "attack" and checking some validation over it. params.taskid_login is the input value to "attack" variable here. I want to detect <script> and alert tags here. I think my syntax is wrong so my page containing this template is not loading. Please help?

Module Ideas, Suggestions, and Requests
4 direct replies — Read more / Contribute
by oducs
on Jun 19, 2018 at 08:34

    I am looking for new and interesting ideas for Perl modules!

    The chosen modules will be uploaded to the new oducs CPAN: https://metacpan.org/author/ODUCS

    If you have a module which you would like to abandon into my care, let's hear about it.

    Also, I have an interest in simultaneously developing Pure-Perl and XS versions.

    Appreciated, Charlie Root (perlmonks@oducs.org)
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 benhmarks.pl 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.

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

    Monks,

    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__
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 (sender@sender.com) - (recip1@domain.com) 0.075 9387 Jun 12 10 mail (sender@sender.com) - (recip1@domain.com),(recip2@domai +n.com) -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 (sender@sender.com) - recip1@domain.com 0.075 9387 Jun 12 10 mail (sender@sender.com) - recip1@domain.com,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.

New Cool Uses for Perl
Why is it so easy to make Perl apps for Apple Mac?
2 direct replies — Read more / Contribute
by Anonymous Monk
on Jun 17, 2018 at 13:40
    Compile your Perl to a portable binary application for Apple Mac (99k):
    echo 'display alert (do shell script "perl -v")' | osacompile -o perl. +app
    Write apps for Apple Mac in Perl: Pt.1, Pt.2, Pt.3
How to write apps for macOS/OSX in Perl! Part 2
No replies — Read more | Post response
by Anonymous Monk
on Jun 14, 2018 at 17:00
    Welcome to Part 2 of How to write apps for macOS/OSX in Perl! See Part 1 to get started with the built-in macOS devtool Automator. This edition demonstrates how to:

    • Process choices with Perl from an Applescript dialog to:
      1. Display output from Perl to an Applescript dialog.
      2. Execute Perl in Terminal to display its output.
      3. Send output from Perl to an application (TextExit).
    Applescript is to the operating system what Javascript is the the web browser. It can do many things and what it can't do can always be handled by shell commands and especially Perl! When the code below is saved by Automator as something like PerlVersionTool.app you will have a 1.3MB portable binary application! Double click and ENJOY!
    • Other techniques covered here include:
      1. Visiting websites (Perlmonks of course!)
      2. Displaying notifications
      3. Application control
      4. Applescript subroutines
      5. Abusing buttons to widen dialogs
      6. How to rule your world with Perl!
    Source:
    (* Demonstration macOS/OSX app in AppleScript and Perl *) (* Posted to perlmonks.org by Anonymous Monk 6/14/2018 *) (* Node: How to write apps for macOS/OSX in Perl! Part 2 *) set TITLE to "Perl Version Tool" set PROMPT to "Make a selection" set _1 to "Perl version, patchlevel and license" set _2 to "Perl configuration summary" set _3 to "Perl command line help" set _4 to "Visit Perlmonks.org!" repeat set what to choose from list {_1, _2, _3, _4} with title TITLE wit +h prompt PROMPT OK button name {"View"} cancel button name {"Exit"} d +efault items _1 set what to what as string if what is _1 then set CMD to "perl -v" # ONE LINERS OR PROGRAMS OF ANY SIZE! else if what is _2 then set CMD to "perl -V" else if what is _3 then set CMD to "perl -h" else if what is _4 then display notification "Opening The Monastery Gates!" set CMD to "open https://perlmonks.org" else if what is "false" then return # EXIT end if if what is _2 then # SEND PERL CODE TO TERMINAL AND EXECUTE doShell(CMD) else if what is _3 then # CAPTURE PERL STDOUT set CMD to do shell script CMD # SEND PERL STDOUT TO TEXTEDIT textEdit(CMD) else # CAPTURE PERL STDOUT set RES to do shell script CMD # MAKE DIALOG WIDE set SPC to " + " # PRINT PERL STDOUT TO APPLESCRIPT ALERT display alert TITLE message RES buttons {SPC & "Cool" & SPC} d +efault button 1 end if end repeat # APPLESCRIPT SUBS: on doShell(CMD) try tell application "Terminal" activate tell application "System Events" to keystroke "n" using {c +ommand down} end tell tell application "System Events" tell application process "Terminal" set frontmost to true keystroke CMD keystroke return end tell end tell on error oops display alert oops as critical end try end doShell on textEdit(CMD) try tell application "TextEdit" activate tell application "System Events" to keystroke "n" using {c +ommand down} end tell tell application "System Events" tell application process "TextEdit" set frontmost to true keystroke CMD end tell end tell on error oops display alert oops as critical end try end textEdit
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-06-21 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (118 votes). Check out past polls.

    Notices?