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

When I want to create <text:p> tag, it become problem. How can I do that?
I want produce this tag when read from 'file.txt'. <text:p text:style-name="Standard"> Hello </text:p> with using this perl script: #!/usr/bin/perl use CGI::Pretty qw(text:p); $c = new CGI; open(FILE, "file.txt"); while($line = <FILE>) { print $c->text:p({-'text:style-name'=>"Standard"},$line); } close FILE; but if use CGI::Pretty qw(text) it can do that, with produce <text text:style-name="Standard"> Hello </text>

Replies are listed 'Best First'.
(jeffa) Re: Problem with CGI::Pretty
by jeffa (Bishop) on Feb 05, 2003 at 15:12 UTC
    You are using the wrong tool, try XML::Writer instead:
    use strict; use warnings; use XML::Writer; my $c = XML::Writer->new; $c->startTag('text:p','text:style-name'=>"Standard"); $c->characters('Hello'); $c->endTag('text:p'); $c->end;
    I also recommend that you get a copy of Perl and XML and/or XML and Perl.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Problem with CGI::Pretty
by theorbtwo (Prior) on Feb 05, 2003 at 12:30 UTC

    There are two problems here. The first is that CGI::Pretty is only designed to output HTML, not XML. HTML tags never have colons in their name, so this wansn't considered in the API of the module. The second is that colons aren't valid in perl identifiers, without special precautions (essensialy, symbolic references). The reason this is going through as a text tag is that text:p is a call to the text sub, with an attribute of p.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: Problem with CGI::Pretty
by PodMaster (Abbot) on Feb 05, 2003 at 12:23 UTC
    That is not HTML as far as i know, and according to the manual (How to RTFM), xml or xhtml is not supported (whatever you're using there).

    Furthermore, if you examine the CGI::Pretty sourcecode, you'll realize that subroutines are being created dynamically out of tag names, so it's impossible for that to work

    perl -e"sub foo:p{'foo:p'};die foo:p" Invalid CODE attribute: p at -e line 1 BEGIN failed--compilation aborted at -e line 1.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.