Including files to pick up variables

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Including files to pick up variables

    Hey there, what is the best way of including a Perl so that the contents
    of it override the existing variables. For example, I have:

    <main Perl program>
    ...
    my $comment = "Default\n";
    my $resDir = "results";
    ...
    if (@ARGV) {
    my $c = shift;
    $config = "testing/$c.pm";
    unless (my $ret = do $config) {
    die "couldn't parse $config: $@" if $@;
    die "couldn't do $config: $!" unless defined $ret;
    die "couldn't run $config" unless $ret;
    }
    # Get the output filehandle
    $output = getOutputFH();
    }
    ...

    <a file in testing/>
    $comment = "A not quite default test.";
    my $resName = 'notdefault.res';

    sub getOutputFH() {
    open($fh, ">${resDir}/${resName}.res") or ***
    die "Can't open results file: $!";
    return $fh;
    }
    1;

    I would like the value of $comment defined in the testing file to
    override the value defined in the main program. I would also
    like for the value of $resDir to be picked up by the getOutputFH() sub,
    however neither of these happens. What is the best way of doing what I'm
    trying to acheive here?
    (Obviously, in real life, more things are going to be set, but if I can
    make this example work, it is a short step to getting the rest working)

    The error I get is "Use of uninitialized value in concatenation (.) or
    string" at the point marked '***'. I assume it is $resDir not being
    defined.

    I know that I could simply have a function that returns a hash (list)
    that I walk through to set the variables, but I want to keep this as
    simple as possible, as I'll be creating lots of little versions of these
    testing files, and the more stuff in them, the more there is to go
    wrong.

    --
    Robin <robin@kallisti.net.nz> JabberID: <eythian@jabber.org>

    Hostes alienigeni me abduxerunt. Qui annus est?

    PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFALEQMzTjgendttmMRApu0AJ9jo66KTGO+GTTaF25u64 FLKpk6uQCfbdGe
    K0JRuNJlTXtDmEo1mgwHRew=
    =4YF+
    -----END PGP SIGNATURE-----

    Robin Sheat Guest

  2. Similar Questions and Discussions

    1. Including files with a control
      I am attempting to make an ASP.NET server control (inheriting from System.Web.UI.Control) and, when the control is dragged from the Visual Studio...
    2. Including text files in Modules
      Greetings, Q1. Is it possible to include txt files (as data sources) inside a Perl package? (not to dump the contents as some kind of variables;...
    3. including files in other directories
      I have some code running on windows on my local machine that uses the jpgraph image library. I include the jpgraph libs using include...
    4. strategies for including files when you're not allowed to make assumptions about directory names
      2 Questions: 1.) Can anyone think of a way to speed up this function? It is terribly slow. I plan to reduce the number of directories to 3, which...
    5. including html files in a .html page
      4bb: Investigate Server-side includes. If your host supports them, they do just what you want. Be aware that the file included MUST BE AN...
  3. #2

    Default Re: Including files to pick up variables

    On Fri, Feb 13, 2004 at 04:27:08PM +1300, Robin Sheat wrote:
    > Hey there, what is the best way of including a Perl so that the contents
    > of it override the existing variables. For example, I have:
    With all the help of those who replied ;) I worked it out. For the
    benefit of others:
    > my $comment = "Default\n";
    needs to be:
    our $comment = "Default\n";

    Then everything just works.

    --
    Robin <robin@kallisti.net.nz> JabberID: <eythian@jabber.org>

    Hostes alienigeni me abduxerunt. Qui annus est?

    PGP Key 0x776DB663 Fingerprint=DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFALZBzzTjgendttmMRAn/NAKC7Vwy3gRrIfMlBSB1eJ9PsC1h7wwCfWXxE
    WsITUrLFnHXkOYCUvV/TPgI=
    =F5+A
    -----END PGP SIGNATURE-----

    Robin Sheat Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139