-2

I have a perl script that connects to the Plex API. It logs in and performs certain actions (mostly working).

However, the Plex API suggests (insists?) that each instance of the script send a unique ID, such that if I share this script with anyone else they should use a different string.

In the interests of keeping this simple, I don't want to have some configuration file that keeps that value outside of the script. I also can't leave a value hard-coded in, no one who downloads this will change it.

Could the perl script modify itself?

If I were to declare it as such:

my $uuid = 1;

... then could I not check immediately afterward if this value is equal to 1, and if so overwrite that with a randomly generated uuid? The script would then exit, but somehow re-invoke itself (so the user doesn't have to run it a second time).

Is there a safe way to do this? Alternatively, is there a better way to accomplish the goal without using this method?

John O
  • 4,863
  • 8
  • 45
  • 78

3 Answers3

1

Make the last line of your script __DATA__ and append the ID to the script either at installation or first run. Reading from the special <DATA> handle reads the data segment of a script.

stark
  • 12,615
  • 3
  • 33
  • 50
0

You could use UUID::Tiny to generate a random UUID:

use UUID::Tiny;   
my $uuid = create_UUID(UUID_V4);

To preserve the UUID between invocations, you'll have to modify the script itself. The answers in this thread might be helpful.

Community
  • 1
  • 1
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • This doesn't accomplish the task. Each time it runs, it will get a new value. It needs to be the same value for any given user, every time they run it. But it needs to be a different value for every user. Low-overhead, single file script. – John O Jun 21 '16 at 21:57
  • 1
    @JohnO Generate the ID as part of the installation process. – ThisSuitIsBlackNot Jun 21 '16 at 21:59
  • It's not anything that requires an "installation process". It's a single-file script fewer than 150 lines of code. Half of that comments and whitespace. That's overkill. – John O Jun 21 '16 at 23:34
  • @JohnO Then generate it the first time the script is run and store it, either in the script itself or in a separate file (my preference). – ThisSuitIsBlackNot Jun 22 '16 at 00:02
  • Generating it the first time is easy. Storing it... I was asking how to do that creatively. Even if impractical, it was an interesting idea but I'm not sure how to do it. – John O Jun 22 '16 at 00:18
  • @JohnO The easiest way would be: if ID file exists, read ID from file; if not, generate new ID and write to file. – ThisSuitIsBlackNot Jun 22 '16 at 00:26
0

Update

You say in the comments that you want a different unique ID "per installation", but you also say that "it needs to be the same value for any given user", so I'm no longer sure that my answer will satisfy your requirements


I suggest that you use the system UUID returned by dmidecode. Of course you will need to have it installed on your computer, and there's a parser module for it on CPAN called Parse::DMIDecode

It's slightly more complex if you have to support Windows systems. You can use DmiDecode for Windows, which is available as a ready-built binary, but the parser module explicitly checks that there are no colons (amongst other things) in the path to the demidecode executable, so a call to the probe method won't work. Instead you must call demidecode and pass the result to the parse method

This short example works fine on both Linux and Windows

use strict;
use warnings 'all';
use feature 'say';

use Parse::DMIDecode;

my $decoder = Parse::DMIDecode->new;
$decoder->parse(qx{dmidecode});

say $decoder->keyword('system-uuid');

output

35304535-3439-4344-3232-3245FFFFFFFF
Community
  • 1
  • 1
Borodin
  • 126,100
  • 9
  • 70
  • 144