Hello Monks,
I would like to set the accessors values of my class using an XML file.
I would like to build my class using a constructor that read an XML file and set the values.
Also, outside the constructor that values must be immutable.
So I have:
package NavigationRules;
our $VERSION=0.01;
use Moose;
use XML::Simple;
use FindBin qw($Bin);
use lib ("$Bin/classes");
has 'visitors' => (is=>'ro',isa=>'Int',required=>1,default=>1);
#has 'numVisits' => (is=>'ro',isa=>'Int',default=>1);
sub BUILD {
my $self=shift;
my $rules= XMLin("$Bin/conf/navigation_rules.xml",NormaliseSpace=>
+2);
$self->visitors=$rules->{navigation}->{visitors};
# $self->numVisits=$rules->{navigation}->{numVisits};
}
__PACKAGE__->meta->make_immutable;
1;
But in this way, I have this error:
Can't modify non-lvalue subroutine call at /home/wolf/Scrivania/naviga
+tor/classes/NavigationRules.pm line 29.
That is beacause the BUILD method (constructor) tryed to modify an accesor method.
So I tried with:
has 'visitors' => (is=>'ro',isa=>'Int',writer=>'BUILD',required=>1);
But without success:
You are overwriting a locally defined method (BUILD) with an accessor
+at /usr/local/lib/perl/5.10.0/Moose/Meta/Attribute.pm line 663
(I don't want to use a builder method for each accessor, beacause I would like read the XML file only one time).
So my question is:
how can I define an accessor value inside a constructor without that error?
Thank you in advance for your answers.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.