Private variables in the Perl Moose class

I'm starting to learn about objects in Perl using Moose.

I'm not sure if I understand the purpose MooseX::Privacy. Consider:

use v5.14;

package PA {
    use Moose;
    my $var='private?';
    1;

    sub getVar {
        return $var;
    }
}

package PB {
    use Moose;
    use MooseX::Privacy;

    has 'var' => (
        is => 'rw',
        isa => 'Str',
        default   => 'private?',
        traits => [qw/Private/],
    );
    1;

    sub getVar {
        my $self = shift;
        return $self->var;
    }
}

my $o1= PA->new();
my $o2= PB->new();

say $o1->getVar();
say $o2->getVar();

In both classes PAand PBI have a personal variable var. Only in class PBI use MooseX::Privacy. What is the difference between these two approaches? And why should I use MooseX::Privacy?

+4
source share
1 answer

If you are looking for Java-style method privacy, then MooseX :: Privacy will be a big disappointment. Here's what happens to the inviolability of the Java style method:

/* This file is called Main.java */
public class Main
{
    public class MyParent
    {
        private String message_string ()
        {
            return "Message from %s\n";
        }

        public void print_message ()
        {
            System.out.printf( this.message_string(), "MyParent" );
        }
    }

    public class MyChild extends MyParent
    {
        public String message_string ()
        {
            return "Another message from %s\n";
        }
    }

    public static void main (String[] args)
    {
        Main o = new Main();
        o.run();
    }

    public void run ()
    {
        MyParent c = new MyChild();
        c.print_message();
    }
}

You can compile and run this example as follows:

$ javac Main.java
$ java Main
Message from MyParent

, . (MyParent) message_string() . child , - !

Perl MooseX:: Privacy...

# This file is called Main.pl
use v5.14;
use strict;
use warnings;

package MyParent {
    use Moose;
    use MooseX::Privacy;

    private_method message_string => sub {
        my $self = shift;
        return "Message from %s\n";
    };

    sub print_message {
        my $self = shift;
        printf($self->message_string(), __PACKAGE__);
    }
}

package MyChild {
    use Moose; extends qw(MyParent);
    use MooseX::Privacy;

    sub message_string {
        my $self = shift;
        return "Another message from %s\n";
    }
}

my $c = new MyChild();
$c->print_message();

:

$ perl Main.pl
Another message from MyParent

, WHA?!?!?! message_string ?! MyChild MyParent?!

, MooseX:: Privacy , OO. MooseX:: , :

die "GO AWAY!!" unless caller eq __PACKAGE__;

, MooseX:: Privacy .

, MooseX:: Privacy. , . :

use v5.14;
use strict;
use warnings;

package MyParent {
    use Moose;

    my $message_string = sub {
        my $self = shift;
        return "Message from %s\n";
    };

    sub print_message {
        my $self = shift;
        printf($self->$message_string(), __PACKAGE__);
    }
}

package MyChild {
    use Moose; extends qw(MyParent);

    sub message_string {
        my $self = shift;
        return "Another message from %s\n";
    }
}

my $c = new MyChild();
$c->print_message();

:

$ perl Main2.pl
Message from MyParent

!! !

, MooseX:: Privacy, ( ), MooseX:: Privacy.

? , CPAN, : Lexical:: Accessor. , "out out" (.. blessed hashref) ( $get_message ).

, MooseX:: Privacy.

+5

Source: https://habr.com/ru/post/1535812/


All Articles