Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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 cooling their heels in the Monastery: (4)
As of 2024-04-23 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found