Perl module problem, please help!! 
Author Message
 Perl module problem, please help!!

Dear all, I'm trying to write a Perl 5 module to write some XML based
markup language tags to separate files. Say I would have something like
this in the source perl script:
-------------------------------------------------------------------------
# I created a directory c:\perl\lib\Xml\ and there are File.pm and
Graphics.pm
use Xml::File;
use Xml::Graphic;

$file = newFile();                                        # create a new
file object
$g = newGraphic();                                   # init a graphics
object
$file->openFile(\*OUT, "file1.xml");         # open a file (OUT,
">file1.xml")
$g->drawText(\*OUT, "Hello World!");    # print a line of text using xml
convention
$file->closeFile(\*OUT, "file1.xml");         # close the filehandle
-------------------------------------------------------------------------
I'm now passing OUT into every method but in fact I only want to pass OUT
into openFile() as a reference once and it would then use the same OUT
throughout the life of $file object, that means I want drawText(),
openFile() and closeFile() to use the SAME filehandle OUT passed in in the
source script. I've tried something like this in the module files:-
-------------------------------------------------------------------------
# File.pm
package Xml::File;
require 5.000;

local *fh;

sub newFile {
    my $class = shift;
    my $self = {};
    bless ($self, $class);
    return $self;

Quote:
}

sub openFile {
    *fh = shift;
    open (fh, ">".$_[1]);
    print fh "<?xml version=\"1.0\"?>\n<!DOCTYPE xml SYSTEM
\"xml.dtd\">\n";
    print fh "<xml " . $_[2] . ">\n";

Quote:
}

sub closeFile {
    *fh = shift;
    print fh "</xml>";
    close (fh);

Quote:
}

1;
-------------------------------------------------------------------------
# Graphics.pm
package Xml::Graphic;
require 5.000;
import Xml::File;

sub newGraphic {
    my $class = shift;
    my $self = {};
    bless ($self, $class);
    return $self;

Quote:
}

sub drawRect {
    *fh = shift;
    print fh "\t<rectangle x=\"$_[1]\" y=\"$_[2]\" width=\"$_[3]\"
height=\"$_[4]\" fillcolor=\"$_[5]\" />\n";

Quote:
}

1;
-------------------------------------------------------------------------
It surely works. But the thing is I have to pass in the filehandle \*OUT
everytime I call each of these functions, is there a way to just pass it to
openFile() and then all other corresponding functions that needs a
filehandle would use the same OUT? Besides, the way I did it is that I
declared a glocal pointer "local *fh" in File.pm and then export it to
Graphic.pm, so Graphic.pm would use the same *fh. But the thing is, since
*fh is global, then if I try to open 2 files one after another and write to
each of them, I will only get all the output into ONE single file (the
second one being initialised) cos' every time File.pm is called to init an
object, *fh will be assigned to that particular filehandle, overwriting the
previous one. So if I try doing this:
-------------------------------------------------------------------------
use Xml::File;
use Xml::Graphic;

$file1 = newFile();                                        # create a new
file object
$file2 = newFile();
$g1 = newGraphic();                                   # init a graphics
object
$g2 = newGraphic();
$file1->openFile(\*OUT1, "file1.xml");       # open a file (OUT,
">file1.xml")
$file2->openFile(\*OUT2, "file2.xml");
$g1->drawText(\*OUT1, "Hello One!");    # print a line of text using xml
convention
$g2->drawText(\*OUT2, "Hello Two!");
$file1->closeFile(\*OUT, "file1.xml");         # close the filehandle
$file2->closeFile(\*OUT, "file2.xml");
-------------------------------------------------------------------------
I will get BOTH lines "Hello One!" and "Hello Two!" in file2.xml cos' OUT2
overwrites OUT1 when File.pm is called when initialising $file1 and then $f
ile2. How do I fix the fault? And how do I NOT have to pass in the
filehandle everytime? Please help.

I know this is a very long question, but it would be deeply appreciated if
anyone can offer help.

Thank you so much,
                             Julius

***There is not enough darkness in the whole world to extinguish the light
of a small candle***



Wed, 25 Apr 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Perl module problem, please help!!

2. Strange problem installing Win32-OLE-0.1403 module (PLEASE HELP )

3. problem with istallation o module:help please!

4. problem importing modules - please help

5. problem importing modules - please help

6. Windows NT Perl 5 DBM Module - Please Help

7. Please help with missing module on perl upgrade?

8. please help with compiling perl/modules 5.005

9. HELP: (please help, wizards) Memory problems

10. Problem with Perl CGI please help

11. CGI PERL problem..please help

12. Please Help friend on perl problem

 

 
Powered by phpBB® Forum Software