perl & heredoc to create xml

Discussion in 'Perl Misc' started by LHradowy, Feb 28, 2007.

  1. LHradowy

    LHradowy Guest

    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);
     
    LHradowy, Feb 28, 2007
    #1
    1. Advertising

  2. LHradowy <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 1, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flupke

    heredoc and variables

    flupke, Jun 4, 2004, in forum: Python
    Replies:
    7
    Views:
    3,380
    =?ISO-8859-1?Q?Xavier_Mart=EDnez?=
    Jun 7, 2004
  2. Paul Vudmaska

    lil problem w/heredoc

    Paul Vudmaska, Apr 7, 2004, in forum: Ruby
    Replies:
    2
    Views:
    144
    Paul Vudmaska
    Apr 7, 2004
  3. David Tran
    Replies:
    6
    Views:
    851
    Eric Hodel
    Mar 17, 2005
  4. Oliver Heins
    Replies:
    2
    Views:
    139
    Oliver Heins
    Sep 11, 2006
  5. Replies:
    3
    Views:
    182
    Wolfgang Nádasi-Donner
    Jan 27, 2007
Loading...

Share This Page