Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: using strict and a config file

by busunsl (Vicar)
on Nov 12, 2001 at 13:14 UTC ( [id://124778]=note: print w/replies, xml ) Need Help??


in reply to using strict and a config file

First of: You have to declare your variables with my when you use strict. Like this:

my @fields;

How do you create the array from the config file?

Post a little more of your program and post your config file.

Replies are listed 'Best First'.
Re: Re: using strict and a config file
by lex2001 (Sexton) on Nov 12, 2001 at 13:33 UTC
    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>
      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.
      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124778]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-19 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found