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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:59:22+00:00 2026-06-10T16:59:22+00:00

I have the following Perl code, which reads the input and indents the file

  • 0

I have the following Perl code, which reads the input and indents the file correctly. I’m not using xmllint, or XML-Tidy because of some problems with the DTD. But let’s say for exercise I’ve use the code below:

 ### begin_: file metadata
        ### <region-file_info>
        ### main:
        ###   - name    : XMLPrettyPrint: simple xml pretty print in perl
        ###     desc    : use perl with XML::Twig library to print indented xml
        ###     date    : created="Thu 2005-12-01 11:08:15"
        ###     last    : lastmod="Thu 2005-12-01 11:22:34"
        ###     lang    : perl
        ###     tags    : perl xml indent formatted pretty string cfPrettyPrint
        ### </region-file_info>

### begin_: init perl
    use strict;
    use warnings;
    use XML::Twig;

### begin_: init vars
    my  $sXML  = join "", (<DATA>);

    ### init params
    my  $params = [qw(none nsgmls nice indented record record_c)];
    my  $sPrettyFormat  = $params->[3] || 'none';

### begin_: process
    my  $twig= new XML::Twig;
    $twig->set_indent(" "x2);
    $twig->parse( $sXML );
    $twig->set_pretty_print( $sPrettyFormat );
    $sXML      = $twig->sprint;

### begin_: output
    print $sXML;

### begin_: sample data
    1;
    __END__
<table><tr age="35" >
<fname>Homer</fname>
<lname>Simpson</lname></tr>
<tr age="33" >
<fname>Barney</fname>
<lname>Rubble</lname></tr>
<tr age="29" >
<fname>Betty</fname>
<lname>Rubble</lname></tr></table>

It indents and print is correctly … however, I needed to read it from an XML file, indent and then right it back to the same file, with the new format. I’m totally new to Perl, how could I read the input from a file, instead of a sample data, and then use that above code to indent?
Thanks

  • 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-10T16:59:24+00:00Added an answer on June 10, 2026 at 4:59 pm

    How about

    my $file= $ARGV[0];
    XML::Twig->new( pretty_print => 'indented')
             ->parsefile( $file )
             ->print_to_file( $file);
    

    No need to set the indent to 2 spaces, that’s the default. The 1 at the end is also not needed, it is only needed for modules, to inform the parser that they have loaded properly.

    This is also equivalent to xml_pp -i myfile.xml

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

Sidebar

Related Questions

I have the following code #!/usr/bin/perl use Tie::File; tie my @last_id, 'Tie::File', 'last_id.txt' or
I have a problem passing a string argument using Perl. The following code #!/usr/bin/perl
I am trying to use the following code, which I have not been able
I have the following code in Perl which posts to my HTTP Servlet, calling
I have the following perl script: $myVariable = some value; //... some code ...
I have written the following code in Perl. I want to iterate through a
I have the following code to open a browser in Perl $IE = Win32::OLE->new(InternetExplorer.Application);
In my perl code I've previously used the following two styles of writing which
I am using the following code to dynamically create an INPUT element and assign
I have the following Perl code: STDOUT->autoflush(1); foreach(...) { ... foreach(...) { print(Processing $folder);

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.