Mod_perl & do()

M

Mike Mimic

Hi!

I tryed to use do() function in mod_perl 1.27 but as I found out that
does not work. For example

test.pl:
#!/usr/bin/perl
my $test = 0;
do 'text.cf';
print "Content-type: text/html\n\n";
print $test;

test.cf:
$test = 1;

prints 0 and not 1. Files are in the same directory and there is '.' in
@INC.

scalar eval `cat test.cf` works but I really would not like to use this.

I use this for including configuration data.


Mike
 
J

Jay Tilton

: I tryed to use do() function in mod_perl 1.27 but as I found out that
: does not work. For example
:
: test.pl:
: #!/usr/bin/perl
: my $test = 0;

Declaring that $test with my() gives it file scope...

: do 'text.cf';
: print "Content-type: text/html\n\n";
: print $test;
:
: test.cf:
: $test = 1;

....so that $test is a completely different variable.
 
M

Mike Mimic

Hi!
Declaring that $test with my() gives it file scope...

: do 'text.cf';
: print "Content-type: text/html\n\n";
: print $test;
:
: test.cf:
: $test = 1;

...so that $test is a completely different variable.

I think that it is not true.

do 'text.cf' should simply include code (like C++ include pragma) so
that the code of the program would be

#!/usr/bin/perl
my $test = 0;
$test = 1;
print "Content-type: text/html\n\n";
print $test;

I have changed the program slightly to show the problem (test.cf is not
loaded).

test.pl:
#!/usr/bin/perl
my $test = 0;
print "Content-type: text/html\n\n";
do 'text.cf';
print $test;

test.cf:
print "Loaded.\n";
$test = 1;

It prints only 0.


Mike
 
A

Andreas Kahari

Hi!
Declaring that $test with my() gives it file scope...
[cut]
I think that it is not true.
[cut]

The manual for 'do' (perldoc -f do) says:

[...] code evaluated with "do FILENAME" cannot see lexicals
in the enclosing scope [...]
 
J

Jay Tilton

: do 'text.cf' should simply include code (like C++ include pragma) so
: that the code of the program would be
:
: #!/usr/bin/perl
: my $test = 0;
: $test = 1;
: print "Content-type: text/html\n\n";
: print $test;

Actually, it would be like

#!/usr/bin/perl
my $test = 0;
$main::test = 1; # package variable, not lexical
print "Content-type: text/html\n\n";
print $test;

: I have changed the program slightly to show the problem (test.cf is not
: loaded).
:
: test.pl:
: #!/usr/bin/perl
: my $test = 0;
: print "Content-type: text/html\n\n";
: do 'text.cf';
^^^^^^^
^

: print $test;
:
: test.cf:
^^^^^^^
^

: print "Loaded.\n";
: $test = 1;
:
: It prints only 0.

It would help if the filenames were the same.
Including checks on whether the do() succeeds is always a good idea.
 
M

Mike Mimic

Hi!

Jay said:
Actually, it would be like

#!/usr/bin/perl
my $test = 0;
$main::test = 1; # package variable, not lexical
print "Content-type: text/html\n\n";
print $test;

Changing my $test to our $test does the trick.
It would help if the filenames were the same.
Including checks on whether the do() succeeds is always a good idea.

Ups. Thanks. It was only a test script but you are right.

Thanks, it works now.


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top