http://www.perlmonks.org?node_id=479

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
How Could I Remove Stopwords using stopwords.txt?
1 direct reply — Read more / Contribute
by mynameisbob
on Dec 05, 2020 at 03:33

    Hi everyone! Now I have extracted all sentences from htmls to Quotes.txt successfully. My next step is to remove all stopwords (using stopwords.txt) from the quotes.txt, and then count the number of occurrences each word appears and output this list to a text file with the most frequent words appearing at the top of the file. Now I am confused how to remove stopwords./p>

    my $quotes="Quotes.txt"; open(OUTPUT, ">$quotes")||die("Could not open $quotes!"); my $page_num=1; while ($page_num<=10){ my $htmlpages="Page$page_num.html"; open (INPUT,"$htmlpages")||die("Could not open $htmlpages"); my $line=""; while ($line=<INPUT>){ if($line=~m/<span class="text" itemprop="text">(.+?)<\/span/ig){ my $quotes=$1; $quotes =~ s/I'm/I am/ig; $quotes =~ s/(\w+?)'re/$1 are/ig; $quotes =~ s/(\w+?)'s/$1 is/ig; $quotes =~ s/(\w+?)n't/$1 not/ig; $quotes =~ s/it's/it is/ig; $quotes =~ s/(\w+?)'ll/$1 will/ig; $quotes =~ s/I've/I have/ig; $quotes =~ s/won't/will not/ig; $quotes =~ s/can't/cannot/ig; $quotes =~ s/\&\#34;/'/ig; $quotes =~ s/\&\#39;/'/ig; $quotes =~ s/let's/let us/ig; $quotes =~ s/lady's/lady is/ig; print OUTPUT "$quotes\n"; } } $page_num=$page_num+1; close(INPUT); } close(OUTPUT); my $quotes_0="WordCount.txt"; my $quotes_1="Quotes.txt"; open(QUOTES,">$quotes_0")||die("Could not open $quotes_0"); my $stopwords="stopwords.txt"; open(WORDS,"$stopwords")||die("Could not open $stopwords"); open(OLD,"$quotes_1")||die("Could not open $quotes_1"); my $line1=""; while(my $stop=<WORDS>){ if($stop=~m/(.+?)/ig){ my $stopwords=$1; if ($line1=<OLD>){ if($line1=~s/\b($stopwords)\b//ig){ print QUOTES "$line1\n"; } } } } close(WORDS); close(OLD); close(QUOTES);
Controlling USB on Raspberry Pi
4 direct replies — Read more / Contribute
by Bod
on Dec 04, 2020 at 16:51

    I have a blind uncle for whom we installed electric curtains for Christmas a couple of years ago as he doesn't know when it goes dark. Currently these are on a rather unreliable timer. Even if the timer were reliable, it doesn't account for the changing sunset time or the change from BST to GMT and back. Generally this hasn't been a problem as we visit and reset the timer monthly. But the COVID lockdown restrictions here in the UK have shown the problems in this arrangement.

    Having looked at several other options I am considering building a controller using a Raspberry Pi Zero and a relay module. I have two choices for connecting the relay module to the RPI - USB or GPIO. My preference is USB but want to check what is involved in controlling the USB port with Perl. Is USB::LibUSB going to be sufficient to control two relays on a module like this one or are there pitfalls awaiting me with USB?

    My plan is that the RPI will get sunset time for each day, open the curtains at 8:30am local time (GMT or BST) and close them at sunset (perhaps with a fixed offest if necessary). I've not used a Raspberry Pi before - any advice or suggestions?

XML::LibXML fails install on macOS Big Sur
2 direct replies — Read more / Contribute
by redsox43
on Dec 04, 2020 at 14:22

    Hi There. I'm not sure exactly what issue I'm hitting, but I think when I try to install XML::LibXML, it's not finding my install of LibXML2. This is what I've tried

    I installed "LibXML2" with Homebrew

    brew install libxml2 Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae Updated 19 formulae. Warning: libxml2 2.9.10_2 is already installed and up-to-date To reinstall 2.9.10_2, run `brew reinstall libxml2`

    I added a link to that in the .bash_profile

    # PATH PATH="/usr/local/Cellar/libxml2/2.9.10_2/bin:$PATH" export PATH

    I then tried to install "XML::LibXML"

    cpanm XML::LibXML --> Working on XML::LibXML Fetching http://www.cpan.org/authors/id/S/SH/SHLOMIF/XML-LibXML-2.0206 +.tar.gz ... OK Configuring XML-LibXML-2.0206 ... OK Building and testing XML-LibXML-2.0206 ... FAIL ! Installing XML::LibXML failed. See /Users/root/.cpanm/work/160710875 +5.33137/build.log for details. Retry with --force to force install it +.

    I did try to force the install, and it installed, but the perl script I'm trying to run still came up with the same error, so I don't think it was successful

    I checked the error log

    Can't load '/Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206 +/blib/arch/auto/XML/LibXML/LibXML.bundle' for module XML::LibXML: dlo +pen(/Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206/blib/a +rch/auto/XML/LibXML/LibXML.bundle, 2): Symbol not found: _xmlFree Referenced from: /Users/root/.cpanm/work/1607108755.33137/XML-LibXML +-2.0206/blib/arch/auto/XML/LibXML/LibXML.bundle Expected in: flat namespace in /Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206/blib/ar +ch/auto/XML/LibXML/LibXML.bundle at /Users/root/perl5/perlbrew/perls/ +perl-5.28.2/lib/5.28.2/darwin-2level/DynaLoader.pm line 197. &#65533; at /Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206 +/blib/lib/XML/LibXML.pm line 156. BEGIN failed--compilation aborted at /Users/root/.cpanm/work/160710875 +5.33137/XML-LibXML-2.0206/blib/lib/XML/LibXML.pm line 156. Compilation failed in require at t/01basic.t line 6. BEGIN failed--compilation aborted at t/01basic.t line 6. # Looks like your test exited with 2 before it could output anything. t/01basic.t ........................................ Dubious, test returned 2 (wstat 512, 0x200) Failed 3/3 subtests Can't load '/Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206 +/blib/arch/auto/XML/LibXML/LibXML.bundle' for module XML::LibXML: dlo +pen(/Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206/blib/a +rch/auto/XML/LibXML/LibXML.bundle, 2): Symbol not found: _xmlFree Referenced from: /Users/root/.cpanm/work/1607108755.33137/XML-LibXML +-2.0206/blib/arch/auto/XML/LibXML/LibXML.bundle Expected in: flat namespace in /Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206/blib/ar +ch/auto/XML/LibXML/LibXML.bundle at /Users/root/perl5/perlbrew/perls/ +perl-5.28.2/lib/5.28.2/darwin-2level/DynaLoader.pm line 197. &#65533; at /Users/root/.cpanm/work/1607108755.33137/XML-LibXML-2.0206 +/blib/lib/XML/LibXML.pm line 156. BEGIN failed--compilation aborted at /Users/root/.cpanm/work/160710875 +5.33137/XML-LibXML-2.0206/blib/lib/XML/LibXML.pm line 156. Compilation failed in require at /Users/root/.cpanm/work/1607108755.33 +137/XML-LibXML-2.0206/blib/lib/XML/LibXML/Common.pm line 29. BEGIN failed--compilation aborted at /Users/root/.cpanm/work/160710875 +5.33137/XML-LibXML-2.0206/blib/lib/XML/LibXML/Common.pm line 29. Compilation failed in require at t/02parse.t line 20. BEGIN failed--compilation aborted at t/02parse.t line 20. # Looks like your test exited with 2 before it could output anything. t/02parse.t ........................................ Dubious, test returned 2 (wstat 512, 0x200) Failed 533/533 subtests

    It goes on like this for about 3 miles

    Has anyone seen this before, and have any thoughts and theories? Please let me know if you need more info. Thanks in advance, your help is much appreciated.

Inspecting package vars in a sub
1 direct reply — Read more / Contribute
by LanX
on Dec 04, 2020 at 13:30
    Hi

    PadWalker has a handy function peek_sub() to identify all private vars in a sub, but lacks a method to do so with package vars.

    DB<318> $code = sub { my $a ** $b ** $bla::c } DB<319> x peek_sub($code) 0 HASH(0x3d161a8) '$a' => SCALAR(0x3d18da0) -> undef DB<320>

    What's the best way to achieve this, except parsing the text output of B::Concise for GVs? (is this even reliable?)

    I think I asked this before, but who knows what changed in the meantime :)

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

    UPDATE

    ) #<-- comments added

    D:\>perl -MO=Concise,bla -e "sub bla {@a = ($b, my $c)}" main::bla: 9 <1> leavesub[1 ref] K/REFC,1 ->(end) - <@> lineseq KP ->9 1 <;> nextstate(main 2 -e:1) v ->2 8 <2> aassign[t5] KS/COM_AGG ->9 - <1> ex-list lKP ->5 2 <0> pushmark s ->3 - <1> ex-rv2sv sK/1 ->4 3 <#> gvsv[*b] s ->4 #<-- $::b 4 <0> padsv[$c:2,3] sM/LVINTRO ->5 #<-- my $c - <1> ex-list lK ->8 5 <0> pushmark s ->6 7 <1> rv2av[t2] lKRM*/1 ->8 6 <#> gv[*a] s ->7 #<-- @::a -e syntax OK
Convert LST dates to LT dates
1 direct reply — Read more / Contribute
by Anonymous Monk
on Dec 03, 2020 at 19:13

    Hi,

    I have a list of dates in local standard time that I want to convert to local time.

    I came up with this, but I guess there's some smarter way...

    Thanks



    use strict; use DateTime::Duration; use DateTime; # These are local standard times in Europe/Rome, i.e. always UTC+0100 my @dates = ( [2020,12,2,1,0], [2021,7,2,1,0] ); my $timezone = '+0100'; # This is the duration to be added to go from LST to UTC my $seconds = -($timezone/100) * 3600; my $dur = DateTime::Duration->new( years => 0, months => 0, weeks => 0, days => 0, hours => 0, minutes => 0, seconds => $seconds, nanoseconds => 0 ); for my $date (@dates) { my $dt = DateTime->new( year => => $date->[0], month => $date->[1], day => $date->[2], hour => $date->[3], minute => $date->[4], second => 0, nanosecond => 0, time_zone => 'UTC' ); $dt->add($dur); # This is a DateTime object in LST print "LST: ",join("-",@{$date}),"\n"; print "UTC: ",$dt->set_time_zone("UTC"),"\n"; print "LT: ",$dt->set_time_zone("Europe/Rome"),"\n"; }
How to Split on specific occurrence of a comma
8 direct replies — Read more / Contribute
by dirtdog
on Dec 03, 2020 at 16:13

    Hi Monks, I'm a little stuck and was hoping one of you could help get me out of the mud :-). I had a script that worked until we added over 999 values to it recently. I now have about 3,000 values separated by a comma on 1 line. Unfortunately, I can't change the way the data is provided. Ultimately, I need to get a max of 999 of them into an array or some other structure so I can then perform a query from the db with those values. i.e, select * from table_name where values in ( ). If I can just get chunks of 999 stuffed into an array I could then do a "for loop" until all values have been queried and added as a new worksheet to my excel spreadsheet. It would need to be dynamic too since the values could increase or decrease over time.

    Below is a snippet of the code that used to work (until we broke the 999 threshold that Oracle allows in an "IN" clause by adding over 3k comma separated values in the input record.)

    open INPUT_FILE, @row[$cell_column] or die "Can't open @row[$cell_colu +mn]: $!\n"; while ( $line = <INPUT_FILE> ) { chomp $line; if ($line =~ /^#/ || $line =~ /^\s*$/) { next; } $line =~ s/^[^=]*\=//; @users = split /,/, $line; } close INPUT_FILE; $placeholders = join ( ',', ('?') x @users ); my $SQLLock=qq{ select user,name from teams where user in ( $placeholders ) }; my $sthL = $dbh->prepare($SQLLock); $sthL->execute(@users); while ( my @row = $sthL->fetchrow_array() ) { print ">@row<\n"; for($cell_column=0; $cell_column<=$#row; $cell_column+ ++){ $worksheet->write_string($i, $cell_column, @r +ow[$cell_column],$format1); } $worksheet->set_row($i, undef, undef, 0, 0, 1); $i++; } $sthL->finish;

    Here is my attempt to get the split function broken up by lines of 3. ( For the example I chose a small number because it's more managable. Just need to prove out the logic, but clearly the "limit" clause of the split isn't keeping 3 values per line. This does not work.

    #!/usr/bin/env perl use DBI; use POSIX 'strftime'; use strict; use Data::Dumper; my $line; my @teams; while ( $line = <DATA> ) { chomp $line; if ($line =~ /^#/ || $line =~ /^\s*$/) { next; } $line =~ s/^[^=]*\=//; @teams = map { [ split /,/, $_, 3 ] } $line; } print Dumper \@teams; __DATA__ Teams=PATRIOTS,BILLS,DOLPHINS,JETS,COWBOYS,GIANTS,EAGLES,REDSKINS,BENG +ALS,OILERS,STEELERS,BROWNS,SEAHAWKS,RAMS,49ERS,RAIDERS

    Would greatly appreciate any help or assistance that could be provided. Thanks!

Perlbrew on shared hosting
5 direct replies — Read more / Contribute
by Bod
on Dec 03, 2020 at 14:19

    I am trying to upgrade from Perl v5.16.3 supplied by my shared hosting provider to Perl v5.32.0 by using Perlbrew. Perlbrew has installed fine by following the instruction \curl -L https://install.perlbrew.pl | bash from PuTTY.

    Perl v5.32.0 has downloaded fine but it will not install. I run the command:

    perlbrew install perl-5.32.0
    and get the message
    Installation process failed. To spot any issues, check /home/shoples1/perl5/perlbrew/build.perl-5.32.0.log
    Looking at the logfile everything makes sense. It shows the questions for a guided installation with the answers to the questions already filled in. Then right at the end is this:
    Use which C compiler? [cc] ./trygcc: line 10: /usr/bin/cc: Permission denied Uh-oh, the C compiler 'cc' doesn't seem to be working. ./trygcc: line 25: /usr/bin/gcc: Permission denied ./checkcc: line 10: /usr/bin/cc: Permission denied Uh-oh, the C compiler 'cc' doesn't seem to be working. You need to find a working C compiler. Either (purchase and) install the C compiler supplied by your OS vendo +r, or for a free C compiler try http://gcc.gnu.org/ I cannot continue any further, aborting. ##### Brew Failed #####
    I have previously installed modules from CPAN using PuTTY and SSH thanks to help from the Monastery. I know that I do not have access to install modules if I attempt it through cPanel but it has worked recently from an SSH shell.

    Am I missing something obvious?

    Is it possible that installing , will installing Perlbrew will upset any of the scripts already running on the server using Perl v5.16.3?

Perl syntax checking without `perl -c`
5 direct replies — Read more / Contribute
by kcott
on Dec 02, 2020 at 20:55

    Very brief background: Some idiot dug a hole, cut through a main electrical cable, and killed the power to my $work (and many others in the vicinity). Most of our systems are back up and running but not all of those that I need. To continue working, I've download various files from out git repostory to my home computer. I'm making code changes here but do not have the full environment available to me. Getting that environment is problematic at best, perhaps not possible at all, while "recovery" procedures are in progress.

    After making changes, typing 'perl -c ...' invariably gives me something like: "Can't locate SomeMod.pm in @INC ...".

    I've checked perlrun, under "-c" it has:

    "causes Perl to check the syntax of the program and then exit without executing it. Actually, it will execute any BEGIN, UNITCHECK, or CHECK blocks and any use statements: these are considered as occurring outside the execution of your program. INIT and END blocks, however, will be skipped."

    The use statements will be my biggest problem; although, others may crop up here and there.

    Does anyone know of something simple, like 'perl -c ...', that will just check the syntax without executing any part of the code?

    I'm hoping the system will be fully operational within the next day or two. Anything that involves a substantial amount of work — e.g. setting up and configuring a framework; installing lots of modules; etc. — is probably not an option for me at the present; however, that could still be useful to know and have available for some future, similar situation.

    — Ken

How is Perl for automation?
10 direct replies — Read more / Contribute
by fidodido
on Dec 02, 2020 at 14:05

    Hi

    I'm planning to learn a scripting language for automation. Perl has been a default choice in this scenario, but with other languages coming up, how's Perl doing? I wanna automate some stuff in my current job,start with simple automation, then may be move on to excel reports with graphs (The management here is big time into it. They just like everything "graphisized" :). I'll then use that experience to sort of "break" into automation focused jobs. So how's Perl demand in automation?

    I know this is like walking into a Harley Davidson showroom and asking folks which is a better thing, a cruiser or a sports bike (or vice versa)? But hope you guys would be able to give me some clarity.

    Another reason for asking here, is, there's a lot of articles on the net that seem to indicate that Perl is on it's way out....and then there's an article about Perl 7 (and many more on Perl 6).

    Which brings me to the next question. I'm planning to start with Perl 5, and then may be move to Perl 6. Perl 5 seems to be default installed on the linux servers here, and then there's some Windows friendly version installed on some other servers.

    That's one too many questions, but hope you folks would clear it up for me.

Upgrade to Big Sur perl issue
3 direct replies — Read more / Contribute
by redsox43
on Dec 02, 2020 at 13:38

    I've just updated my Mac to 11.0.1, and I had a perl script that ran successfully before doing that. When I run it now I get

    "Can't locate Archive/Extract.pm in @INC (you may need to install the Archive::Extract module)"

    I thought that during the upgrade Archive/Extract.pm had been removed, or a link to it had been broken, so I figured I would install it, but when I do that I get the below

    I read some posts on possible solutions, and I tried removing the "02packages.details.txt.gz" and the "01mailrc.txt.gz", and rebuilding those files with "perl -MCPAN -e shell", but I still get the same error

    Other posts where I should run Strawberry Perl instead, and leave the Perl that came with the OS alone, but I wanted to ask if anyone had any thoughts before I started down that path.

    Thanks

    cpan[1]> install Archive::Extract.pm Reading '/Users/root/.cpan/Metadata' Database was generated on Fri, 14 Dec 2018 11:17:03 GMT Reading '/Users/root/.cpan/sources/authors/01mailrc.txt.gz' ...................................................................... +......DONE Reading '/Users/root/.cpan/sources/modules/02packages.details.txt.gz' Warning: Your /Users/root/.cpan/sources/modules/02packages.details.txt +.gz does not contain a Line-Count header. Please check the validity of the index file by comparing it to more than one CPAN mirror. I'll continue but problems seem likely to happen. Warning: Your /Users/root/.cpan/sources/modules/02packages.details.txt +.gz does not contain a Last-Updated header. Please check the validity of the index file by comparing it to more than one CPAN mirror. I'll continue but problems seem likely to happen. Could not split line[""] Could not split line[""] .Could not split line[" !function(f,b,e,v,n,t,s)"] Could not split line[" {if(f.fbq)return;n=f.fbq=function(){n.callMe +thod?"] Giving up parsing your /Users/root/.cpan/sources/modules/02packages.de +tails.txt.gz, too many errorsReading '/Users/root/.cpan/sources/autho +rs/01mailrc.txt.gz' ...................................................................... +......DONE Reading '/Users/root/.cpan/sources/modules/02packages.details.txt.gz' Warning: Your /Users/root/.cpan/sources/modules/02packages.details.txt +.gz does not contain a Line-Count header. Please check the validity of the index file by comparing it to more than one CPAN mirror. I'll continue but problems seem likely to happen. Warning: Your /Users/root/.cpan/sources/modules/02packages.details.txt +.gz does not contain a Last-Updated header. Please check the validity of the index file by comparing it to more than one CPAN mirror. I'll continue but problems seem likely to happen. Could not split line[""] Could not split line[""] .Could not split line[" !function(f,b,e,v,n,t,s)"] Could not split line[" {if(f.fbq)return;n=f.fbq=function(){n.callMe +thod?"] Giving up parsing your /Users/root/.cpan/sources/modules/02packages.de +tails.txt.gz, too many errorsTerminal does not support GetHistory. Lockfile removed.

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!
  • 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.