Tom Norwood wrote:
> I'm looking into writing some OO modules for my sites, and have found
> a few
> examples
> but they don't define their own 'clone' functions.  So from what you
> said
> previously
> 'clone' must be defined within one of the following:
>
> use HTTP::Status;
> use HTTP::Request;
> use LWP::UserAgent;
> use URI::URL;
>
> Is this right, and if so how do I find out how to use it myself.

Hi Tom.

You don't say how you know that there is a 'clone' method
at all, but given that there is, you are correct. To find out
exactly which modules support clone, add this code to the
head of your program:

    for (qw (
            HTTP::Status
            HTTP::Request
            LWP::UserAgent
            URI::URL )) {
        print "$_ can clone\n" if $_->can('clone');
    }

output:

    HTTP::Request can clone
    LWP::UserAgent can clone
    URI::URL can clone

so all of your modules except for HTTP::Status
have this method. To find out how to use them,
just type:

    perldoc HTTP::Request

etc. on the command line.

To explain how clone works in general, remember that
objects are simply data references, so clearly if your
object was based around an array:

    my @data;
    my $object = [EMAIL PROTECTED];

you couldn't get a new object by simply writing:

    my $object2 = $object;

as what you would have is simply a second handle
to the same object, and all changes to one would
be echoed in the other. You would have to do:

    my $object2 = clone ($object);

    sub clone {
        my $original = shift;
        my @newdata = @$original;
        return [EMAIL PROTECTED];
    }

to copy all of the constituent data and form a
independent object. This is exactly what clone
normally does, and is called like this:

    my $object2 = $object->clone;

often with some optional parameters to make
minor changes to the resulting clone. Use the
perldoc commands to find out exactly what you
can do.

Beware, though, that clone() can do anything at all.
Like new(), Perl doesn't distinguish it from any other
method name. However an author would be very
irresponsible to publish software with anything
other than the obvious functionality in these methods.

I hope this helps.

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to