Better to explain the problem by code :)
Just a small annotation: I'm writing a wrapper under Config::
<https://metacpan.org/pod/Config::JSON>JSON
<https://metacpan.org/pod/Config::JSON> module. When calling ->new
constructor of Config::JSON it's required to pass obligatory parameter,
pathToFile So it's not possible to construct wrapper via Mojo::Base
attribute <https://metacpan.org/pod/Mojo::Base#has> or Mojo::Base::new
basic constructor <https://metacpan.org/pod/Mojo::Base#new> because
pathToFile is user-defined.
I decided to make a custom constructor setup() function. But this function
doesn't work if I try to change attribute inside it.
This code doesn't work:
package ConfigJSON;
use Mojo::Base -base;
use Config::JSON;
has 'pathToTokensFile' => 'config.json'; # default is config.json
has 'tokensfile'; # pathToTokensFile wrapped by Config::JSON
sub setup {
my $self = shift;
$self->tokensfile = Config::JSON->new(pathToFile => $self->
pathToTokensFile); # this string cause Can't modify non-lvalue subroutine
call error
};
package main;
use Data::Dumper;
my $c = ConfigJSON->new;
$c->pathToTokensFile('gapi.conf');
$c->setup();
warn Dumper $c;
This code works:
package ConfigJSON;
use Mojo::Base -base;
use Config::JSON;
has 'pathToTokensFile' => 'config.json'; # default is config.json
my $tokensfile;
sub setup {
my $self = shift;
$tokensfile = Config::JSON->new(pathToFile => $self->pathToTokensFile);
};
package main;
use Data::Dumper;
my $c = ConfigJSON->new;
$c->pathToTokensFile('gapi.conf');
$c->setup();
warn Dumper $c;
Why first example doesn't work?
I'd like to save ability to call ConfigJSON->tokensfile from another module.
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.