That's a good idea Joni,

however, personally, i'd always add a constructor, no matter how basic...

you could do something like this (add this to the file joni described):

sub new { return bless {}, shift }

this will indicate that this object belongs to that package you just
wrote...
this makes things a lot more flexible, and you can store information in the
object too (it's really just a hash reference)

here's how you'd use it:

my $obj = new Package_name;

and if you had the function 'blaa' Joni described, you would access that as:

$obj->blaa( $arg1, $arg2 );

so just like a normal subroutine, only now you got it in a nice and cool
object.

say you'd build lots and lots of methods in taht package name, you can have
them, instead of giving return values,
store stuff in your object (cuz it's just a hash ref remember! )

like so for example:

$obj->{foo} = 'bar';

now every subroutine that is called in that package thru $obj has access to
the value belonging to 'foo'
or can even overwrite it...

I am feeling i'm getting a bit technical here... if there's a desire i write
a tutorial about basic packages, please let me know
you can view some other tuts i wrote on http://japh.nu already
(or http://japh.nu/index.cgi if you want to see the experimental/work in
progress stuff )

regards,

Jos Boumans



> using a separate module is a good idea in this kind of situation.
>
> Put your subroutine to a separate file with the following format:
>
> package packagename;
>
> sub blaa {
>
> code...code...code...
>
> }
>
> 1; # So that the module will return a true value
>
> __END__
>
>
> Save this as packagename.pm and that's it.
>
> Then to your main script you need to get this module included like:
>
> use lib qw(path/to/your/module);
> use packagename;
>
> Then in your code you can access your good 'ol subroutine like this:
>
> packagename::blaa();
>
> or you can pass some argumenst into it as well:
>
> packagename::blaa($argument);
>
> etc. You probably get the picture.
>
> A word of warning: I'm also a newbie, so if there were some errors in my
> explanation, I'd be grateful to hear about it!
>
> Cheers,
>
> Joni
>



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

Reply via email to