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

On one perl page that prints out links to itself with ?variable=value on them so it prints out new info.
This value is a directory name. The info is the contents of the directory.
So I can go to this file and click links to view the contents of other directories.
At the top of this multi-tasking perl page is a printed out line of what directory on the server I am viewing, this is printed out from a hard set variable and the sent value.
I want to be able to use this printed out line to have each directory name turned into a link that links again to this same file but with the new value.
its a bit like this:

#!/usr/bin/perl use CGI; $query = new CGI; $dire = $query->param("dire"); print $query->header ( ); $live = "/home/client/www/site"; print "$live<a href=\"./index.cgi?dire=$dire\">$dire</a>"; print "<br><br>" #then my readdir which reads $live$dire and prints out the contents

So when I click on the linked part it takes me to view the contents of that directory. Simple.

This works fine when I am in "/html" and $dire is "/html" But not when I am in "/html/code" and $dire is "/html/code".
When it is this case and it prints out
home/client/www/site/html/code

I want to be able to click on the "html" part of that line and have it take me to "./index.cgi?dire=/html"
or to click the "code" part of that line and have it take me to "./index.cgi?dire=/html/code"

You get the idea, i want to be able to move back directories. How can I split up $dire and print it out as separate links?

Thank you for any help or pointers. I tried my best to describe the problem but if it's not clear let me know!

Replies are listed 'Best First'.
Re: Abs directory turned into links
by grep (Monsignor) on Feb 07, 2002 at 06:35 UTC
    I would recommend File::Basename:
    #!/usr/bin/perl -w use strict; use File::Basename; my $dir = "/usr/bin/mighty/long/path/perl"; my $end = basename($dir); while ($dir ne "/" and $dir ne "") { print "$dir $end\n"; $dir = dirname($dir); $end = basename($dir); }


    grep
    grep> chown linux:users /world
Re: Abs directory turned into links
by Zaxo (Archbishop) on Feb 07, 2002 at 08:53 UTC

    Apache will do the file listing for you if there is no DirectoryIndex in a publicly available directory.

    If you want more control, I'd suggest glob. or File::Find to get the lists and File::Basename to guard against malicious input and dress up the text, as ++grep suggests. Here are some fragments to get you started:

    # Specify what directory is meant by a tag. # Bogus tags will fail.to see anything undesirable my %dirs = ( foo => '/foo', bar => '/foo/bar', baz => '/quux', ); # restrict to html my @fles = glob $ENV{DOCUMENT_ROOT}.$dirs{$query->param('dire')}.'/*.h +tml'; # print GET urls for this script print join " / ", map {$query->a( { href=>$query->url() . "?dire=$_"}, $_)} keys % +dirs; # print file list use File::Basename; print map {$query->a({ href => $base_uri . substr( ($_, length( $ENV{DOCUMENT_ROOT} )) }, basename( $_, '.html') ) . $query->br() . "\n" } @fles;
    Warning, this is off the top of my head. Mostly untested.

    After Compline,
    Zaxo