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

In reply to Sharing a variable between (>=) 2 files by dichtfux

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.