Perl Questions

I've been programming for many years but am new to Perl. I'd like to learn about both Perl and Wiki by exploring (dissecting, reviewing) the Wiki source code.

DumpBinding question

Suppose that DumpBinding was altered as follows:

 sub DumpBinding {

if ($outputmode = html) { $BindingTop = "<hr><dl>"; $ItemOpen = "\n"; $TitleOpen = "<dt>"; $TitleClose = ""; $DefOpen = "<dd>"; $DefClose = ""; $ItemClose = ""; $BindingBottom = "</dl><hr>\n"; } elsif ($outputMode = XML) { $BindingTop = "<LIST>"; $ItemOpen = "\n"; $TitleOpen = "<ITEM name='"; $TitleClose = "' "; $DefOpen = "value='"; $DefClose = "'"; $ItemClose = "/>"; $BindingBottom = "\n</LIST>\n"; }

local(*dict) = @_; print $BindingTop; for (keys(%dict)) { print $ItemOpen; print $TitleOpen; print $_; print $TitleClose; print $DefOpen; print $dict{$_}; print $DefClose; print $ItemClose; } print $BindingBottom;

}

Seems to me this would allow "one-stop shopping" of output to either HTML or XML!

ScriptName question

1. Is there any particular reason for creating the array @path, which is never used again anywhere else in the source?

2. Is the script name always provided by the SCRIPT_NAME environment variable, regardless of the platform (Mac, Windows, Unix)?

3. If so, does the SCRIPT_NAME always delimit directories with a forward slash (Unix style), even on Windows (traditionally with backslash) or Mac (which uses colons)?

My suggestion:

 sub GetScriptName {

return pop (split ('/', "$ENV{SCRIPT_NAME}");

}

$ScriptName = &GetScriptName;

Besides, the use of pop here is unidiomatic. Remember, in Perl, indexing arrays or lists with negative numbers counts from the right. So:

  sub scriptName {
    return split( '/', $ENV{SCRIPT_NAME})[-1];
  }  # For more obscurity, remove the return keyword. 

--EricJablow

Note the use of double quotes in the sub as well as the use of & on the subroutine call is unnecessary, and in the latter case even potentially dangerous. All that and not to mention way too long:

 ($ScriptName) = $ENV{SCRIPT_NAME}=~/([^\\\/:]+)$/;

Which even has the added benefit of being platform tolerant. (Although File::Spec and friends are probably a better approach these days.)

--YjO

Perl startup question

Can a Perl script be run automatically whenever the web server first serves a page? Can this script set global variables which are available to all subsequent scripts invoked within the same web site?

In the Active Server Page environment, the declaration of globals could go into sub Application_OnStart in global.asa to be run automatically by the web server whenever the site is first accessed, and to remain in memory for subsequent accesses. Some of the global variables in the Wiki code shouldn't change between invocations of the script - such as $LinkPattern and $LogoImage - while others would change per invocation - such as $ScriptName and $CookedInput. In the ASP world, $LinkPattern, $LogoImage etc. would clearly belong in global.asa and not in the per-page scripts. Maybe the Perl equivalent would be to put sub PopulateGlobals into a module that would be referenced by the Wiki script?

Environment variables question

Is there something about Perl style that prevents the script from reading the following from the environment?

 In the shell:
 DEFAULT_TITLE = "Front Page"
 HTTP_ROOT = "http://c2.com"
 LOCAL_ROOT_PATH = "/usr/ward/pages"
 LOCAL_LOGO_URL = "img/wikibase.gif"
 LOCAL_SCRIPT_ROOT = "cgi"
 LOCAL_LOCK_ROOT = "/tmp"
 LOCAL_SIG_URL = "sig/ward.gif"

In the Wiki code: $DataBase = "$ENV{ROOT_PATH}/$ScriptName"; $DefaultTitle = "$ENV{DEFAULT_TITLE}" $LogoUrl = "$ENV{HTTP_ROOT}/$ENV{LOCAL_LOGO_URL}" $ScriptUrl = "$ENV{HTTP_ROOT}/$ENV{LOCAL_SCRIPT_ROOT}" $LockDirectory = "$ENV{LOCAL_LOCK_ROOT}/$ScriptName" $SignatureUrl = "$ENV{ROOT_PATH}/$ENV{LOCAL_SIG_URL}"

This would allow the code to be moved intact to another server, just change the environment variables to run the code at the other server.

Module questions

Seems to me that the following functions could be useful for other projects that have nothing to do with the Web. Why not create a Utility module containing them?

 GetScriptName
 RequestLock 
 ReleaseLock
 GetTodaysFormattedDate
 LockDirectory - get it from the environment

And the following functions could be useful for other Web projects that have nothing to do with Wiki. How about a Html module for them?

 DumpBinding
 EscapeMetaCharacters

Removing these general purpose routines would trim the Wiki script down to only the things directly having to do with Wiki.

Thanks for reading!


This would allow the code to be moved intact to another server, just change the environment variables to run the code at the other server.

Just because you have permission to run CGI on a server, doesn't mean you have shell access - and thus it might not be possible to set the environment variables like that. It'd make things difficult for releasing the code; people with free web hosts like ProHosting? (don't mean to advertise; it's just the one I ended up with) would complain about not being able to set it up properly. -- KarlKnechtel


EditText of this page (last edited December 14, 2004) or FindPage with title or text search