Question

My script :

#!/usr/local/bin/perl
use POSIX qw(strftime);
use strict;
use warnings;
use Getopt::Long;

##input parameters

my ($artifact,$package_id);
GetOptions("artifact=s" =>\$artifact,
       "package_id=i" => \$package_id);
if($artifact =~ /\.zip$/i)
    {
    chdir("/apps/checkout/artifactory/xxkintana/$package_id");
    unzip  $artifact;
 }

run :

./script.pl 4370177 test-1.0.zip

Error :

Use of uninitialized value in pattern match (m//) at ./script.pl line 21.

Please help me that to find whether its zip file or not. input file like test-1.0.zip

-Thnaks

Was it helpful?

Solution

You're checking to see if $artifact ends in .zip, but you don't actually set it when calling the script.

./script.pl --package_id=4370177 --artifact=test-1.0.zip

I would suggest cleaning up the parsing of your options, so that you actually do some error checking.

#!/usr/local/bin/perl

use strict;
use warnings;

use POSIX qw(strftime);
use Getopt::Long;

##input parameters

GetOptions(
    "artifact=s"   => \my $artifact,
    "package_id=i" => \my $package_id,
) or usage();

usage("Unknown arguments: @ARGV\n") if @ARGV;
usage("--artifact is required\n") if ! $artifact;
usage("--package_id is required\n") if ! $package_id;

if ($artifact =~ /\.zip$/i) {
    chdir("/apps/checkout/artifactory/xxkintana/$package_id");
    unzip  $artifact;
}

sub usage {
    my $message = shift;
    die "${message}Usage: ./$0 --artifact=<artifact> --package_id=<id>\n";
}

Finally, I would probably make additional error checking to ensure that the directory you're trying to chdir exists, that $artifact exists, and throw appropriate error messages for those and any more cases that you can think of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top