Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5963989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:21:05+00:00 2026-05-22T19:21:05+00:00

This one makes no sense to me. I have these two subroutines. sub load_config_file

  • 0

This one makes no sense to me. I have these two subroutines.

sub load_config_file {
    if (@_ eq '') {
        die RED . "No configuration file defined" . RESET . "\n";
    } else {
        if (! -e "@_") {
            die RED . "@_ not found!" . RESET . "\n";
        } else {
            if (`cat @_` eq '') {
                die RED . "$config_file_path is an empty file!" . RESET . "\n\n";
            } else {
                print "Configuration file:" . GREEN . "@_" . RESET . "\n";
                my $xml_obj = XML::Simple->new();
                my $config_xml = $xml_obj->XMLin("@_", SuppressEmpty => 1);
                %config_file = %$config_xml;
            }
        }
    }
} # End load_config_file

sub load_guest_os_file {
    if (@_ eq '') {
        die RED . "No guest operating system file defined" . RESET . "\n";
    } else {
        if (! -e "@_") {
            die RED . "@_ not found!" . RESET . "\n";
        } else {
            if (`cat @_` eq '') {
                die RED . "@_ is an empty file!" . RESET . "\n\n";
           } else {
                print "Guest OS file:" . GREEN . "@_" . RESET . "\n";
                my $xml_obj = XML::Simple->new();
                my $guest_os_xml = $xml_obj->XMLin("@_", SuppressEmpty => 1);
                %guest_os_file = %$guest_os_xml;
            }
        }
    }
} # End load_guest_os_file

Their purpose is to load a specific config file needed for my script. The first one, load_config_file, works perfect. But when I move onto the second one, load_guest_os_file, I get these errors from Perl:

Use of uninitialized value $_[0] in join or string at analyze.pl line 146.
Use of uninitialized value $_[0] in join or string at analyze.pl line 148.

Line 146 in my script is

if (! -e "@_") {

and line 148 is

die RED . "@_ not found!" . RESET . "\n";

What am I missing? When I call the subroutine thus:

load_config_file($config_file_path)
load_guest_os_file($guest_os_file_path)

… the values assigned to those two variables are

my $config_file_path = './config.xml'

and

my $guest_os_file_path = './guest_os.xml'

Edit: I should also add the values for the two variables coming from the command line arguments processed by Getopt::Long. If no value is assigned, the variable is just “declared”, I think that’s the term. I do not assign a value to it, it’s just my $config_file_path; and my $guest_os_file_path;.

Update

Here is the code from the beginning of the script.

#!/usr/bin/perl
use strict;
use warnings;

# Modules to load
use Getopt::Long;
use Term::ANSIColor qw(:constants);
use XML::Simple;
use Net::Ping;
use Net::OpenSSH;
use Data::Dumper;

# Script version
my $version = 'v0.6';

my (%config_file, %guest_os_file, %machines_xml, $ssh_obj);

my @selected_mode;

# Configuration file
my $config_file_path;

# Guest OS file
my $guest_os_file_path;

# Exclusion file
my $exclude_file_path;

# Disables snapshot capture
my $no_snapshots = 0;

my $logfile_path;

my $verbose = 0;

# Program modes
my %program_modes = (
    analyze => \&analyze,
    backup  => \&backup,
    restore  => \&restore,
    help  => \&help,
);

GetOptions(
    'c=s' => \$config_file_path,
    'e=s' => \$exclude_file_path,
    'g=s' => \$guest_os_file_path,
    'l=s' => \$logfile_path,
    'v' => \$verbose,
    'x' => \$no_snapshots,
    'a' => sub { push @selected_mode, "analyze" },
    'b' => sub { push @selected_mode, "backup" },
    'h' => sub { push @selected_mode, "help" },
    'r' => sub { push @selected_mode, "restore" },
    's' => sub { push @selected_mode, "setup" },
);

# Show the help menu if no program mode has been selected
if (@selected_mode == 0) {

    help();

# Throw an error and show the help menu if too many modes are selected
} elsif (@selected_mode > 1) {

    print RED . "Too many program modes specified" . RESET . "\n";

    print "See help menu [-h] for further information\n";

# Run the selected program mode
} elsif (@selected_mode == 1) {

    if ($selected_mode[0] eq 'help') {

        help();

    } else {

        # Die unless user is root
        die RED . "You must be have superuser permissions to run this script" . RESET . "\n" unless ($> == 0);

        system "clear";

        print "Solignis's VMware $selected_mode[0] script $version for ESX\\ESX(i) 4.0+\n";

        load_config_file($config_file_path);

        if ($selected_mode[0] eq 'analyze') {

            load_guest_os_file($guest_os_file_path);

        } else {

            ######

        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:21:06+00:00Added an answer on May 22, 2026 at 7:21 pm

    Some genereal pointers on your code:

    • Consider using elsif instead of the ever nesting else blocks.
    • If you have a bunch of error conditions you’re filtering out, consider using statement modifier if/unless logic.
    • Consider using -z or -s to get your file size ( see http://perldoc.perl.org/functions/-X.html ).
    • Unpack @_ at the top of your subroutines.
    • Minimize use of global variables. Explicitly pass all data in and out of your subs.

    Here’s a cleaned up version of your first sub:

    sub load_config_file {
        my $config_file = shift;
    
        die RED . "No configuration file defined" . RESET . "\n"
            unless defined $config_file;
    
        die RED . "$config_file not found!" . RESET . "\n"
            unless -e $config_file;
    
        die RED . "$config_file_path is an empty file!" . RESET . "\n\n"
            if -z $config_file;
    
    
        print "Configuration file:" . GREEN . "@_" . RESET . "\n";
    
        my $xml_obj = XML::Simple->new();
        my $config_xml = $xml_obj->XMLin("@_", SuppressEmpty => 1);
    
        return $config_xml;
    
    } # End load_config_file
    

    BTW, I am not sure what you have going on with the REDs and RESETs in your die messages, but I have a feeling that it could be better achieved with an exception handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Put value as a field makes sense only if this one represents some object's
i make one class file for jar. and this class have use wurfl. and
I've never experienced this problem. I have two hosting accounts, one with godaddy, and
I am exploring this and see if this one make sense. For instance I
Hope all this makes sense :) I'll clarify via comments if necessary. Also, I
I have two tables in a database. One is for a member and one
I have these two snippets of code (written in Java) which is more more
Say you have these two classes. public class Author { public int ID {get;
This one seems to be a simple problem, but I can't make it work
I am not able to make a clear decision on this one. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.