Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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
catch die from say
3 direct replies — Read more / Contribute
by PierreForget
on Mar 31, 2024 at 17:25
    Hi Monks,

    I have a server which is local (192.168.0.10) and I know mailx works correctly, because I can send an email directly from the terminal, even if I am local. This is the beauty of SimpleSMTP.

    I have a program that sends an email with ssmtp (SimpleSMTP) via mailx. If the email is sent and there is no error, the user gets the sms and the web user gets back to the main menu page. If there is an error (Telus blocks me after a few tries), I tried to catch it with the END statement to send it back to the main menu, but it doesn't work.

    Here is the code, with dummy email and domain: It's part of a sub:

    In the web site logs, I get "mailx: ... message not sent:".

    Thanks in advance.

    $courrielclient="5555555555\msg.telus.com"; $textecourriel = "Ceci est un test de message texte (SMS)"; $sujet = "Test de rappel de rendez-vous $nomfournisseurcellulaire"; $from = "nepasrepondre\@testsms.ca"; open( my $mail, "| mailx -r $from -s $sujet $courrielclient"); say ("$mail $textecourriel"); close $mail; END { my $redirect = "/cgi-bin/chiro/menuprincipal.pl"; print "Location:$redirect\n\n"; }
Tk in Strawberry on W10
3 direct replies — Read more / Contribute
by gsd4me
on Mar 30, 2024 at 18:55

    My apologies if this seems to be old hat but frustration has now become annoying and an internet search has not come up with an appropriate solution:

    I wrote a perl program 2 years ago that worked and did what it was told to/asked to do and it utilised:

    use Tk

    I am now trying to rerun this program but it fails at trying to 'find' Tk - sure enough the module is not in any of the libraries in the path. Why it is no longer there is for another frustrating investigation by Yours Truly

    I have tried re-installing it using ppm, cpan, cpanm (with and without 'forcing') but there is always something that fails

    It also seems that Tk is no longer the simple name for this module but it has a version number as part of the build

    All I want is my simple TK module back so that my loyal program can fulfil its duties once more.

    I am using Strawberry Perl, v5.38.0 on a 64 Windows 10 machine

    Any guidance would be most gratefully received

    ADB
DBI qw(:sql_types) with placeholders versus SQLite3
1 direct reply — Read more / Contribute
by mldvx4
on Mar 30, 2024 at 14:45

    Greetings. I have a question about prepare-execute statements when passing a list of integers.

    I'd like to figure out how to get the first query below ( SELECT recno, body FROM body WHERE recno IN (?); ) to produce the same three db records that the second query produces. Currently Dumper() shows an empty set for the first query but shows the correct three records for the second query. So the first query produces and empty set. The second query produces three records as expected. The difference seems to be a matter of query syntax. I am stumped as to the correct syntax for the prepare statement.

    #!/usr/bin/perl use DBI; use Data::Dumper; use strict; use warnings; my %bodies = (); my $sth; my @recnos = (2284, 2285, 2286); my $dbfile="database-example.sqlite3"; my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", undef, undef, { AutoCommit => 0, RaiseError => 1 }) or die("Could not open database '$dbfile': $!\n"); # this produces an empty set my $query = 'SELECT recno, body FROM body WHERE recno IN (?);'; $sth = $dbh->prepare($query) or die("prepare statement failed: $dbh->errstr()\n"); $sth->execute( join(',', @recnos) ) or die("execute statement failed: $dbh->errstr()\n"); my $bodies = $sth->fetchall_arrayref; print "Part A\n",Dumper($bodies); print "-"x39,"\n"; # this produces the expected three records my $query = 'SELECT recno, body FROM body WHERE recno IN (2284, 2285, +2286);'; $sth = $dbh->prepare($query) or die("prepare statement failed: $dbh->errstr()\n"); $sth->execute( ) or die("execute statement failed: $dbh->errstr()\n"); my $bodies = $sth->fetchall_arrayref; print "Part B\n",Dumper($bodies); $sth->finish; $dbh->disconnect; exit(0);

    A work-around would be to just create a large query string and not worry about the place holder, but somehow I have it in my mind that place holders are the way to go. Sorry, I can't package this one up as a standalone, ready-to-run script as far as I know.

Simple Regex drives me insane
5 direct replies — Read more / Contribute
by cheerydog
on Mar 30, 2024 at 10:30
    Hello dear Perl Monks
    My brain is on strike and I cannot find out the regex with the following properties:

    - the occurence of the string is optional (0 or one occurences)
    - if there the string is present, it has to start with '#'
    - this string should be captured, without this leading mandatory character '#'


    Example:
    'bli bla' -> $1 is empty

    '#foo bli bla'. -> $1 is 'foo'

    Thanks anyways and highest regards

    Cheery
Why is my counter not updating in a threaded application?
2 direct replies — Read more / Contribute
by cormanaz
on Mar 29, 2024 at 14:35
    Good day monks. I have an application that is threaded with Parallel::Forkmanager. I am trying to use a counter to keep track of progress like so (partial code):
    our $ctr = 0; say "processing duplicates..."; LOOP: foreach my $pid (@duplicated) { $pm->start and next LOOP; # do the fork unless ($ctr % 1000) {say $ctr;} $ctr++; #do stuff $pm->finish; # exit the child process }
    The counter is printing, but it's always zero. Why isn't it incrementing properly?
Perl doesn't give error when module is not found 2
5 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 26, 2024 at 06:53
    In Perl I have a script 'a.pl' that calls a module 'MyModule.pm' and inside that module
    there's a call with function 'call()' to another module 'MyModule2.pm' function '$authorize'
    as 'MyModule2::authorize()' but MyModule2 is not loaded with 'use MyModule2' inside MyModule.pm.

    But I get no error "module cannot be found" when running the script 'perl a.pl' which calls 'MyModule.pm' function 'call()' which calls 'MyModule2::authorize()'

    Only if I run 'perl MyModule.pm' I get error "MyModule2.pm not found".

Perl doesn't give error when module is not found
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 26, 2024 at 06:36
    Very simply I don't have a 'use Data::Dumper' line in my script
    but when later on doing 'print Dump $myvar' I get no error!
    Also when doing 'print Dumperrrrrrrrrr $myvar' I get no error too!
    Why is that???????
PERL5LIB not in @INC
3 direct replies — Read more / Contribute
by Bod
on Mar 23, 2024 at 10:56

    I have some common modules that are used in several places so I have created a directory for them at /usr/lib/perl_modules. I want to include this location in @INC for every user, including CRON.

    I've added export PERL5LIB=/usr/lib/perl_modules in both /etc/environment and /etc/profile.

    When I list the environment variables, PERL5LIB is there as expected. But when I try to use on of the modules I get an error:

    Can't locate my_module.pm in @INC (you may need to install the my_modu +le module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/ +perl/5.36.0 /usr/local/share/perl/5.36.0)
    There are other locations in @INC but /usr/lib/perl_modules is not one of them...

    I suspect the environment variable is set for root that I'm using to list the variables, but not for whatever process is running the script within Apache.

    How can I properly set PERL5LIB for all users and processes or is there a better way to get an extra entry in @INC for every script without having to use lib in every script?

boot_DynaLoader: what, where, how
2 direct replies — Read more / Contribute
by rodd
on Mar 23, 2024 at 09:26
    Respectable keepers of knowledge:

    I'm working on binding some JS and Perl code using Bun FFI (https://bun.sh/docs/api/ffi) and perlembed. Things work good, except for XS dynamic loading. Probably due to the way the Bun FFI links to the boot_DynaLoader in my libperl.so, and the general lack of tools and knowledge on my part to circumvent, emulate or recode some of those parts.

    So, my question: where or how in core is the libperl boot_DynaLoader C function defined? I just can't find it! I've been to the Perl core code and docs on XS, boot_DynaLoader and ExtUtils::Embed, but to my surprise I can't figure how it works or where its code resides in core (my core is 5.22.1).

    Any leads, links or pointers to help me acquire the knowledge of the inner workings of dynamic loading would be of great help.

    PD: I didn't want to bore you with my code, but here it is, for context:

    const xsInit = new JSCallback( (pTHX) => { Perl.Perl_newXS( perl, pstr('DynaLoader::boot_DynaLoader'), Perl.boot_DynaLoader.ptr, pstr(import.meta.file) ); }, { args: ['ptr'] } ); const rc = Perl.perl_parse(perl, xsInit.ptr, argc, argv, envp);

    The above does not dynamic loads XS modules as intended due to the way the FFI library exports symbols, which are meant primarily to be used within the JS realm, not to be sent back to libperl as a callback or (void *)() pointers. I've tried some other workarounds and practices, ie using my own compiled xs_init() function from C and FFI linking to that, but loading is still not working. I probably will be able to solve it at some point, but still, I'd like to understand the inner workings of the core in that regard. I got it working on darwin x86 by accident, but the same workaround fails in linux.

Holding site variables
7 direct replies — Read more / Contribute
by Bod
on Mar 21, 2024 at 06:39

    We operate a number of websites, all of which operate on the same server.

    Currently, I am the only developer. But that is likely to change over the next 18 months or so. I'm making some changes that present the opportunity to make some improvements to the internal design and security of the sites. I'm looking for some input on the "best" way to do this. Any input welcome but especially around global site variables.

    Currently we have this directory structure (plus a few others omitted for simplicity:

    site/prod/bin/ site/prod/lib/ site/prod/template/ site/prod/www/ site/test/bin/ site/test/lib/ site/test/template/ site/test/www/

    Every site has identical code in prod and test (except for during development of course) except for one file site/lib/vars.pm which declares the site variables needed for that site and environment. Things like the DB credentials, the DB instance to connect to, Stripe keys, API keys, etc.

    use strict; use warnings; our $env_db_user = 'dbusername'; our $env_db_pass = 'dbpassword'; our $env_paypal = 'PP username'; # etc, etc, etc
    There is no logic code in this module, it just defines variables with our. This module is used by a utility module that is used by every script on the website.

    When we bring another developer onboard, I want to split the site variables into two - those they have access to (test database schema name, text Stripe keys, etc) and those they don't (live Stripe keys, database credentials, etc). I could relocate this file to further up the directory structure where they don't have access, but I feel sure there is a better way to handle this as it must be a common problem in multi-developer environments.

    What I have works well and it not in need of imminent change. But I have opportunity to make it more robust as I am making other changes.

    What advise can you give on this matter kind and wise Monks?


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 about the Monastery: (3)
As of 2024-04-26 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found