Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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
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?

Does each DBI 'do' start a new transaction?
3 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 19, 2024 at 10:12

    The following code when the 'update stock' condition returns 0, executes sql_error()'s sql 'insert into sql_error' AND dosql()'s 'insert into stock_valid' ,despite that the first statement of sql_error() is $dbh->rollback. I know that wrapping do_sql()'s sql statements in $dbh->begin_work would probably make it work, but I'm interested in the background of why it is happening.

    I have a theory that every 'do' statement does start a new implicit transaction. So let's say that do(insert into stock_valid) is T1 and do(update stock) is T2. Then, T1 is rolled back when the rollback of sql_error() is hit, while T2 is not rolled back and instead is committed when $dbh->commit; of sql_error() is hit.

    My 2cents. What do you think?

    $dbh->{AutoCommit}=0; $dbh->{RaiseError}=1; $dbh->{HandleError}=\&handle_error; sub do_sql() { $dbh->do(qq{ insert into stock_valid (last_year,orderid,suppliervatno, code,executedquantity,productprice) values ( $last_year,$orderid,$suppliervatno, $code,$executedquantity,$productprice ) }); my $rowsupdated=$dbh->do(qq{ update stock set quantity=quantity+$executedquantity where code=$code }); if ($rowsupdated==0) { sql_error($order,'no rows found','LocalError'); } $dbh->commit; $dbh->disconnect; } sub sql_error() { $dbh->rollback; my $orderid=$dbh->quote(shift); my $resultreason=$dbh->quote(shift); my $resultcode=$dbh->quote(shift); $dbh->do(qq{ insert into sql_error (orderid,resultreason,resultcode) values ( $orderid,$resultreason,$resultcode ) }); $dbh->commit; $dbh->disconnect; die; }

    Grandfather replaced pre tags with Code tags and fixed paragraphing.

Lexical variable not available
6 direct replies — Read more / Contribute
by choroba
on Mar 18, 2024 at 17:06
    Copied from one of the Perl Discords:

    By user "timka7060":

    Who can figure this out. I narrowed down my issue in some other code to this simple use case:
    # Can print $v only once!!! # Second call returns undef. # Some coderef optimization??? # Lexical variable goes undef on second call. # sub { my $v = 111; eval q( eval 'print $v, "\n"'; # 111 eval 'print $v, "\n"'; # undef ); }->();

    User "tyrrminal" replies with:

    If you enable warnings, you see a line Variable "$v" is not available at (eval 3) line 1. Googling that led me to a stack overflow response which seems to relate to your case (2nd answer, starting with See perldiag)

    timka7060's reaction:

    So why does the first eval output 111?
    (I'd figure either both lines should be 111 or both undef.)
    Btw, prior to 5.10, both lines would output 111: https://perlbanjo.com/b28dce8db2
    This is a simple fix, but only for a single variable: https://perlbanjo.com/13dbb8d48d

    My observation:

    sub { my $v = 111; eval q( eval 'print $v, "A\n"'; eval 'print $v, "B\n"'; print $v, "C\n"; ); }->();
    Now, none of either A or B sees $v, but C (obviously) does.

    What's going on here?

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
MTA for Perl
5 direct replies — Read more / Contribute
by Bod
on Mar 18, 2024 at 16:51

    I need to install a Mail Transfer Agent that will be used with MIME::Lite

    I understand that the modern choices are Postfix or Exim. However, MIME::Lite says it uses sendmail which is pretty old isn't it?

    I have never looked at MTA's before so any advice would be appreciated. This is being installed on a RaspberryPi. Does the MTA only deal with outgoing mail or will it manage incoming mail as well? If not, what do I need for that?

Common subs and Global Variables
5 direct replies — Read more / Contribute
by Anonymous Monk
on Mar 18, 2024 at 15:56
    In the course of writing a bunch of perl code for my firm, I noticed that some subs were used over an over and some variables were most naturally implemented as global variables.

    After some experimentation I hit upon a method of making this happen. Let "program.pl" be one of the perl programs which needs to use (a) one of these common subs and (b) needs to share some variables with this sub.

    Context: This is in Windows 10 (ugh) using Strawberry perl v5.20.2.

    I construct a file "includee.pm" which looks like this:

    use vars qw { $SHAREDVARIABLE1 @SHAREDVARIABLE2 %SHAREDVARIABLE3 ... } sub function { ... }
    Then in program.pl I put at the top this line:

    use includee
    Both program.pl and includee.pm are in the same folder, which is also the current folder when the program runs.

    This actually produces the desired effect: the shared variables are accessible in both progam.pl and includee.pm, and the sub function is callable from program.pl.

    The question is: is this legitimate? Am I depending on a quirk, which might go away in a future version of perl? In particular, will this method work in a linux context also?

Exact definition of DateCalc and Delta_Format functions
1 direct reply — Read more / Contribute
by zatlas1
on Mar 18, 2024 at 14:20
    I am looking for the exact definition of DateCalc and Delta_Format functions in Date::Manip and I seem not to be able to find those definitions. Could somebody please point me to those definitions. Thank you ZA

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 pondering the Monastery: (5)
As of 2024-03-28 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found