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:

package Script::Singleton; use strict; use warnings; use Cwd qw(abs_path); use IPC::Shareable; our $VERSION = '0.03'; sub import { my ($class, %params) = @_; $params{glue} = abs_path((caller())[1]) if ! exists $params{glue}; IPC::Shareable->singleton($params{glue}, $params{warn}); } sub __placeholder {} 1;

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:

#!/usr/bin/perl use warnings; use strict; require '/home/demo/pullWorkload/scpLRSIControllerDir/toys/Singleton.p +m'; use Script::Singleton; # Line 6 :: Actually run the code in "Si +ngleton.pm" ??? package main; print "Starting script...\n"; sleep(10); print "I can use \"Singleton.pm!\".\n";

But script output is:

me@/path/to/my/scripts/$ ./runTest.perl Can't locate Script/Singleton.pm in @INC (you may need to install the +Script::Singleton module) (@INC contains: /etc/perl /usr/local/lib/x8 +6_64-linux-gnu/perl/5.30.0 /usr/local/share/perl/5.30.0 /usr/lib/x86_ +64-linux-gnu/perl5/5.30 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/pe +rl/5.30 /usr/share/perl/5.30 /usr/local/lib/site_perl /usr/lib/x86_64 +-linux-gnu/perl-base) at ./toy02.perl line 6. BEGIN failed--compilation aborted at ./runTest.perl line 6. me@/path/to/my/scripts/$

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

In reply to Modules 101 :: Using a Local Module from CPAN by redapplesonly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.