For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > XML::Writer....Lessons Learned









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML::Writer....Lessons Learned
Dave Adams

2007-04-27, 6:58 pm

Because I am not a very good perl programmer (but working on it) an unable
to answer most of the questions from the group, I thought I could at least
contribute what I have learned so I can help others that are in my position.

Today I learned something about XML::Writer where I was using single quotes
instead of double quotes and it caused me all sorts of problems later in the
script. Here is my corrected code:

use strict;
use CGI;
use CGI::Carp;
use CGI::Carp qw(fatalsToBrowser);
use CGI qw(escapeHTML);
use XML::Writer;
use IO::File;

my $output = new IO::File(">test.xml");
my $writer = new XML::Writer(OUTPUT => $output);
$writer->startTag("greeting","class" => "simple"); # It was here that I was
using singles instead of doubles and it caused problems later on.
$writer->characters("Hello, world!");
$writer->endTag("greeting");
$writer->end();
$output->close();

my $page = new CGI;
my $htmldoc;
$htmldoc .= "Testing Testing";
print $page->header; # create the HTTP header
print $page->start_html('TEST PAGE'); # start the HTML
print $htmldoc;
print $page->end_html;

Thanks to people in the group who have helped me. It is a great resource.

Tom Phoenix

2007-04-27, 6:58 pm

On 4/27/07, Dave Adams <davidlamontadams@gmail.com> wrote:

> Today I learned something about XML::Writer where I was using single quotes
> instead of double quotes and it caused me all sorts of problems later in the
> script.


That can happen. But I can't find it happening anywhere in the code you posted.

A single quoted string is just syntax for a literal string. But double
quoted strings in Perl may be more than just literal strings; they can
implicitly invoke join() or concatenation, for example. Still, in the
code you posted, your strings are merely string literals, with no
interpolation going on. Single quotes would have worked as well.

Sometimes, people have the idea that 'fred' is a different string than
"fred". But they're analogous to writing 255.0 versus 255.00000; even
though they look different to human eyes, they mean the same to Perl.

Maybe the quote marks made a difference in another program, such as a
text editor? Or maybe you fixed a missing quote mark when you changed
from singles to doubles? In any case, I'm glad that you were
successful.

Cheers!

--Tom Phoenix
Stonehenge Perl Training
Dr.Ruud

2007-04-28, 6:58 pm

"Tom Phoenix" schreef:

> Sometimes, people have the idea that 'fred' is a different string than
> "fred".


They are, from a maintenance point of view. I've seen it go bad often
with changes like

before:
"user"
after:
"user@example.com"

so I suggest to write q{user}, to make explicit that no expansion should
ever happen.

--
Affijn, Ruud

"Gewoon is een tijger."

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com