You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Mag Gam <ma...@gmail.com> on 2008/02/07 04:06:05 UTC

Question about open()

Hi All,

I have been using mod_perl, and I have several perl-cgi files that use the
open(). I use this function to open/include my header, footer, basically a
poor man template system. Currently, when I open a file I have to use the
absolute  path (/var/www/appname/top.inc). Is it possible for me to use just
'top.inc'? I think it trying to read from DocumentRoot, which is causing the
issue. Has anyone ever faced this?

TIA

Re: Question about open()

Posted by "Roberto C. Sánchez" <ro...@connexer.com>.
On Wed, Feb 06, 2008 at 10:47:08PM -0500, Mag Gam wrote:
> What is global.asa?
> 
> Sorry, I am somewhat new at this...
> 
My mistake.  I am using Apache::ASP (which is built on mod_perl) for
my pages.  Sorry for the confusion.

Regards,

-Roberto

-- 
Roberto C. Sánchez
http://people.connexer.com/~roberto
http://www.connexer.com

Re: Question about open()

Posted by Mag Gam <ma...@gmail.com>.
What is global.asa?

Sorry, I am somewhat new at this...


On Feb 6, 2008 10:10 PM, Roberto C. Sánchez <ro...@connexer.com> wrote:

> On Wed, Feb 06, 2008 at 10:06:05PM -0500, Mag Gam wrote:
> > Hi All,
> >
> > I have been using mod_perl, and I have several perl-cgi files that use
> the
> > open(). I use this function to open/include my header, footer, basically
> a
> > poor man template system. Currently, when I open a file I have to use
> the
> > absolute  path (/var/www/appname/top.inc). Is it possible for me to use
> just
> > 'top.inc'? I think it trying to read from DocumentRoot, which is causing
> the
> > issue. Has anyone ever faced this?
> >
> I just set something like this in global.asa:
>
> my $base_dir = cwd . '/';
>
> Then I can make the $base_dir available globally and just use that
> anywhere I need to refer to an on disk file.
>
> Regards,
>
> -Roberto
>
> --
> Roberto C. Sánchez
> http://people.connexer.com/~roberto<http://people.connexer.com/%7Eroberto>
> http://www.connexer.com
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQFHqnaQ5SXWIKfIlGQRAui5AKCb+dxo6Se7QhTUsKYnIbYUdzcoSwCguzi8
> +yLFnkIF7CcF2aw4eBc/jnE=
> =y9pZ
> -----END PGP SIGNATURE-----
>
>

Re: Question about open()

Posted by "Roberto C. Sánchez" <ro...@connexer.com>.
On Wed, Feb 06, 2008 at 10:06:05PM -0500, Mag Gam wrote:
> Hi All,
> 
> I have been using mod_perl, and I have several perl-cgi files that use the
> open(). I use this function to open/include my header, footer, basically a
> poor man template system. Currently, when I open a file I have to use the
> absolute  path (/var/www/appname/top.inc). Is it possible for me to use just
> 'top.inc'? I think it trying to read from DocumentRoot, which is causing the
> issue. Has anyone ever faced this?
> 
I just set something like this in global.asa:

my $base_dir = cwd . '/';

Then I can make the $base_dir available globally and just use that
anywhere I need to refer to an on disk file.

Regards,

-Roberto

-- 
Roberto C. Sánchez
http://people.connexer.com/~roberto
http://www.connexer.com

Re: Question about open()

Posted by Perrin Harkins <pe...@elem.com>.
On Feb 10, 2008 12:37 PM, Mag Gam <ma...@gmail.com> wrote:
> I am trying to change DocumentRoot because, currently I am using open()  to
> load templates for my website. I have header, menu, footer in 3 seperate
> files, and I generate content like that. It works fine now, just not too
> dynamic when I want to move the stuff around. I have
> open("/var/www/perl/header.file") hardcoded, which is a pain. I just want
> open ("header.file").
>
> If I do open("header.file") its trying to read from DocumentRoot ie.
> /var/www/header.inc, which does not exist.

That's not related to DocumentRoot.  The open() command opens files
relative to your current working directory.  If you use
ModPerl::RegsitryPreFork, that will be the same directory your script
is in.  Alternatively, you can chdir() to the directory you want.
What you're trying to do right now with DocumentRoot should be done
with chdir() instead.

- Perrin

Re: Question about open()

Posted by Foo JH <jh...@extracktor.com>.
I do keep my templates in other directories also.

1. If your directories are relative to the document root, and you can 
take advantage of this fact by rendering a variable to remember the 
template path.
2. If your template directories have nothing in common with the document 
root, then you can set the variable via SetPerlVar within http.conf. 
That takes the 'hard' out of your code. :)




Mag Gam wrote:
> Thanks for the quick response Perrin.
>  
> I am trying to change DocumentRoot because, currently I am using 
> open()  to load templates for my website. I have header, menu, footer 
> in 3 seperate files, and I generate content like that. It works fine 
> now, just not too dynamic when I want to move the stuff around. I have 
> open("/var/www/perl/header.file") hardcoded, which is a pain. I just 
> want open ("header.file").
>  
> If I do open("header.file") its trying to read from DocumentRoot ie. 
> /var/www/header.inc, which does not exist.
>  
>  
>
>
>  
> On Feb 10, 2008 12:32 PM, Perrin Harkins <perrin@elem.com 
> <ma...@elem.com>> wrote:
>
>     On Feb 10, 2008 12:24 PM, Mag Gam <magawake@gmail.com
>     <ma...@gmail.com>> wrote:
>     > my $docroot = $r->document_root('/var/www/html/perl');
>
>     Why are you trying to change the DocumentRoot?
>
>     - Perrin
>
>


Re: Question about open()

Posted by Mag Gam <ma...@gmail.com>.
Thanks for the quick response Perrin.

I am trying to change DocumentRoot because, currently I am using open()  to
load templates for my website. I have header, menu, footer in 3 seperate
files, and I generate content like that. It works fine now, just not too
dynamic when I want to move the stuff around. I have
open("/var/www/perl/header.file") hardcoded, which is a pain. I just want
open ("header.file").

If I do open("header.file") its trying to read from DocumentRoot ie.
/var/www/header.inc, which does not exist.





On Feb 10, 2008 12:32 PM, Perrin Harkins <pe...@elem.com> wrote:

> On Feb 10, 2008 12:24 PM, Mag Gam <ma...@gmail.com> wrote:
> > my $docroot = $r->document_root('/var/www/html/perl');
>
> Why are you trying to change the DocumentRoot?
>
> - Perrin
>

Re: Question about open()

Posted by Perrin Harkins <pe...@elem.com>.
On Feb 10, 2008 12:24 PM, Mag Gam <ma...@gmail.com> wrote:
> my $docroot = $r->document_root('/var/www/html/perl');

Why are you trying to change the DocumentRoot?

- Perrin

Re: Question about open()

Posted by Mag Gam <ma...@gmail.com>.
I am taking the Apache2::Request method, and will integrate the DB solution
as Chandra mentioned.



#!/usr/bin/perl -w
use strict;
use CGI;
use Apache2::Request;
use Data::Dumper;

print "Content-type: text/html\n\n";
my Apache2::Request $r = shift;
my $docroot = $r->document_root('/var/www/html/perl');
print $docroot;

For some reason, its still defaulting to /var/www/html. I need it to goto
/var/www/html/perl

Any thoughts?




On Feb 7, 2008 5:14 PM, Chandrakumar Muthaiah <su...@chandrasudha.net>
wrote:

>
> Perrin Harkins wrote:
> > On Feb 6, 2008 10:06 PM, Mag Gam <ma...@gmail.com> wrote:
> >
> >> Currently, when I open a file I have to use the
> >> absolute  path (/var/www/appname/top.inc). Is it possible for me to use
> just
> >> 'top.inc'?
> >>
> >
> > You can either use DocumentRoot
> > (
> http://perl.apache.org/docs/2.0/api/Apache2/RequestUtil.html#C_document_root_
> )
> > or use ModPerl::RegistryPreFork which will chdir to the directory your
> > script is in just like a normal CGI script does.  That would let you
> > use files relative to the script.
> >
> > - Perrin
> >
>
> Few quirks with document root. Your application may not be hosted under
> document root. you may have all your templates and customizations some
> where else.
>
> I think better option would be to define the template/support files
> location in Directory Configuration or in a Database configuration
> table. That would be more staple solution.
>
> in your .htaccess or on the Location/Directory Configuration put a line
> that says
>
> PerlSetVar AppsSupportFiles "/home/httpd/support/site1/template"
>
>
> and access this parameter from your $r->dir_config('AppsSupportFiles')
>
>
> I think this may be more customizable and scalable solution to your
> problem
>
> -Chandra
>

Re: Question about open()

Posted by Chandrakumar Muthaiah <su...@chandrasudha.net>.
Perrin Harkins wrote:
> On Feb 6, 2008 10:06 PM, Mag Gam <ma...@gmail.com> wrote:
>   
>> Currently, when I open a file I have to use the
>> absolute  path (/var/www/appname/top.inc). Is it possible for me to use just
>> 'top.inc'?
>>     
>
> You can either use DocumentRoot
> (http://perl.apache.org/docs/2.0/api/Apache2/RequestUtil.html#C_document_root_)
> or use ModPerl::RegistryPreFork which will chdir to the directory your
> script is in just like a normal CGI script does.  That would let you
> use files relative to the script.
>
> - Perrin
>   

Few quirks with document root. Your application may not be hosted under 
document root. you may have all your templates and customizations some 
where else.

I think better option would be to define the template/support files 
location in Directory Configuration or in a Database configuration 
table. That would be more staple solution.

in your .htaccess or on the Location/Directory Configuration put a line 
that says

PerlSetVar AppsSupportFiles "/home/httpd/support/site1/template"


and access this parameter from your $r->dir_config('AppsSupportFiles')


I think this may be more customizable and scalable solution to your problem

-Chandra

Re: Question about open()

Posted by Perrin Harkins <pe...@elem.com>.
On Feb 6, 2008 10:06 PM, Mag Gam <ma...@gmail.com> wrote:
> Currently, when I open a file I have to use the
> absolute  path (/var/www/appname/top.inc). Is it possible for me to use just
> 'top.inc'?

You can either use DocumentRoot
(http://perl.apache.org/docs/2.0/api/Apache2/RequestUtil.html#C_document_root_)
or use ModPerl::RegistryPreFork which will chdir to the directory your
script is in just like a normal CGI script does.  That would let you
use files relative to the script.

- Perrin