perl & heredoc to create xml

L

LHradowy

I have a perl script that runs a sql query to a temp file, what I need
to do is take that temp file and pull data to make an xml file.
So there are 1000 lines in this file, so I would need 1000 xml files.
I can get the sql to work...
But, I need to input the account information into an XML, where the
ticket number is.

my $fin_tck = "/home/fin_tck";
chdir ("$fin_tck");

my $tempFile = "$fin_tck/fin.tmp.$$";

my $command = <<EOF;
sqlplus -s $oraUser/$oraPass\@$ENV{'ORACLE_SID'} <<EOR>$tempFile
set heading off
set termout off
set linesize 807
set space 0
set wrap off
set feedback off
set pagesize 7010
SELECT tn||','||ticket_number||','||rcd
FROM vt_table
quit
EOF
system($command);

chomp( my $date = `date '+%Y%B%d%H'` );
open( TEMP, "$tempFile" );

while (<TEMP>) {
chomp;
s/\s+/,/g; # remove trailing whitespace
my($tn,$account,$related) = split(",", $_);
print OUT <<EOF;

<?xml version="1.0" encoding="ISO-8859-1"?><External2AC
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespac
eSchemaLocation="ext2ac.xsd">
<TroubleTicket action="finalize" type="ticket">
<KeyInfo>
<TicketNumber>$account</TicketNumber>
</External2AC>
EOF
}

close OUT, AC_OUT;
close TEMP;
unlink($tempFile);
 
T

Tad McClellan

LHradowy said:
chdir ("$fin_tck");

(perl isn't the shell.)

Please see:

perldoc -q vars

What’s wrong with always quoting "$vars"?


You should also check the return value to see if you actually
got what you asked for:

chdir( $fin_tck ) or die "could not cd to '$fin_tck' $!";

system($command);


If you are interested in whether you actually got what you asked
for, then you should check the return value here too:

!system( $command ) or die "problem running sqlplus $?";
^ ^^
^ ^^
chomp( my $date = `date '+%Y%B%d%H'` );


perldoc -f localtime

open( TEMP, "$tempFile" );


You should always, yes *always*, check the return value from open():

open( TEMP, $tempFile ) or die "could not open '$tempFile' $!";

s/\s+/,/g; # remove trailing whitespace


That does much more that merely removing trailing whitespace!

my($tn,$account,$related) = split(",", $_);


Whitespace is not a scarce resource. Feel free to use as much of it
as you like to make your code easier to read and understand.

A regular expression should *look like* a regular expression:

my($tn, $account, $related) = split(/,/, $_);


close OUT, AC_OUT;


That does not do what you think it does.

You should enable

use warnings;
use strict;

in every program that you write.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top