Monday, April 14, 2008

Where am I?

Ok, windows knows where perl is now. But my htdocs is in a completely different place.  And the hostname changes too.

No idea if this will stand the test of time, but here's my figure out where I function right now, which appears to work on both my windows XP laptop and the linux host I use most often:

(my $script, my $path, my $script_url, my $script_url_path, my $host) = figure_out_script_and_path();

sub figure_out_script_and_path{
    my $script, my $path, my $script_url, my $script_url_path, my $host;

    if ($0 =~ m#^\w:#){ # should be true on a windows host
        $script = $0;
            $script =~ s#^\w:.*\/##; #remove path
        $path = $0;
            $path =~ s#(^\w:.*)\/[^\/]*$#$1#; # remove name of script
    }
    else { # assume path starts with a slash:
        $script = $0;
            $script =~ s#^\/.*\/##; #remove path
        $path = $0;
            $path =~ s#(^\/.*)\/[^\/]*$#$1#; # remove name of script
    }

    $script_url = $ENV{"SCRIPT_NAME"};
    $script_url_path = $script_url;
    $script_url_path =~s#(^\/.*)\/[^\/]*$#$1#;
    $host = $ENV{"SERVER_NAME"};

    return ($script, $path, $script_url, $script_url_path, $host);

}

Would you believe that windows live writer doesn't have a "code" option in the format menu? And I'm afraid I'm too lazy to edit the source. :)

No comments: