Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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
problem with returning all records
2 direct replies — Read more / Contribute
by frank1
on Apr 16, 2024 at 06:30

    My script below works, but i have one problem, it just display out 1 record

    What i need is to return all records from database

    my $sth = $dbh->prepare("SELECT snd.name, snd.country, m.item, m.eqty, m.price, m.line, m.prodct FRO +M buys as m JOIN Busers as snd ON snd.id = m.buyer WHERE CASE WHEN (SELECT SUM(s +tatus = ?) FROM buys) > 0 THEN m.status = ? ELSE m.status = ? OR m. +status = ? OR m.status = ? END ORDER BY created ASC"); $sth->execute('sold', 'forsell', 'bugt', 'paid', 'inshop'); my $Data = $sth->fetchall_arrayref(); foreach my $Data (@$Data) { my ($name, $country, $item, $eqty, $price, $line, $prodct) = @$Dat +a; $html = "<tr> <td> <p>$name</p> <p> $eqty, $price, $line, </p> $prodct $country </td> </tr> "; }
External (extra) files when using Inline::CPP
1 direct reply — Read more / Contribute
by cavac
on Apr 16, 2024 at 05:52

    First of all, i have to admit that i feel that i should know this already, but i just don't.

    Is it possible to add external cpp/h files when using Inline::CPP? I only need XS bindings for the stuff i'm inlining, the rest only needs to be accesible through the inlined CPP code.

    Here is the simplest example i could tink of.

    test.pl
    use Inline CPP; for(1..3) { print "Perfect random number: ", cast_die(), "\n"; } __END__ __CPP__ #include "xkcd.h" int cast_die() { int rolled = xkcd_dice_roll(); return rolled; }
    xkcd.h
    #ifndef XKCD_DICE #define XKCD_DICE // See https://xkcd.com/221/ for why this is absolutely perfect #define FAIRLY_GENERATED_RANDOM_NUMBER 4 int xkcd_dice_roll(); #endif
    xkcd.cpp
    #include "xkcd.h" int xkcd_dice_roll() { return FAIRLY_GENERATED_RANDOM_NUMBER; }

    Is something like this even possible with Inline::CPP?

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Running modulino inside BEGIN
3 direct replies — Read more / Contribute
by LanX
on Apr 14, 2024 at 13:22
    Hi

    while developing a module I like to run (F5) the testsuite automatically. I'm using a moulino approach which works considerably well

    #BEGIN { # TODO unless ( caller() ) { # Modulino for development exec q(cd ../..; prove ./t/); #run proove from dist root } #}

    It occurred to me that I have to enclose this inside a BEGIN block to avoid the overhead and side-effects of loding all modules with use (this happens in the BEGIN phase) before the unless is executed at run time.

    This doesn't work, since caller() IS defined inside BEGIN

    BEGIN { warn "Caller Begin: ",defined caller(0) } warn "Caller Normal: ",defined caller();

    Caller Begin: 1 at /home/lanx/... Caller Normal: at /home/lanx/...

    EDIT Solution

    while writing this I realized that BEGIN is called 2 levels down, hence caller(2) seems to work

    BEGIN { # TODO unless ( caller(2) ) { # Modulino for development exec q(cd ../..; prove ./t/); #run proove from dist root } }

    Is this reliable?

    (I think it is, but I already wrote this post, before finding the solution and the monastery is not overwhelmed with writeups :)

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

CPAN test suites with SQL
1 direct reply — Read more / Contribute
by LanX
on Apr 14, 2024 at 06:22
    Hi

    I'm developing a DBIx distribution and need to check plenty of SQL in the tests.

    My approach is to use SQLite as primary engine, since it comes with the DBD::SQLite module installed and that's not much of a prerequisite (it's even comes bundled with Strawberry Perl!)

    My issue is where to put the temporary SQLite file...

    • in the distribution's /t directory?
    • in the FS's /tmp ? What about other OSes?
    • or should I restrict the SQL-testing to the authors system

    Those SQLite files don't become big, but I'm not sure about writing stuff into the dist directory. On a side note:

    I want to be able to switch to other DB-Servers while testing. (But now really author's side unless explicitly wanted by the user)

    What's the best approach to make this configurable?

    • A prove option
    • an ENV setting
    • a prompt while testing? (DBI can test for available DBD:: drivers)

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

How to use a splice inside a foreach
3 direct replies — Read more / Contribute
by Veltro
on Apr 13, 2024 at 03:55

    I found out that I left some unaltered code that I programmed 10 years ago and it went unnoticed. I knew this bug and I actually fixed it quick and dirty in multiple places but I never found a good fix for it.

    What is the best way to use the splice inside of a loop like this (foreach, or other)

    Original code bugged:

    sub filterExclude{ my $ar = $_[0] ; # REF! my $someConfig = $_[1] ; my $ix = 0 ; foreach ( @{$ar} ) { my $sKey = $_ ; if ( exists ( $someConfig ->{ $sKey }->{ Exclude} ) && $someConf +ig ->{ $sKey }->{ Exclude} ) { splice ( @{$ar}, $ix, 1 ) ; $ix = $ix - 1 ; } $ix = $ix + 1 ; } }

    Right now I have:

    sub filterExclude{ # 20240414 Quick fix needed because of the splice my $ar = $_[0] ; # REF! my $someConfig = $_[1] ; my $tmpCheckedEverything = 0 ; while ( !($tmpCheckedEverything) ) { $tmpCheckedEverything = 1 ; my $ix = 0 ; foreach ( @{$ar} ) { my $sKey = $_ ; if ( exists ( $someConfig ->{ $sKey }->{ Exclude} ) && $someC +onfig ->{ $sKey }->{ Exclude} ) { splice ( @{$ar}, $ix, 1 ) ; $tmpCheckedEverything = 0 ; } $ix = $ix + 1 ; } }

    But of course this is ugly and not efficient

Tk Entry & Right Single Quote
3 direct replies — Read more / Contribute
by cmv
on Apr 11, 2024 at 14:39
    Hi Monks-

    I have users who are doing cut-n-paste into a Tk Entry widget in my perl app and causing some issues.

    Apparently things like Outlook, Notepad, etc, can be configured to change an ASCII single quote (') into smart quotes aka Unicode Right Single Quote and Left Single Quote. When they cut-n-paste this into my entry widget, I see goofy things happen later in my script. I'm including a test script below, note the difference between what shows up in the message box and what the print STDERR outputs.

    What is going on here, and what are some intelligent ways for me to handle it?

    I think I want to simply translate any unicode single quotes (left or right) to the ASCII single quote - no? What if they start pasting other unicode characters? Looking for experience & guidance here.

    Thanks

    -Craig

    #!/opt/homebrew/bin/perl use strict; use warnings; use Tk; my $main = new Tk::MainWindow(); my $entry_test = $main->Entry(-text => "single-quotes: ’'")->pack(); my $btn_test = $main->Button( -text => ' Test ', -command => sub { my $text = $entry_test- +>get(); &msg($main, $text) if ( +$text); } )->pack(); sub msg { my ($parent, $msg) = @_; print STDERR "Messaging: $msg\n"; $parent->messageBox( -title => 'Event', -message => $msg, -type => ' +ok', -icon => 'info' ); } MainLoop();
namespaces in XML::RSS::Parser::Element
2 direct replies — Read more / Contribute
by wintermute_115
on Apr 11, 2024 at 08:38

    I'm trying to parse RSS feeds with XML::RSS::Parser, and it's mostly working fine, except that I need to be able to pull the content of elements in the itunes namespace and nothing I can think of seems to work.

    If I'm trying to read the element itunes:summary (it will be en object of type XML::RSS::Parser::Element), for example, I've tried:

    $summary = $element->query('itunes:summary'); $summary = $element->query('{http://www.itunes.com/dtds/podcast-1.0.dt +d}summary'); $summary = $element->query('summary') $summary = $element->query(process_name('itunes:summary'): $summary = $element->query(process_name('{http://www.itunes.com/dtds/p +odcast-1.0.dtd}summary'):
    and some other things I don't remember. But everything I try, it ether returns undef or I get told Bad call to match: '<XXX>' contains unknown token.

    Does anyone know how this works? Thanks.

Unexpected line breaks in substitution results
2 direct replies — Read more / Contribute
by paschacroutt
on Apr 10, 2024 at 10:22
    Hi monks,

    What follows is my very first adventure in perl world, so please be merciful.

    I wrote the following code to modify a bunch of pandoc-generated DokuWiki formatted files in order to convert some expressions to DokuWiki internal links. That went through many iterations and I am now somewhat satisfied with the result except for one mystery I am unable to pierce through.
    It does not crash my code, nor does it sends any warning, but I can't explain this result.

    Here's my problem:

    In this substitution part of the code: .(defined($4) ? $4 =~ tr[\/*][]dr : '.') line 44, I check if the value of $4 is defined and if it is, I apply a tr to it to remove italic (//) or bold (**) marks if they are present. If $4 is undefined (meaning there are no dot, comma or semicolon after the last word of the expression), the conditional operator sends a dot to end the substitution.

    So either this data entry:

    **Voir :** proton, solution hydrogénée//.//
    or that one:
    **Voir :** proton, solution hydrogénée.
    Give this result:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee|solution hydrogénée]].
    Which is exactly what is needed.
    But if there is no final dot or comma after solution hydrogénée:
    **Voir :** proton, solution hydrogénée
    What I get is:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee |solution hydrogénée ]].
    With line breaks after solution_hydrogene and hydrogénée
    Which is ok but I was expecting:
    **Voir :** [[glossaire:entrees:p:proton|proton]], [[glossaire:entrees: +s:solution_hydrogenee|solution hydrogénée]].
    No line breaks.
    And I don't understand why and how those line breaks get inserted.
    Actually, those breaks don't really matter, as DokuWiki format being some kind of lesser markdown, it just gives the same html output with or without them, but it worries me as a sign of my incomplete understanding of my own code.

    It may have something to do with the /x modifier I suspect.

    The actual code follows

    Thanks for reading through !

replacement for Auth::GoogleAuth
1 direct reply — Read more / Contribute
by ForgotPasswordAgain
on Apr 09, 2024 at 12:24
    We currently use qr_code of Auth::GoogleAuth to generate QR codes for Google Authenticator, but recently found out that the underlying Google Image Charts was deprecated in 2012... (apparently it's periodically shut off to "notify" users of it going away). Anyone know of an easy replacement or have any recommendations?
Parallel::ForkManager install fails
5 direct replies — Read more / Contribute
by cormanaz
on Apr 07, 2024 at 16:30
    When I try to install Parallel::ForkManager on Windoze 10 it fails:
    Test Summary Report ------------------- t/start_child.t (Wstat: 1280 Tests: 0 Failed: 0) Non-zero exit status: 5 Parse errors: Bad plan. You planned 2 tests but ran 0. t/waitpid-conflict.t (Wstat: 0 Tests: 2 Failed: 0) TODO passed: 1-2 t/waitpid-waitonechild.t (Wstat: 0 Tests: 3 Failed: 0) TODO passed: 2-3 Files=12, Tests=26, 74 wallclock secs ( 0.05 usr + 0.02 sys = 0.06 C +PU) Result: FAIL Failed 1/12 test programs. 0/26 subtests failed. dmake.exe: Error code 255, while making 'test_dynamic' -> FAIL Installing Parallel::ForkManager failed. See C:\Users\boss\.cp +anm\work\1712513913.232104\build.log for details. Retry with --force +to force install it. Expiring 2 work directories.
    The docs list no test results of the current version on Windoze. Is there anything I can do to fix this?

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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-16 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found