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 3787996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:55:44+00:00 2026-05-19T11:55:44+00:00

Sorry for not being clear. The problem I am trying to convey is that

  • 0

Sorry for not being clear.
The problem I am trying to convey is that XML::Simple does not allow me direct access to the hash, I must refer to the key in question through the XML::Simple module. Using something like $xml->{key}.

Here is the code I am using and a copy of the hash output.

The reason this is a problem is because I need to construct a series of loops using the keys of the hash [seen below], and I cannot build a foreach loop with a hash referrence, when I tried it, perl gave me an error.

So what I am looking for is a module or solution to allow me to dump the contents of the XML file to a hash that I declare in my script. I also need the ability to write back out to an XML file.

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

system ("clear");

my $xml = XML::Simple->new;

# Using the XML::Simple object, read guest_os.xml to a hash

my $xml_file = $xml->XMLin('config.xml',

                SearchPath => './config',

                SuppressEmpty => 1);




$VAR1 = \{
        'esxi01' => {
                    'password' => 'myspoonistoobig!',
                    'user' => 'root',
                    'port' => '22'
                  },
        'esxi02' => {
                    'password' => 'myspoonistoobig!',
                    'user' => 'root',
                    'port' => '22'
                  },
        'Setup' => 'FALSE'
      };

For me to access a single key/values in the hash that is created [above] I have to use something like this.

$xml_file->{esxi01}{password}.

Oh yeah I forgot the actual XML file.

<Config>
  <esxi01>
    <password>password</password>
    <port>22</port>
    <user>root</user>
  </esxi01>
  <esxi02>
    <password>password</password>
    <port>22</port>
    <user>root</user>
  </esxi02>
  <Setup>FALSE</Setup>
</Config>

This is the simplest of the 3 or so I have.

Update:

The first part of the loop works with no problem, the problem happens when I try to use the second part of the loop.

Here is the way my code looks

my $xml = XML::Simple->new;

# Using the XML::Simple object, read guest_os.xml to a hash

my $xml_file = $xml->XMLin('config.xml',

                SearchPath => './config',

                SuppressEmpty => 1);

foreach my $server (keys %$xml_file) {
    foreach my $attribute (keys %{$xml_file->{$server}}) {
        print "$attribute\n";
    }
}

The output looks like this,

Can't use string ("password") as a HASH ref while "strict refs" in use at foreach_test line 21.

I have tried using quotes in several places to fix the problem but nothing seems to work.

  • 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-19T11:55:45+00:00Added an answer on May 19, 2026 at 11:55 am

    I’m not sure if I understand what you need correctly – the question is very confusing. What you SEEM to be asking is:

    • You have a complicated nested XML. E.g. <t1><t2><t3>value</t3></t2></t1>

    • You seem to want to access contents of tag <t3> but without needing to go through the hash keys in a hash-of-hash-of… data structure that XML::Simple creates to represent the DOM. E.g. you don’t want to do $xml->{t1}->{t2}->{t3}.

    If that’s the case, your problem is that you are using a DOM parser. A SAX parser (like XML::Twig) lets you execute events upon parsing specific tags, e.g. t3 no matter where in the sructure that tag is.


    If your problem is that you just don’t know which tags exist (as keys in the hash),
    you can do keys %$xml or keys %{ $xml->{t1} } to list them.

    UPDATE

    To loop using your example:

    foreach my $key (keys %$xml) { # $key will be esxi01, etc...
        foreach my $attribute (keys %{ $xml->{$key} }) {
            "Key: $key; attribute: $attribute; value: $xml->{$key}->{$attribute}\n";
        }
        print "Port for $key: $xml->{$key}->{port}\n"; # Hardcoded
    }
    

    UPDATE2

    The problem is in the <Setup>FALSE</Setup> XML tag. It has no subtags, so in the hash its value will be a SCALAR and not a hash. To fix, you need to check that the inner element is, indeed, a hash:

    foreach my $server (keys %$xml_file) {
        next if (ref ($xml_file->{$server}) ne ref({})); # Not a hash
        foreach my $attribute (keys %{$xml_file->{$server}}) {
            print "$attribute\n";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if I'm being too clear with the title. Sorry, it's late and
Sorry the title is not very clear. This is a follow up to my
Sorry if question is not clear. My vocabulary for terminology is bad. Anyways I
Sorry if the title is not that specific, but I don't know how else
Sorry for a long question and not a very descriptive title, but my problem
First of all sorry that I could not think of a more descriptive title.
Sorry if title is not too clear but I think it's about right. NEhow,
I am very sorry that I am not able to provide more details of
Sorry for the not so great title, but I'm curious how people build a
Sorry this is not a very well defined question, I am thinking about an

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.