CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
loading dynamic configuration variables

 



tiaml
Deleted

Jun 1, 2000, 10:13 PM

Post #1 of 4 (536 views)
loading dynamic configuration variables Can't Post

I need to alter my Perl cgi compile/runtime variables according to 1st-contact by client using href link.
eg, http://fred.com/cgibin/script.cgi?id=ABC
Using params value I want Perl to get variables from file ABC.rc so that scripts code will process according to environment needed by user id ABC.
Not able to understand use/require/Camel-page285. NO code needed, just pre-sets for variables used in script.
I am using 'use strict;' and '-w Perl switch'
Help greatly appreciated - mind is totally blasted away by this and can't get to 1st base with the mechanics/syntax!!!
Thanks
Timal


japhy
Enthusiast

Jun 1, 2000, 10:24 PM

Post #2 of 4 (536 views)
Re: loading dynamic configuration variables [In reply to] Can't Post

The data you request is stored in the $ENV{QUERY_STRING} variable. If it is simple enough, you can parse it yourself:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


($file = $ENV{QUERY_STRING}) =~ s/id=//;
require "$file.rc";
</pre><HR></BLOCKQUOTE>

You can use the CGI.pm module:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use CGI;
$file = CGI::param('id');
require "$file.rc";
</pre><HR></BLOCKQUOTE>

Or, you can send the data to your program without the leading id= part (just "foo.cgi?username") and get the value from $ARGV[0]:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$file = $ARGV[0];
require "$file.rc";
</pre><HR></BLOCKQUOTE>


tiaml
Deleted

Jun 3, 2000, 7:34 AM

Post #3 of 4 (536 views)
Re: loading dynamic configuration variables [In reply to] Can't Post

Thank you for such multi-faceted reply!! Have no problem with getting param/vals into cgi... but your tip on /foo.cgi?username was indeed interesting and I'll use it! However I think is problem with compile/run time Perl processing regards what is in scope at this time since only one of the following (using eval() ) actually got part-way to what I want:
-------------------------------------------
FILE: testrc1
CONTENTS:
#!... -w
use strict;
my $UID = $ARGV[0];
require "$UID.rc";
print "$a,$b,$c\n";
exit(0);
----------
FILE: abc1.rc
CONTENTS:
my $a = 1;
my $b = 2;
my $c = 3;
1;
===========
RUN-IT:
$ testrc1 abc1
RESULTS:
Global symbol "$c" requires explicit package name at ./testrc1 line 5
COMMENT:
Ok, seems asif scope in require'd file: abc1.rc deemed local to file and no explicit import of $a,$b,$c into main::
I am probably way off here, but above code
is what I'd like to have ;-)
IE, main:: code uses variables which it does not know about values UNTIL process-in the username.rc file which 'somehow' loads values into the symbol table etc... so that Perl not get upset when compile...

-------------------------------------------

FILE: testrc2
CONTENTS:
#!... -w
use strict;
my $UID = $ARGV[0];
print eval `cat $UID.rc`, "\n";
print "$a,$b,$c\n";
exit(0);
----------
FILE: abc2.rc
CONTENTS:
$a = 4;
$b = 5;
$c = 6;
1;
===========
RUN-IT:
$ testrc2 abc2
RESULTS:
1
4,5,6
COMENT:
Nice... but in main:: I've had to explicitly declare $a,$b,$c. Since eval scopes locally no point in having my $a=1;,... in username.rc
Perhaps this it? IE, all username resource files need decs like: $USER_NAME = 'FRED';
$USER_EMAIL = 'fred@blogs.com'; ...
Then in my main:: I have empty decs like:
my ($USER_NAME, $USER_EMAIL); ...
Of couse I would check for username.rc 'being-there' and that eval returns ok else shout if not !
==========================================
What you think? I really thought could do the 'require' thing and Perl (at compile time) load symbols in (with presets) and then when Perl compiles main:: code which uses these symbols - well, that the world would be a better place!! ;-)

Regards,
Tim


[This message has been edited by tiaml (edited 06-03-2000).]


japhy
Enthusiast

Jun 4, 2000, 7:22 PM

Post #4 of 4 (536 views)
Re: loading dynamic configuration variables [In reply to] Can't Post

The problem with TESTRC1 is that your require()d file declares its variables with my(), which means they're not seen OUTSIDE that file -- not what you'd like. And in your main program, $c was never declared using my(), so strict.pm got upset. By the way, $a and $b are exempt from strict.pm's tyranny, since they're used in sort() subroutines. Variables declared via my() are not entered into the symbol table at all. Only global variables are found in the symbol table.

If you want to get the effect of require() at compile-time (I don't know exactly why that's important to you), then wrap it in a BEGIN { } block:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


BEGIN { require "config.pl" }
</pre><HR></BLOCKQUOTE>

Here is an example that DOESN'T work, and an explanation why:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# config.pl
$this = 10;
$that = 20;

# main.pl
#!/usr/bin/perl -w
use strict;
my ($this,$that);
require "config.pl";
print "$this $that\n";
</pre><HR></BLOCKQUOTE>

This will warn you that $this and $that are uninitialized. The reason is because config.pl is setting $main::this and $main::that, while main.pl is using lexically scoped variables $this and $that, not the global variables $main::this and $main::that.

Here is a working example:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# config.pl
# this file is free from 'use strict'
$this = 10;
$that = 20;

# main.pl
#!/usr/bin/perl -w
use strict;
use vars qw( $this $that );
require "config.pl";
# or
# BEGIN { require "config.pl" }
# for compile-time loading
print "$this $that";
</pre><HR></BLOCKQUOTE>

config.pl is free from main.pl's 'use strict' limitation because the code is executed in a NEW scope, not within main.pl's scope, and strict.pm's effects are scope-based. The variables we define in config.pl are in main::. Then, in main.pl, we tell strict.pm to allow $this and $that without having to give an explicit package name. The vars.pm pragma basically tells Perl that these symbol names are to be assumed to be in the package you're in, so saying:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use strict;
use vars '$foo';
$main::foo = 20;
print $foo;
</pre><HR></BLOCKQUOTE>

will print 20. However, it does NOT alias '$foo' to mean '$currentpackage::foo':

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use strict;
package Bar;
use vars '$this';
# tells Perl that $this in package
# Bar means $Bar::this
$this = 20;

package Foo;
$this = 30; # strict complains here
</pre><HR></BLOCKQUOTE>

You'd need to add another call to vars.pm after you start package Foo:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


use strict;
package Bar;
use vars '$this';
$this = 20;
package Foo;
use vars '$this';
$this = 30;
</pre><HR></BLOCKQUOTE>

That's enough for now. I'm tired, and I want to sleep.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives