Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

After the user click on the buy button, I’m creating an order.xml file using the format:

$item=”heat741a”; #for the sake of the example I fixed the variables $OrderQty = 55; open(LOG, ">order.xml") || die "Can't open the log file: $!\n"; $format = "format LOG = \n" . "\<\?xml version\=\"1\.0\"\?\>" . "\n" . "\<order\>" ."\n" . "\<product\>" .'@' . '<<<<<<' . "\<\\product\>" . "\n" . '$item' . "\n" . "\<quantity\>" .'@' . '<' x (length($OrderQty)-1) . "<\\quantity +>" . "\n" . '$OrderQty' . "\n" . "\<\\order\>" ."\n" . ".\n"; eval $format; write LOG; close LOG;


for some reason I’m getting this result:

<?xml version="1.0"?>
<order>
<product>heat741a \product>
<quantity>55 \quantity>
<\order>

Missing the ‘<’ sign before \product> and \quantity> despite I’m escaping them. Does anyone know what’s going wrong.
I know it’s easier just to use print instead of the dynamic format but the exercise is a bit more complex than that, I just posted what’s relative to the problem.

Thanks, Michelle

Replies are listed 'Best First'.
Re: Format weird behaviour
by bobn (Chaplain) on Jun 08, 2003 at 02:07 UTC
    I can't help myself, even though I'll probably lose XP. Why in the name of whatever_deity_you_like wouldn't you just say:
    $item="heat741a" ; $OrderQty = 55; open(LOG, ">order.xml") || die "Can't open the log file: $!\n"; print LOG qq(<?xml version="1.0"?> <order> <product>$item</product> <quantity>$OrderQty</quantity> </order>);
    I never use formats unless I want something that automatically paginates or wraps blocks of text in columns. Update: removed trailing \n which might have caused well-formednes complaints.
Re: Format weird behaviour
by fglock (Vicar) on Jun 08, 2003 at 00:52 UTC
    print "\<"; # < print '\<'; # \<

    You have to use single quotes there. Double quotes "eat" the backslash.

Re: Format weird behaviour
by antirice (Priest) on Jun 08, 2003 at 00:58 UTC

    First, let me say ugh. What are you using to parse this that it requires fixed length fields? Secondly, shouldn't you have closing tags starting with / instead of \?

    Now how to fix your problem. It's pretty simple. If you output what is going to be evaluated, you'd realize that format will see @<<<<<<<\product. So it's misinterpreting what you really want. Replace the < that you want to show up with an @ and add "<" to the list of variables to fill it in. For more information on this, perldoc perlform.

    $item="heat741a"; #for the sake of the example I fixed the variables $OrderQty = 55; open(LOG, ">order.xml") || die "Can't open the log file: $!\n"; $format = "format LOG = \n" . "\<\?xml version\=\"1\.0\"\?\>" . "\n" . "\<order\>" ."\n" . "\<product\>" .'@' . '<<<<<<' . '@' . "\\product\>" . "\n" . '$item,"<"' . "\n" . "\<quantity\>" .'@' . '<' x (length($OrderQty)-1) . '@' . "\\qua +ntity>" . "\n" . '$OrderQty,"<"' . "\n" . "\<\\order\>" ."\n" . ".\n"; eval $format; write LOG; close LOG;

    Hope that helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      antirice,

      It didn't work, i got a space. Using single quote in place of double quotes didn't work either: (if i write '</quantity>' i'll get /quantity>; and if i write '\</quantity>', i get \</quantity>)it's really getting weird.

      Tanx

        Ah, I never cleaned up the code and am reaping the rewards now. This post is pretty long, so I've used readmore to explain everything in detail.

        Sorry for not going into greater detail the first time.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Format weird behaviour
by gmpassos (Priest) on Jun 08, 2003 at 02:40 UTC
    Use some XML module if you want to create real XML data easy.

    You can use XML::Simple (that works with XML::Parser), or XML::Smart (that works with XML::Parser or with it own Parser too).

    I prefer to use XML::Smart since you don't need to install an extra module for the parser, and works like XML::Simple, but with some extra resources. But I'm also the author of XML::Smart and my opinion don't count much in this case. ;-P

    Graciliano M. P.
    "The creativity is the expression of the liberty".