in reply to Perl/Moose calling writer doesnt work
I concur with haukex that you should make a SSCCE to demonstrate/research your issue. Something very simple like:
This will show you the error clearly. Output:# 1210614.pl package MyClass { use Moose; has FileName => (is => 'rw', isa => 'Str', writer => 'SetFileName' +); sub SetFileName { my $self = shift; my $arg = shift; warn "I am here with $arg"; } }; package main { my $obj = MyClass->new; $obj->SetFileName('/foo/bar'); };
$ perl 1210614.pl You are overwriting a locally defined method (SetFileName) with an acc +essor ...
As you can see this is not what specifying a custom writer does. As far as I understand it, specifying a custom writer property for an attribute simply changes the name of the writer from the default.
Output:# 1210614-1.pl package MyClass { use Moose; has FileName => (is => 'rw', isa => 'Str', writer => 'SetFileName' +); }; package main { my $obj = MyClass->new; $obj->SetFileName('/foo/bar'); warn $obj->FileName; };
$ perl 1210614-1.pl /foo/bar at 1210614-1.pl line 9.
For what you are doing your code should use a custom type check, or a coercion, or a trigger instead.
Output:# 1210614-2.pl package MyClass { use Moose; has FileName => (is => 'rw', isa => 'Str', trigger => \&SetFileNam +e); sub SetFileName { my $self = shift; my $arg = shift; warn "I am here with $arg"; } }; package main { my $obj = MyClass->new; $obj->SetFileName('/foo/bar'); };
$ perl 1210614-2.pl I am here with /foo/bar at 1210614-2.pl line 7.
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl/Moose calling writer doesnt work
by jorba (Sexton) on Mar 10, 2018 at 13:36 UTC | |
by duelafn (Parson) on Mar 10, 2018 at 18:48 UTC |