dichtfux has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm currently stuck at a very simple problem it seems, but I don't get it done. Maybe I'm just confused atm, I don't know. But enough talk, here's the problem:

I'm trying to share a variable %default_settings (a dictionary or assoc array to be precise) between 2 perl files. The file that is executed by the user is called 'app.pl', the other file is called 'settings.pl'.

I do 'use strict' in my code and I'd like to continue doing so. I'm using perl 5.8.8 on Linux, btw. Here's the files (relevant sections):

##### start of app.pl ##### #!/usr/bin/perl my $settings_file = "./settings.pl"; use strict; our %default_settings; require $settings_file; # ERROR IS IN THE NEXT LINE: %settings is empty (because %default_sett +ings is empty) my %settings = %default_settings; ##### end of app.pl #####
##### start of settings.pl ##### %default_settings = ( $verbose => 1, $debug => 0, $help => 0, $game => "etqw" ); ##### end of settings.pl #####


That's it. It seems that require in Perl isn't equivalent to #include in C. In C, this works (and is what I want to do):

##### start example ##### spirit@threat:~/develop/example$ cat hello.cpp #include <stdio.h> #include <stdlib.h> #include "var.cpp" int main(int argc, char *argv[]) { printf("Hello world!\n"); printf("I'm %i years old!\n", var); return 1; } spirit@threat:~/develop/example$ cat var.cpp int var = 3; spirit@threat:~/develop/example$ g++ hello.cpp spirit@threat:~/develop/example$ ./a.out Hello world! I'm 3 years old! ##### end example #####


My problem is related to namespaces if I'm not mistaken. I searched a bit and a thread on another forum suggested to use 'our'. That's what I do in the code as you can see, but I obvisously managed to mess it up anyways. Ideas?

Thanks in advance and 'Hello' to everyone around! (This is my first post here) -

dichtfux

Replies are listed 'Best First'.
Re: Sharing a variable between (>=) 2 files
by Joost (Canon) on Nov 30, 2007 at 22:38 UTC
      Omg, you're right, that solved it. I feel a bit ashamed now - especially since that was my first question here.

      For future reference: using 'our' works, only problem with the code was that I was too stupid to use an assoc array in Perl. :(

      Thanks a lot!



      Btw: Do I now have a reputation of -1 because my question sucked or simply because I asked 1 question and answered 0 so far (and 0 - 1 = -1)?
        The "real" problem is, that using 'strict' and 'warnings' does not influence require()d files, so references to unused variables in your require()d file don't give warnings or errors.

        Btw: Do I now have a reputation of -1 because my question sucked or simply because I asked 1 question and answered 0 so far (and 0 - 1 = -1)?
        If you mean, why has your post a rep of -1, it probably means 1 of the "regulars" downvoted your post for some reason and none of them bothered to upvote it.

        Don't worry too much about it unless you get below -5 or so. Large (positive or negative) XP values indicate a sort of consensus. Small XP values change a lot.

        Btw: Do I now have a reputation of -1 because my question sucked or simply because I asked 1 question and answered 0 so far (and 0 - 1 = -1)?

        Your node has a reputation. Users get levels which are based on how many experience points they have (like in Dungeons & Dragons and several other role-playing games that followed its lead).

        Don't beat yourself up too much -- to the person that wrote the code it looks correct so the bug gets glossed over. It is almost a prerequisite that a second pair of eyes is needed to catch this kind of bug.

Re: Sharing a variable between (>=) 2 files
by moritz (Cardinal) on Nov 30, 2007 at 22:32 UTC
    Do you want to run both programs simultaneously? If yes, you need some inter process communication, see perlipc.

    If not, you can store your data in a file by using some serialization scheme, like YAML, Data::Dumper or XML.

    Or do you want to have a static data structure, that is used in both files? Then you probably want to write a module.

      I wanted the latter, but it turned out that everything worked well. Problem was something different, see the next post.
Re: Sharing a variable between (>=) 2 files
by LighthouseJ (Sexton) on Dec 03, 2007 at 21:20 UTC
    I changed the settings.pl to use regular key names instead of variables. If I want to interpret a file as perl code, I generally use an eval this way:
    $ cat settings.pl %default_settings = ( verbose => 1, debug => 0, help => 0, game => "etqw" ); $ cat app.pl #!/usr/bin/perl -w my $settings_file = "./settings.pl"; use strict; my %default_settings = (); open SETTINGS,$settings_file or die "cannot open $settings_file"; my @data = <SETTINGS>; eval "@data"; close SETTINGS; my %settings = %default_settings; print $_,'->',$settings{$_},"\n" foreach (keys %settings); $ ./app.pl verbose->1 game->etqw debug->0 help->0
    This allows me to not 'require' anything at startup but rather gives me the flexibility to do what I need if I need it.

    "The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why." -- `man perl`
Re: Sharing a variable between (>=) 2 files
by agent_smith (Scribe) on Dec 06, 2007 at 14:03 UTC
    Would AppConfig work for this type of thing?


    agent_smith