I tried many ways to create a XML file but doesn't work. Below is the code to generate plain text file. Can someone suggest a method on how I can create a XML file instead of a text file in the below code?
#!/usr/bin/perl -w
use CGI;
use strict;
use warnings;
#use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header;
print $q->start_html(-title => "Line Calculation");
my $namex1 = $q->param('value_x1');
my $namex2 = $q->param('value_x2');
my $namey1 = $q->param('value_y1');
my $namey2 = $q->param('value_y2');
#entered values are printed:
print $q->h1("Calculation of slope and y-intercept");
print $q->ul(
$q->li(["<b>X1 value:</b> $namex1","<b>X2 value:</b> $namex2",
"<b>Y1 value:</b> $namey1", "<b>Y2 value:</b> $namey2"])
);
#calculation of slope and y-intercept is below:
my $slope1 = ($namey2-$namey1)/($namex2-$namex1);
print $q->h2("Slope(m) is $slope1");
my $inter = ($namey1-($slope1*$namex1));
print $q->h2("y-intercept(b) is $inter");
#file is processed below:
my $filename = 'file.txt';
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh "\n\nx1=$namex1, x2=$namex2, y1=$namey1, y2=$namey2\n";
print $fh "\nslope is $slope1, y-intercept is $inter\n\n";
close $fh;
open (MYFILE, '>data.xml');
print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>};
print MYFILE '<x1=$namex1, x2=$namex2, y1=$namey1, y2=$namey2>';
print MYFILE '<slope is $slope1, y-intercept is $inter>';
close (MYFILE);
print $q->li("\nall the list of data entered so far is accessible in file: 'file.txt' and 'data.xml'\n");
print $q->end_html;