Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reference to a sub return / Config::General

by Oberon (Monk)
on Jan 22, 2004 at 21:48 UTC ( #323328=perlquestion: print w/ replies, xml ) Need Help??
Oberon has asked for the wisdom of the Perl Monks concerning the following question:

Hopefully I'm missing something obvious here.

I want to take my configuration file and turn it into a hashref. So I have this code:

my $config = { Config::General->new('my.conf')->getall() };

Now, correct me if I'm wrong, but doesn't that take the hash that getall() built and copy it into the anonymous hash I'm constructing? I suppose it's no big deal if it's a small file, but the point is I don't really need a copy.

FTM, doesn't this:

my %config = Config::General->new('my.conf')->getall();

(which is the recommended (by Config::General's doco) method of doing it) perform a useless copy as well? Or is the copy somehow optimized away? (Is that even possible?)

So my question is twofold:

1) Is there some way I can get around this in Perl? ANAICT, you can't actually take a reference to the return of a subroutine because it's a value, not a variable. But is there some way around that?

2) Failing that, is there some alternate way of using Config::General that I'm misisng? I tried giving new() both a -ConfigFile and a -ConfigHash, but that doesn't work.

TIA.

Comment on Reference to a sub return / Config::General
Select or Download Code
Re: Reference to a sub return / Config::General
by freddo411 (Chaplain) on Jan 22, 2004 at 22:04 UTC
    Try this working code for a file named config.dat:

    use Config::General; my %CF = ParseConfig("config.dat");

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

      > my %CF = ParseConfig("config.dat");

      Nice shortcut, freddo. I'll use that. Still has that copy in it though. :)

Re: Reference to a sub return / Config::General
by jeffa (Chancellor) on Jan 22, 2004 at 22:06 UTC

    Config::General was designed to take a text file, parse it, and return the results in a hash. You could patch the code to return a hash reference instead, but if your config file is gigantic in the first place, then maybe you are designing something wrong. Most config files i work with never make into the megabyte range (or even over 100K for that matter).

    Regardless of whether you use:

    my $config = { Config::General->new('my.conf')->getall() };
    or
    my %config = Config::General->new('my.conf')->getall(); my $config = \%config;
    some copying will need to be performed. Don't worry about size unless you have to, and it sounds like you really don't.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      > Don't worry about size unless you have to, and it sounds like you really don't.

      I know, I know: don't microoptimize. But it's the principle dammit! <s>

      No, I shan't worry about it. But I thought maybe there was some way around it that would teach me something new. <he said wistfully ...>

        But I thought maybe there was some way around it

        If the subroutine returns a list instead of a hashref, there's not much you can do about it.

        Just for a little bit more explanation: when you say return %hash, the hash gets flattened into a plain ol' list. When you subsequently say my %hash = foo(), that shoehorns the list into a new hash. Again, there's nothing you can do about this without modifying the subroutine's return statement.

Re: Reference to a sub return / Config::General
by ajt (Prior) on Jan 23, 2004 at 09:02 UTC

    As has been mentioned you can happily submit a patch to the author to make the module return a reference to a hash, rather than the hash. If you think it's worth while, then perhaps other people will, and your patch will be a welcome addition.

    You could also look at the plethora of other configuration file reading programs out there. There is no shortage, Apache style, Windows .ini style, XML or YAML format and dirt simple style. I'm sure you will find that some of them return their configuration data in hashref format.

    ... and more. There are plenty to choose from. Some I know for a fact return their configuration as hasrefs.

    HTH, and Good Luck!


    --
    ajt
      > ... you can happily submit a patch to the author to make the module return a reference to a hash, rather than the hash.

      Well, that's true. Not sure I care that much; I just wanted to know if I was missing something here, and it appears I'm not. So that's good. Although there is another aspect of Config::General I wouldn't mind improving, so if I get around to submitting a patch for that I might throw in the other for free, as it were. <s>

      > You could also look at the plethora of other configuration file reading programs out there.

      Oh, believe me, I've been looking--although you did have one or two new ones on me in that list; thanx for that! But really Config::General is a great choice IMHO. Just that one thing I thought was a bit odd. But I'm over it now. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://323328]
Approved by xenchu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2014-10-30 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls