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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:32:35+00:00 2026-06-10T04:32:35+00:00

@files = glob *.xml; undef $/; for $file (@files) { $indent = 0; open

  • 0
@files = glob "*.xml";

undef $/;
for $file (@files) {
    $indent = 0;
    open FILE, $file or die "Couldn't open $file for reading: $!";
    $_ = readline *FILE;
    close FILE or die "Couldn't close $file: $!";

    # Remove whitespace between > and < if that is the only thing separating them
    s/(?<=>)\s+(?=<)//g;

    # Indent
    s{  # Capture a tag <$1$2$3>,
        # a potential closing slash $1
        # the contents $2
        # a potential closing slash $3
        <(/?)([^/>]+)(/?)> 

        # Optional white space
        \s*

        # Optional tag.
        # $4 contains either undef, "<" or "</"
        (?=(</?))?
    }
    {
        # Adjust the indentation level.
        # $3: A <foo/> tag. No alteration to indentation.
        # $1: A closing </foo> tag. Drop one indentation level
        # else: An opening <foo> tag. Increase one indentation level
        $indent +=
            $3 ?  0 :
            $1 ? -1 :
                  1;

        # Put the captured tag back into place
        "<$1$2$3>" .
        # Two closing tags in a row. Add a newline and indent the next line
        ($1 and ($4 eq "</") ?
            "\n" . ("  " x $indent) : 
        # This isn't a closing tag but the next tag is. Add a newline and
        # indent the next line.
        $4 ?
            "\n" . ("  " x $indent) :
        # This isn't a closing tag - no special indentation. I forget why
        # this works.
            ""
        )
    # /g repeat as necessary
    # /e Execute the block of perl code to create replacement text
    # /x Allow whitespace and comments in the regex
    }gex;

    open FILE, ">", $file or die "Couldn't open $file for writing: $!";
    print FILE or die "Couldn't write to $file: $!";
    close FILE or die "Couldn't close $file: $!";
}

I’m using this code to indent a bunch of xml files correctly. However, when I execute I get:

Use of uninitialized value $4 in string eq at C:/Users/souzamor/workspace/Parser/xmlreformat.pl line 25.

and line 25 is:

# $4 contains either undef, "<" or "</"

I don’t know why is it, and I’m new to Perl. Could someone please help me?

  • 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-06-10T04:32:36+00:00Added an answer on June 10, 2026 at 4:32 am

    The $4 refers to the fourth capturing parenthesis in your regular expression, in this case: (?=(</?))?. As the comment states this may be undefined because of the ? at the very end which means “this thing may be there, but it also might not be”.

    If you use an undefined value (signalled via the special value undef in Perl) in certain ways, including in a string comparison with eq, you get a warning from Perl. You can easily check whether or not a variable is defined with defined($var).

    In your particular case $4 is used in this phrase:

    ($1 and ($4 eq "</") ? "\n" . ("  " x $indent) : 
     $4                  ? "\n" . ("  " x $indent) :
                           ""
    

    Fixing the warning is as easy as replacing those tests with this:

    ($1 and defined($4) and ($4 eq "</") ? "\n" . ("  " x $indent) : 
    $4                                   ? "\n" . ("  " x $indent) :
                                           ""
    

    Note that you don’t have to check for defined($4) in the second line in this particular case, but it wouldn’t hurt either.

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

Sidebar

Related Questions

I am reading file contents in perl by following code my @files = glob($PATH/*);
What's the C++ way of Perl's idiom: my @files = glob(file*.txt); foreach my $file
I need to glob ./../path/to/files/**/*.txt but instead of receiving matches like this: ./../path/to/files/subdir/file.txt I
I have to following code in ruby: <% files = Dir.glob('/**/*') files.each do |file|
#include path include_directories( ${PROJECT_SOURCE_DIR}/include ${HGE_INCLUDE_DIR} ${IKL_INCLUDE_DIR} ${BOOST_DIRECTORY} ) #include all files FILE(GLOB CORE_SRCS source/core/*.cpp)
I have the following Perl script with is meant to indent a XML file
I'm trying to open a number of files using glob and feed them through
I've created a list of files using: file(GLOB_RECURSE DEPLOY_FILES ${PROJECT_SOURCE_DIR}/install/*) I want to install
I want all CSV files in a directory, so I use glob('my/dir/*.CSV') This however
I'm using Mongoid on Sinatra . And I use Dir.glob(File.join(File.dirname(__FILE__),'models','*.rb')).each do |file| require file

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.