Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:32+00:00 2026-05-31T19:00:32+00:00

In my previous question Moose – Loading values from conf files… Jack Maney was

  • 0

In my previous question Moose – Loading values from conf files… Jack Maney was kind enough to provide an example of how to do so using Moose.

In order to make the configuration object even more general I decided to use Config::Auto.

The problem is that I still am very green as to how Moose works. For instance, Jack’s example is:

package My::Module;
use Moose;

has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);

around BUILDARGS=>sub
{
  my $orig=shift;
  my $class=shift;
  my $args=shift; #other arguments passed in (if any).

  my %config_hash=();
  open(my $read,"<","config_file") or confess $!;
  while(<$read>)
  {
    chomp;
    my @array=split /:/;
    $config_hash{$array[0]}=$array[1];
  }
  close($read);

  $args->{config}=\%config_hash;

  return $class->$orig($args);
};

no Moose;
1;

I had modified it to this:

#!/usr/local/bin/perl
package DART::Setup;

use namespace::autoclean;
use Moose;
use Config::Auto;

our $VERSION = '0.0.1';


has 'EMPTY' => ( isa => 'Str', is => 'ro', default => q{} );
has 'PPLTESTEXECUTIONID' => ( isa => 'Int', is => 'ro', default => 0 );
has 'SARQTESTEXECUTIONID' => ( isa => 'Int', is => 'ro', default => 0 );
has 'ISPROXY' => ( isa => 'Int', is => 'ro', default => 0 );
has 'LOCALHOST' => ( isa => 'Str', is => 'ro', default => '127.0.0.1' );
has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);
has 'SSO' => ( isa => 'Str', is => 'rw', default => q{} );
has 'cookieFile' => ( isa => 'Str', is => 'rw', default => q{} );

around BUILDARGS=>sub
{
  my $orig=shift;
  my $class=shift;
  my $args=shift;

  my $cfg = Config::Auto::parse($args);
  my %config_hash = %{$cfg};

  $args->{config}=\%config_hash;

  return $class->$orig($args);
};


return 1;

But to be honest I’m not sure what I’m doing here. First off, how many arguments do I need to provide when I’m creating a new Setup object? Do I just pass it the path to my configuration file, something like:

my $newConfig = DART::Setup->new('/home/y/conf/MyApp/MyApp.cfg');

Or do I need to provide arguments for $orig and $class?

Finally, how do I now access my newly loaded configurations? Can I do something like:

my %configHash = %{$newConfig->config()};

foreach my $key (keys %configHash) {
print "the key is, $key, and the value is: $configHash{$key}\n";
}

Am I understanding this correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:00:33+00:00Added an answer on May 31, 2026 at 7:00 pm

    Okay, inside of BUILDARGS, you want to read in the config file and pass the key-value pairs into the config attribute. Here’s a modified version with another attribute for the config file.

    package My::Module;
    use Moose;
    use Config::Auto;
    
    has 'config'=>(isa=>'HashRef[Str]',is=>'rw',required=>1);
    
    has 'config_file'=>(isa=>'Str',is=>'ro');
    
    around BUILDARGS=>sub
    {
      my $orig=shift;
      my $class=shift;
      my $args=shift; #currently {config_file=>'/path/to/file/config_file.conf'} (or whatever)
    
      #make sure we've been passed a config file
      confess "No config file found in BUILDARGS" unless defined $args->{config_file};
    
      #Now, we open the user-specified config file via Config::Any
      my $ca=Config::Auto->new(source=>$args->{config_file},format=>"colon");
      my $parsed=$ca->parse; #hash reference containing the parsed data.
    
      #Now, we add this to our arguments that will become our attributes:
    
      $args->{config}=$parsed;
    
      return $class->$orig($args);
    }
    
    no Moose;
    1;
    

    The main thing to realize about BUILDARGS is that it takes the following arguments: the names of the class and original constructor (which are passed to Moose::Object) and then any other arguments passed to the constructor. So, if you call

    my $mm=My::Module->new({config_file=>"/path/to/file/file.conf"});
    

    Then, in BUILDARGS, we initially have

    $args=={config_file=>"/path/to/file/file.conf"}
    

    But after parsing the file and adding the $parsed hash reference, it turns into

    $args=={config_file=>"/path/to/file/file.conf",config=>{server=>"mozilla.org",protocol=>"HTTP",...}}
    

    etc, etc.

    By writing my $cfg = Config::Auto::parse($args); inside of BUILDARGS, you’re trying to pass a config_file argument to the parser in Config::Auto, and it’ll have no idea what to do with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This follows on from my previous question about Moose structured types. I apologise for
Greetings, As a followup to my previous question about Moose, I've now run into
This question follows on from a previous question, that has raised a further issue.
Continuing from my previous question , is there a comprehensive document that lists all
This is kind of a followup on my previous question . I have an
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is directly related to my previous question ASP.NET AJAX Is it possible
this question is an extension to a previous question i asked (and was answered).
This question is related to a previous question of mine That's my current code
This question is related to my previous question How to generate Cartesian Coordinate (x,y)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.