in reply to Re: using strict and a config file
in thread using strict and a config file

Here is my config_mailto.txt file:
my @fields = ("to", "from", "subject", "message"); my @field_names; @field_names = ("To", "From", "Subject", "Message"); my $page_title = "Mailto";
Here is how I'm using @fields in my main perl script:
<tr> <td>From:</td> <td><select name="$fields[1]" size="1"> more code... </select></td> </tr> <tr> <td>Subject:</td> <td><input type="text" name="$fields[2]" size="24"></t +d> </tr> <tr> <td>Message:</td> <td><textarea name="$fields[3]" cols="40" rows="4"></t +extarea></td> </tr> <tr> <td>Send it!:</td> <td><INPUT TYPE="submit" NAME="action" VALUE="Send"></ +td>

Replies are listed 'Best First'.
Re: Re: Re: using strict and a config file
by busunsl (Vicar) on Nov 12, 2001 at 13:55 UTC
    Remove the my declaration from your config file.

    Declare your arrays with our in your program:

    our @fields; out @field_names;
    Then require your config file:
    require 'config_mailto.txt';
    That should work.

    Have a look at perldoc -f our and perldoc -f my for more info.

      There are a lot of people still using version 5.005_03, in which case the our solution is simply not an option. But even if you do have 5.6.x installed, I still am not convinced that it is a good idea. See Why is 'our' good? for some of my doubts.
        I agree. In fact I think a better solution would be not to have Perl code in the config file but name/value pairs and read them into a hash.
        Perhaps even better use a CPAN module for the config file handling.
Re: Re: Re: using strict and a config file
by mce (Curate) on Nov 12, 2001 at 13:55 UTC
    Hi,
    I have a similar setup and the way I work is fully qualifing the variable using the package name. This will prevent strict from complaining. For Example: The config_mail file
    Package MailConfig; use strict; @MailConfig::fields=qw /to from subject message/;
    etc... And in your actual program, do:
    use strict; require "config_mail.pl"; ...your print code... <tr> <td>From:</td> <td><select name='$MailConfig::fields[1]" size="1"> more code... </select></td> </tr> <tr> <td>Subject:</td> <td><input type="text" name="$MailConfig::fields[2]" size="24"></t +d>
    This is the most readable solution, as you know where to look for when you want to change a variable.
    Dr. Mark Ceulemans

    Senior Consultant

    IT Masters, Belgium

      Glancing at your print code strongly suggests to me that you have a lot to learn about separation of presentation and content.

      A good first link I would therefore recommend is the following paper on how to use the template toolkit in building web sites.

Re: using strict and a config file
by guha (Priest) on Nov 12, 2001 at 16:12 UTC
    How about this setup which I think is clean way to do it.

    Content of the config file, say cfg.cfg

    %cfg = (To => 'to', From => 'from', Subject => 'subject', Message => "message\nmsg2\n", Page_Title => 'MailTo'); %cfg;

    Then you do something like this in the main program.

    use strict; . . my $file = 'cfg.cfg'; my %cfg; my $return = do $file; unless ($return) { die "couldn't parse $file: $@" if $@; die "couldn't do $file: $!" unless defined $return; die "couldn't run $file" unless $return; } %cfg = %{$return};

    And then later in your code use it as

    <td><select name="$cfg{From}" size="1"> more code... </select></td> </tr> <tr> <td>Subject:</td> <td><input type="text" name="$cfg{Subject}" size="24"> +</t
    This is easily expanded to have several different configs in the same file.
    Ex $cfg{Guest}{To}, $cfg{Admin}{Page_Title}, etc etc