''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 Dump''''''Binding {
if ($output''''''mode = html) {
$Binding''''''Top = "
";
$Item''''''Open = "\n";
$Title''''''Open = "- ";
$Title''''''Close = "";
$Def''''''Open = "
- ";
$Def''''''Close = "";
$Item''''''Close = "";
$Binding''''''Bottom = "
\n";
}
elsif ($output''''''Mode = XML) {
$Binding''''''Top = "";
$Item''''''Open = "\n";
$Title''''''Open = " ";
$Binding''''''Bottom = "\n
\n";
}
local(*dict) = @_;
print $Binding''''''Top;
for (keys(%dict)) {
print $Item''''''Open;
print $Title''''''Open;
print $_;
print $Title''''''Close;
print $Def''''''Open;
print $dict{$_};
print $Def''''''Close;
print $Item''''''Close;
}
print $Binding''''''Bottom;
}
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''' Get''''''Script''''''Name {
return pop (split ('/', "$ENV{SCRIPT_NAME}");
}
$Script''''''Name = &Get''''''Script''''''Name;
''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:
($Script''''''Name) = $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_On''''''Start 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 $Link''''''Pattern and $Logo''''''Image - while others would change per invocation - such as $Script''''''Name and $Cooked''''''Input. In the ASP world, $Link''''''Pattern, $Logo''''''Image etc. would clearly belong in global.asa and not in the per-page scripts. Maybe the Perl equivalent would be to put sub Populate''''''Globals 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:
$Data''''''Base = "$ENV{ROOT_PATH}/$Script''''''Name";
$Default''''''Title = "$ENV{DEFAULT_TITLE}"
$Logo''''''Url = "$ENV{HTTP_ROOT}/$ENV{LOCAL_LOGO_URL}"
$Script''''''Url = "$ENV{HTTP_ROOT}/$ENV{LOCAL_SCRIPT_ROOT}"
$Lock''''''Directory = "$ENV{LOCAL_LOCK_ROOT}/$Script''''''Name"
$Signature''''''Url = "$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?
Get''''''Script''''''Name
Request''''''Lock
Release''''''Lock
Get''''''Todays''''''Formatted''''''Date
Lock''''''Directory - 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?
Dump''''''Binding
Escape''''''Meta''''''Characters
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