On Fri, Aug 08, 2003 at 12:05:47AM +0000 Pablo Fischer wrote:

> I have some questions about OOP in Perl.
> 
> 1. In my class I have lot of methods, but some of them just parse a file or 
> pass a string to a different format (like in C, Im thinking that they are 
> private), In perl, whats the better way to call a private method:
> 
> $state = $this->city2state("Arg1");
> 
> and in the citystate have
> 
> sub city2state {
>       my $this = shift;
>       my $city = $_[0];
> 
> ...MoreCode..
> }

That's an ordinary method call. Nothing wrong with that. Nonetheless I'd
write

    my ($this, $city) = @_;

inside the method.

> Or this way
> 
> $state = &city2state("Arg1");
> 
> and in citystate have
> 
> sub citystate {
>       my $city = $_[0];
> 
> MoreCode..
> }

No. This is a plain function call. city2state() is now no longer a
method and wouldn't obey to inheritance either, for example.

With 'private', do you perhaps really mean 'static'? That is, the object
on which the method was called, isn't actually used? If so, call it as a
class method:

    Your::Class->city2state("Arg1");

    sub city2state {
        my ($class, $city) = @_;
        # $class is now "Your::Class" and can be ignored if you want
    }

> Which is the correct way? Im confused if I need to use $this for public and 
> private methods or no..

If you really mean private versus public this is a different story.
Whatever you call the object inside your methods should be of no concern
for the interface. If you call it $this or $self or $mona_lisa is up to
you.

There is however a certain convention in Perl to make private methods
look private by prepending an underscore:

    $obj->_private;

Of course, perl just doesn't care and _private() can still be accessed
by anyone who gets hold of it. 

> 2. Exists an special nomenclature, like Arrays need to be in capital letters 
> and scalar variables not.

Not in this way. The nomenclature is to use lowercase names for lexical
variables (so it's $this, not $This and certainly not $THIS). Globals
should be all uppercased ($GLOBAL). As for things like $Variable, I am
not sure. I haven't often seen them so it is usually either only
lowercase or uppercase.

One final thing about method names: make them lowercase and use
underscores to separate words. This looks Perl-ish:

    $obj->get_attribute;

while this looks Java-ish and is harder to read:

    $obj->getAttribute;

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Reply via email to