Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Seekers of Perl Wisdom

( [id://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.

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.

Post a new question!

User Questions
IP Lookup Tables
2 direct replies — Read more / Contribute
by monsignor
on Mar 03, 2024 at 02:22

    Greeting fellow monks. I am again trying my hand at Perl and hope that someone can point me in the right direction.

    I found a good free IP Range to Country/City/ASN, CSV database:

    https://ipinfo.io/pricing - (Partway down the page you can see the free db downloads)

    The Database seems good, but that leaves the issue of how do do lookups. I'm wondering if anybody has any suggestions?

    I would think that there should be code available for find an IPv4 address in a table of Low/High addresses.

    I'm open to other free solutions, or some code that I can hack. Its just for personal use to help with spam filtering.

    Any suggestions would be much appreciated.

Win32::OLE Excel Conditional Formatting error
4 direct replies — Read more / Contribute
by slick.user
on Feb 29, 2024 at 12:06
    Hi, I'm trying to use Conditional Formatting. I recorded the Macro (comment inside code) and tried to translate to Perl OLE with the code below but got error: Can't call method "Font" on an undefined value at excel_test.pl line 56. I can loop through each cell and check if it not equal to "TRUE" and assign color. I'm trying to avoid looping in Perl.
    use strict; use warnings; use Win32::OLE; use Win32::OLE qw(in with); my $EXCEL = Win32::OLE->new("Excel.Application","Quit"); $EXCEL->{"Visible"} = 1; $EXCEL->{DisplayAlerts}= 0; my $outfile = "C:\\test\\file1.xls"; $EXCEL -> {"SheetsInNewWorkBook"} = 1; my $Workbook = $EXCEL -> Workbooks -> Add(); my $CurrentSheet = $Workbook -> Worksheets(1); $CurrentSheet->Range("A1")->{"Value"} = "DataA"; $CurrentSheet->Range("A2")->{"Value"} = "abs"; $CurrentSheet->Range("A3")->{"Value"} = "FALSE"; $CurrentSheet->Range("A4")->{"Value"} = "TRUE"; $CurrentSheet->Range("A5")->{"Value"} = "ABC"; $CurrentSheet->Range("A6")->{"Value"} = "xyz"; $CurrentSheet->Range("A7")->{"Value"} = "123"; $CurrentSheet->Range("A8")->{"Value"} = "-99999"; $CurrentSheet->Range("A9")->{"Value"} = "TRUE"; $CurrentSheet->Range("A10")->{"Value"} = "10"; $CurrentSheet->Range("B1")->{"Value"} = "DataB"; $CurrentSheet->Range("B2")->{"Value"} = "absx"; $CurrentSheet->Range("B3")->{"Value"} = "FALSE"; $CurrentSheet->Range("B4")->{"Value"} = "TRUE"; $CurrentSheet->Range("B5")->{"Value"} = "ABCx"; $CurrentSheet->Range("B6")->{"Value"} = "xyzx"; $CurrentSheet->Range("B7")->{"Value"} = "123x"; $CurrentSheet->Range("B8")->{"Value"} = "-99999x"; $CurrentSheet->Range("B9")->{"Value"} = "TRUE"; $CurrentSheet->Range("B10")->{"Value"} = "10x"; $CurrentSheet->Range("C1")->{"Value"} = "A=B?"; #my $rangeFormat = $CurrentSheet->Range("A4"); #$rangeFormat->Interior->{Color} = 255; # Columns("A:A").Select # Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNot +Equal, _ # Formula1:="=TRUE" # Selection.FormatConditions(Selection.FormatConditions.Count).SetF +irstPriority # With Selection.FormatConditions(1).Font # .Color = -16776961 # .TintAndShade = 0 # End With # Selection.FormatConditions(1).StopIfTrue = True my $rangeFormat = $CurrentSheet->Range("A:A"); $rangeFormat->FormatConditions->Add({Type=>"xlCellValue", Operator=>"x +lNotEqual", Formula1=>"=TRUE"}); $rangeFormat->FormatConditions(1)->Font->{Color} = -16776961; $Workbook->SaveAs({FileName => $outfile, FileFormat => -4143}); $Workbook -> Save(); $EXCEL->Quit(); Win32::OLE->FreeUnusedLibraries();
PDL undefined reference to pdl_pthread error on install
1 direct reply — Read more / Contribute
by Don Coyote
on Feb 28, 2024 at 10:39

    Fail point in build log

    g++ Core.def -o ..\..\blib\arch\auto\PDL\Core\Core.dll -mdll -s -L"C:\ +Dwimperl\perl\lib\CORE" -L"C:\Dwimperl\c\lib" ... pdlutil.o "lib.a" x +<i>n</i> -Wl,--enable-auto-image-base
    pdlutil.o:pdlutil.c:(.text+0x1f76): undefined reference to `pdl_pthrea +d_realloc_vsnprintf' pdlutil.o:pdlutil.c:(.text+0x4c): undefined reference to `pdl_pthread_ +free' collect2: ld returned 1 exit status dmake: Error code 129, while making '..\..\blib\arch\auto\PDL\Core\Co +re.dll' dmake: Error code 255, while making 'subdirs' dmake.exe: Error code 255, while making 'subdirs' -> FAIL Installing PDL failed.

    h tihere57!

    using cpanm PDL failed here. Also tried cpan PDL, but that did not even want to connect to strawberryperl website, and failed before it started.

    Not sure I could even go in and comment these out, as its being auto-installed from cpan.

    background: tk is way too slow at gfx, maybe SDL will be faster. Reading the Manual, want to intereact iwith my gfx capapbilities if possile. I did do a bit of blitting a while back at the suggestion of an esteemed perlmonk. Hearing the screams rising up from the pits of hell is probably best listened to only once though. OpenGL wont install on usb, so its gotta be PDL, for now.


    DeCoy
DBI and FEDERATED table slowdown
2 direct replies — Read more / Contribute
by misterperl
on Feb 27, 2024 at 15:59
    some of my mysql 5.7 tables are federated, hosted on a remote machine, on the same LAN. Pings between the machines are good- about 80ms.
    Prior to federation, DBI 1.643 transactions to the mysql database were fast. Since federation though, federated table transaction slowed to the point that users get messages like:

    DBD::mysql::st execute failed: Got timeout reading communication packets

    a WISE monk here once told me that DBI does nothing more than run the same mysql transactions as run ad hoc. That statement helped me, and held up through many trials for years (logged in as the same user running the same query, DBI and ad hoc produce the same result, or error, in roughly the same time).

    So, I tried the exact query that timed out in mysql, and the federated table (joined with a local table) responded immediately; no apparent speed difference with federated vs local tables from the mysql ad hoc query. I'd expected given the theorm above, it would also time out, or at least be slow. No; lightning fast!

    So I propose that theorm is not always true- there is some apparent and significant difference using DBI, vs ad hoc queries, on federated tables. Being a pretty rare configuration, I'm not confident that even the astute monk community would have advice on this topic, and I may have to ask the esteemed Tim Bunce if he's still in the DBI business! If not I wish him well.

    Best regards to all Monks, and any thoughts are appreciated. Gemini suggested increasing the TIMEOUTS on each end which I did, but that didn't help.

Module to extract text from HTML
7 direct replies — Read more / Contribute
by Bod
on Feb 27, 2024 at 06:10

    I've been searching unsuccessfully for a module to extract just the text from an HTML webpage...
    Any suggestions?

    Ideally, I want to feed in a URL and return the page's text as plain text - no formatting, tags, etc.

    Even most of the text would suffice.

    I'm currently using HTML::TreeBuilder and just extracting the p tags which is not quite good enough:

    my $http = HTTP::Tiny->new; my $resp = $http->get($url); my $tree = HTML::TreeBuilder->new; $tree->parse($resp->{'content'}); my @paragraph = $tree->look_down('_tag', 'p'); print "Content-type: text/plain\n\n"; foreach my $line(@paragraph) { print $line->as_trimmed_text . "\n"; }

    I thought I'd found a solution with HTML::Extract. But when the sample code in the documentation doesn't compile I knew I was heading down a dead end!

    Do you know of a module to extract just the text?

Get text between start and end tag
4 direct replies — Read more / Contribute
by Dirk80
on Feb 27, 2024 at 05:31

    I have a big text file. I'm interested only in the beginning tag and the text between a beginning and an end tag.

    textfile.txt

    Any text is in this document. But I'm only interested in the text between a beginning and an end tag +. BEGIN_TAG_110 In this text I'm interested in. It can consist of several lines. Until the end tag. END_TAG and any other things I'm not interested in until the next begi +nning tag starts. Any other text. BEGIN_TAG_237 I need this text. END_TAG More text ...

    The result should be like this:

    # BEGIN_TAG_110: # In this text I'm interested in. # It can consist of several lines. Until the end tag. # # BEGIN_TAG 237: # I need this text.

    I'd like to have the beginning tag as the key of a hash and the corresponding text as its string value.

    I tried this regular expression:

    m/^.*BEGIN_TAG_(\d+)(.*)END_TAG.*/s;

    But it works only with the last one. It seems to be too greedy. But my main problem is how not to get only one, but all.

    I'm really interested in how it could be done in a good manner or for any hints from you. Thank you!!

Do you have AI::MXNet running?
1 direct reply — Read more / Contribute
by The_Dj
on Feb 26, 2024 at 22:15

    Greetings Monks and lurkers.

    I had a system update break my MXNet installation a while back.
    Sadly it's been long enough that I don't recall the exact magic combination of OS/mxnet/perl/ai::mxnet/phase of moon/sacrificial gpu/other That I'd used to get it running.

    If anyone has a currently working installation of AI::MXNet, please let me know what version of all these things is the magic mix I need to replicate.

    Alternately, I did consider witching to AI::TensorFlow, but I haven't been able to figure out how to train a new model...
    all the sample code uses pre-trained networks and I just can't figure out the (too deep for me) magic to train a new network. I'd be more than happy with a simple 'Hello world' (MNIST handwriting) demo if anyone has something to hand.

    Thanks :-)
READDIR and DBI
4 direct replies — Read more / Contribute
by justin423
on Feb 26, 2024 at 11:42
    This code doesn't work, but I have to think there is an easier way to read a file directory into a database, but I just don't see it.
    my $SQL= "INSERT INTO DIRECTORY_LIST(FILENAME) VALUES ('$filename')"; my $sth= $dbh->prepare($SQL) or die "Prepare".$dbh->errstr; opendir my $dh, $dir or die "Could not open '$dir' for reading: $!\n"; $sth-> execute() or die "".$dbh->errstr; while (my $row = readdir $dh) { if ($filename eq '.' or $filename eq '..'){ next; my ($filename)=($row->[0]); } print "$filename\n"; } print "\n"; closedir $dh; $dbh->disconnect;
Solved- Getopt::Long- Trying to specify "prefix" configuration option produces error
1 direct reply — Read more / Contribute
by parv
on Feb 24, 2024 at 18:24

    I was trying to set prefix option for Getopt::Long but that produces the error message as if that option is unknown or I am doing something wrong (what exactly?) ...

    use v5.32; use warnings; use Getopt::Long; printf qq[Version: perl %s, Getopt::Long %s\n], $^V, $Getopt::Long::VERSION; #use Getopt::Long qw[:config debug prefix - ]; # # Only difference is time, compile or run, of error ... # # Getopt::Long: unknown or erroneous config parameter "prefix" # Getopt::Long::Configure( q[debug], q[prefix], q[-] ); __END__ Version: perl v5.36.3, Getopt::Long 2.52 Getopt::Long: unknown or erroneous config parameter "prefix"

    ... So how do I set the prefix string?

    Got the same error with long_prefix_pattern configuration option. A search brings up issues with user options; did not see anything relevant to module configuration.

    Linked to here on Mastodon.

Enforce single hyphen for single character options in Getopt::Long
5 direct replies — Read more / Contribute
by mrnoname1000
on Feb 24, 2024 at 16:22

    Hiya, Perl newbie here! I've been learning it alongside Ruby with my background consisting of mostly shell, C, and Python (the latter of which takes a wholly different approach to argument parsing).

    As I write more Perl scripts with options, I prefer my option parser to be very strictly configured. My problem is that no matter how I configure Getopt::Long, it always accepts single character options with two preceding dashes. I achieved a compromise when rewriting one of my uglier sh+awk scripts, and here's a stripped down example:

    use Getopt::Long qw( :config posix_default gnu_compat bundling no_auto_abbrev no_ignore_case ); my $column = 1; GetOptions( 'column|c=i' => \$column, ) or die; die if $column < 1;

    I want this program to accept exactly two strings as options, --column and -c, but it also accepts --c. I want the prefix for short options to be - and only -, similar to how -- is treated for long options when bundling is enabled. I had hoped qw( :config prefix_pattern - long_prefix_pattern -- ) would get me what I want, but it seems like the latter isn't processed if the former doesn't match. Anyone know of a clean way to achieve this?

    Also, feel free to suggest improvements or mention best practices! If anyone wants, I can post the full script as well; it's intended to format numbers from stdin/files into human-readable sizes like 681.2K, 12.1M, 3.5G. I should probably figure out my preferred license as well...


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":


  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.