![]() |
|
The stupid question is the question not asked | |
PerlMonks |
Modules 101 :: Using a Local Module from CPANby redapplesonly (Sexton) |
on Dec 06, 2022 at 17:10 UTC ( #11148616=perlquestion: print w/replies, xml ) | Need Help?? |
redapplesonly has asked for the wisdom of the Perl Monks concerning the following question: Hi Perl Monks, So I'm a Perl newbie, making lots of rookie mistakes. I've made two posts on this website* already in my fumbling attempts to solve a singular problem: I need a way to ensure only one instance of my Perl script can run at a time. Stevieb was kind enough to recommend his Script::Singleton solution, but I was too much of a Perl beginner to realize what he'd handed to me at first. It was a bolt of lightning when I realized that Script::Singleton was another script, and all I needed to do was copy that script from the CPAN.org site**, save it in a local directory, and then have my script run his script. Easy Peasy. Except, I'm struggling at that "have my script run his script part." (I think this is prob something you Perl Ninja Black Belts do all the time, so you can discuss the process in shorthand. But for newbs like me, the process is confusing.) (FYI, I'm developing on a Ubuntu 20.04 machine, using Perl 5.30.0) Let me show you actual code. First, here's Stevieb's script:
I've saved this script in the same directory as Singleton.pm, plus run a chmod 775 Singleton.pm on it, just to be sure. Stevieb's script should be good to go. Now for the part where I'm all thumbs. I've read through a few "Perl Module" tutorials, trying to puzzle how my script can use or require the module script. Here was what I thought would be the best attempt:
But script output is:
Okay, obviously my script is searching throughout all the directories in @INC for Singleton.pm, but not looking in the local directory. Frustrating. If things were working correctly, I'd hope that the first time the script runs, it would execute Stevieb's code, confirm that it is the only instance of this script running, then sleep for 10 seconds. In theory, a second execution of my script before those 10 seconds are up should... I dunno, not execute at all? If I comment out Line 6 (which I thought was necessary to actually run Stevieb's code), then my script runs with no errors. But you can run multiple instances of my script at the same time, which is no bueno. Anyway, I apologize for (A) essentially asking you guys the same question three times in a row, and (B) asking a question which is probably Perl 101 to you guys. I do research my questions before I post them, FWIW. Any comments or criticism is appreciated. Thank you! *Full Disclosure :: My previous posts are: Ensure Only One Instance of Your Script is Running... with 'ps -ef' ?https://www.perlmonks.org/?node_id=11148582
Mechanism for ensuring only one instance of a Perl script can only run?
https://www.perlmonks.org/?node_id=11148499
**The CPAN site where I found Stevieb's code is located:
https://metacpan.org/dist/Script-Singleton/source/lib/Script/Singleton.pm
Back to
Seekers of Perl Wisdom
|
|