Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

using strict and a config file

by lex2001 (Sexton)
on Nov 12, 2001 at 12:30 UTC ( [id://124772]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I'm using strict; with a basic email program. I have some html code (in my main perl script) that prints out a form that a user fills out. I have also created a config file that creates an array called @fields. So for each element in @fields I want to print out:

<input type="text" name="$fields[0]" size="24">

and so ...

However when I use strict; I get the following error:

Global symbol "@fields" requires explicit package name at /Library/Web +Server/CGI-Executables/mailto/mailto.cgi line 106.

The error line corresponds to:

print<<HTML;

but it obviously has a problem with the my @fields reference.

How can I use strict; and also use a separate config file? I want the config file so that my scripts can be portable. Is it a bad idea to use config file? If not then how can I make them work under strict;

thanks in advance.

Edited 2001-11-11 by Ovid

Replies are listed 'Best First'.
Re: using strict and a config file
by snowcrash (Friar) on Nov 12, 2001 at 13:13 UTC
    You probably require the configfile. That happens during runtime, so the interpreter doesn't know about @fields when compiling the script. Either put the configuration into a perl module you import via use or tell the interpreter you have a variable named @fields.
    **** script.pl **** #!/usr/bin/perl use warnings; use strict; use vars qw(@fields); require "conf.pl"; print $fields[0]; **** conf.pl **** @fields = qw( FOO BAR BAZ );
    hth, snowcrash
      While using modules is a Good Thing™ in general, in this case it is a red herring. Whether you make your configuration into a module or not doesn't affect what you need to do to allow globals to be accessed while strict is on.
      I got my config file to work -- Thanks! I have a question concerning modules. If I where to create a module config file would it be possible to save it (and make it work) in the same directory that contains the main perl script or in my user directory -- such as: home/users/username From what I have read is seems like the best places are in paths like /usr/lib/perl5/ or /usr/local/apache/perl-lib/ but I may not be able to place them in these locations thanks...
        Yes it is possible. All you have to do is
        use lib '.';

        at the beginning of your program.

        This will make Perl look for modules in the current directory too.

      Just FYI...

      I believe use vars is now obsolete, as it has been superseded by our.

      --
      my one true love
Re: using strict and a config file
by busunsl (Vicar) on Nov 12, 2001 at 13:14 UTC
    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.

      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.

        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

        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: perlquestion [id://124772]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found