Shubhra Garg
Shubhra Garg

Reputation: 167

How to make use of UUIDGEN of unix in perl script

I am trying to generate a unique number using uuidgen (of unix). The generated unique number should get stored in a variable. When i am doing it in a function , I am facing errors.

Can anyone let me know how to make use of uuidgen script in perl.

    #!/usr/bin/perl
    sub function_uuidgen
    {
       my myuuid;

       system(`export myuuid=uuidgen`);
       print "\n unique id is : $myuuid";

       # I need not to export the variable, I just need to unique number generated by UUID GENERATOR in a variable.
     }

     int main
     {
        print "\n I am in main function";
        &function_uuidgen;
     }

I am facing the below error when I am running uuidgen as mentioned below. Can anybody help me out with exporting the JAVA VARIABLE in perl ? How to export the path variable,in case if this error is related to that.

Error :

         /bin/java: uuidgen 1: not found

Code :

   sub function_uuidgen
    {

   my $myuuid = qx(uuidgen);
   chomp $myuuid;

   print "\n unique id is : $myuuid";

   # I need to export the variable, as it is giving me error without exporting.
    }

   int main
   {
    print "\n I am in main function";
    function_uuidgen();
   }

Upvotes: 0

Views: 2167

Answers (4)

cdarke
cdarke

Reputation: 44394

export is a shell command that adds a variable and value to its environment block. The environment block is private to a process, but (by default) copied to a child process. You appear to be thinking it is some sort of global area - it is not.

So, all you would be doing is adding a value to the shell's environment block, not your own! (That's the shell created by system(), not the one you were running from). Placing the export inside back-ticks is strange, if not wrong.

Easier to use:

my $myuuid = qx(uuidgen);
chomp $myuuid;

Notice I am using qx instead of back-ticks `` because they can be confusing (back-ticks are deprecated in UNIX shells as well).

To run the subroutine, loose the C style int main:

print "\n I am in ", __PACKAGE__, "package\n";
function_uuidgen();

The leading & on a subroutine call has side-effects that you probably don't need.

Upvotes: 2

Demnogonis
Demnogonis

Reputation: 3222

You're mixing up C and Perl here. As far as I know you can't access exportet system variables that way from perl (correct me if I am wrong. I don't have much knowledge of linux system variables).

A way to generate UUID's would be the Data::GUID Module from CPAN

use strict;
use warnings;
use Data::GUID qw( guid_string );

my $guid = guid_string();
print "$guid\n";

Also Perl doesn't have an int main function. Your code starts at the top and runs down to the bottom. Of course this gets a bit different if you create an object orientated module.

Upvotes: 7

asjo
asjo

Reputation: 3194

If you for some reason can't use Data::GUID, this is a way to use the output of uuidgen (note the backticks):

#!/usr/bin/perl

use strict;
use warnings;

my $uuid=`uuidgen`;
chomp $uuid;

print "$uuid\n";

Example output:

$ ./hep.pl 
fe82c4f6-a1f2-4242-ab45-853780931927
$ 

Also, using & before function calls went out of fashion many years ago :-)

Upvotes: 2

DeVadder
DeVadder

Reputation: 1404

Without knowing anything about uuidgen: You could just

my $perlVar = `uuidgen`;

within perl. Assuming calling uuidgen in your console returns the number you are looking for.

Upvotes: 2

Related Questions