Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl file names and extensions

by bradcathey (Prior)
on Aug 03, 2004 at 15:14 UTC ( #379687=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monasterians,

We are all familiar with the conventional Perl file extensions of .pl, .plx, .pm, etc. But what about no extensions? At the risk of being too persistent in trying to have a clean URL and not reveal my "secrets" (see my recent node), is there any thing suggesting "bad practices" to have:

http://www.domain.com/cgi-bin/login?mode=new

versus

http://www.domain.com/cgi-bin/login.plx?mode=new

or, as my previous node suggests:

http://www.domain.com/acme/login?mode=new

Thoughts? Thanks, and I promise not to bring it up again, I just want to establish a standard way of doing this, stick with it, and not have it come back to bite me in the butt later.


—Brad
"Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton

Replies are listed 'Best First'.
Re: Perl file names and extensions
by davorg (Chancellor) on Aug 03, 2004 at 15:17 UTC

    Generally, I think it's a good idea for Perl programs (or, indeed, any programs) not to have an extension. Why should your users know or care what language a program is written in.

    Your last example wouldn't work without some kind of mod_rewrite magic, but something that I've used successfully in the past is to create directories containing an index.cgi so you can use URLs like:

    http://www.domain.com/acme/login/?mode=new
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      You don't need mod_rewrite for that. You could use <Files> or <Location> directives; the latter are more flexible but only work in the main httpd.conf, while the former can be dropped into a .htaccess.

      Makeshifts last the longest.

      davorg, thanks for this example. I have seen URL's like this, but could not figure out how it worked. However, I'm still not sure of the mechanics.

      I assume that the index.cgi is in the directory "login" in your example, but does the lone "?" fire the cgi without the need of a program name, as in: /login/index.cgi?mode=new. I.e., the "index.cgi" is not needed, right?

      This is the first time I've been exposed to mod_rewrite and have done some Super Searching and found this by Mad Hatter. Would something like this work with your example?

      RewriteEngine on RewriteBase /subDir/ # only redirect if the file requested isn't index.cgi # capture anything else and redirect it to index.cgi RewriteCond %{REQUEST_FILENAME} !^index.cgi RewriteRule ^(.+)/?$ index.cgi?rm=$1 [L]

      Well, I'll try this to see if it works differently or better than my usual .htaccess:

      Options +ExecCGI SetHandler cgi-script

      —Brad
      "Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton
        You are over thinking it. url.tld/foo/bar/, in apache, attempts to open whatever the default page for a folder is. In most installs of apache, url.tld/foo/bar/ resolves to url.tld/foo/bar/index.html However, you can change which file extensions you want the index page to use, for example, change it to .pl or .cgi. Then url.tld/foo/bar/ resolves to url.tld/foo/bar/index.pl and anything after the question mark is passed to that cgi. You'll notice perlmonks does something similar. The default page is index.pl so perlmonks.org resolves to perlmonks.org/index.pl so perlmonks.org?foo=bar resolves to perlmonks.org/index.pl?foo=bar
Re: Perl file names and extensions
by nite_man (Deacon) on Aug 03, 2004 at 15:51 UTC

    Let's see how do it with mod_perl (example from mod_perl documentation):
    This is an URL on website -

    http://example.com/news/20021031/09/index.html
    but you need convert it as
    http://example.com/perl/news.pl?date=20021031&id=09&page=index.html
    You can use for that simple mod_perl handler:
    package MyApache::RewriteURI; use strict; use warnings; use Apache::RequestRec (); use Apache::Const -compile => qw(DECLINED); sub handler { my $r = shift; my($date, $id, $page) = $r->uri =~ m|^/news/(\d+)/(\d+)/(.*)|; $r->uri("/perl/news.pl"); $r->args("date=$date&id=$id&page=$page"); return Apache::DECLINED; } 1;
    Also, you should add in http.conf:
    PerlTransHandler +MyApache::RewriteURI
    So, first URL is easier to understand for user than second. Also, based on this you can create something like selector to process and redirect HTTP requests according your rules.

    ---
    Schiller

    It's only my opinion and it doesn't have pretensions of absoluteness!

Re: Perl file names and extensions
by lhoward (Vicar) on Aug 03, 2004 at 15:24 UTC
    If you have control of your webserver, there are tricks you can play there too. on apache I have one server set up to handle .html files as if they were php (so nobody knows I use php) and mod_rewrite to obfuscate filename extensions and CGI parameters (and the fact that its even a cgi-bin script). i.e. mapping /foo/bar/login to /cgi-bin/login.cgi?a=foo&b=bar L
      Ah... I see! But I only have limited control over my webserver, that's what I get for being a cheapskate...
Re: Perl file names and extensions
by pelagic (Priest) on Aug 03, 2004 at 15:29 UTC
    If your webserver knows what to do whith it, it must be ok.
    For me the only reason to use suffixes as .pl .pm etc. is for humans beeing able to distinguish the probable file use from its name. But I actually prefer other ways e.g. directories. Working in unix systems you anyway can't rely on the suffix only.
    There is a caveat with require; see require.

    pelagic
Re: Perl file names and extensions
by katgirl (Hermit) on Aug 03, 2004 at 15:24 UTC
    How would this actually work? Perhaps it's just me, but that url would give a 500 error, which I would then have to capture using another script and redirect to the proper url... is there Another Way To Do It?

      No no not all - it just depends on the configuration of the webserver, of course if you are on windows which depends on the extension to know what to do with the file then you are a bit stuck, short of some ISAPI filter magic or creating some completely different extension, but with , say Apache, this is common practice - of course if you are using mod_perl the thing in the 'file' position at the end of the url may not actually be a physical resource at all.

      /J\

      Yeah. Use mod_rewrite.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://379687]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2019-08-23 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?