Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

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. :)

#!/usr/bin/perl on windows

Thankyou perl monks for your wisdom:


http://www.perlmonks.org/?node_id=167481

I installed activestate in c:\usr, and now I can use the same shebang line on my windows laptop as on my linux box.

Life can be so extremely good!

Sunday, November 19, 2006

finding includes in html

For the second time in the a few months I've wanted to be able to fish through the includes in an shtml file in perl.

This while statement will itterate once for each time it matches a pattern for a virtual include, letting me check to see if the include is there, slurp it up, whatever:


while ($file_contents =~ m{<!--#?include\s+virtual="([^"]*)"\s*-->}gis){
# do something with $1 (a path)
}

Wednesday, November 01, 2006

perl array cheat sheet

How long is my array?



my @hw_array = ("hello","world","wassup?");

print "<br>size: ", scalar @hw_array; # 3
print "<br>size: " . @hw_array; # 3, scalar is implicit
print "<br>index of last: ", $#hw_array; # 2