C:\cd windows
C:\windows>win.exe

Perl Breadcrumb Trail

Posted on January 22nd, 2016

What is the a Breadcrumb Trail?

The Perl CGI Breadcrumb Trail navigation script dynamically creates a navigation menu based on the users’ current location within the web site.

 Home >> Blog >> Perl Breadcrumb Trail

Showing the user exactly where they are in relation to the rest of the site, and allowing them to quickly get to any of the parent sections.

It’s written in Perl, so supported on 99.9% of all the web servers out there, Windows, Linux or UNIX. It’s totally free; you can do what you want with it.

Installation

The Perl Breadcrumb Trail navigation menu script is very easy to install, even if you have no Perl or web development experience, you shouldn’t have any problems.

Before you Start:

As the CGI script creates the menus dynamically, you need to follow a few simple rules to get the most form it. Although, if you follow good web development practices, you’ll probably find you already are.

Installing the Script

  1. Change the first line of the script to your path for Perl. If it’s
    #!/usr/bin/perl

    then you don’t need to change anything. If you’re not sure, ask your hosting provider

  2. FTP the script to you cgi-bin (or any folder that can run cgi scripts) in ASCII mode
  3. CHMOD the script permissions to 755, that’s owner; read, write, execute. Group and public; read and execute
  4. Use
    <!−−#exec cgi="/cgi-bin/navigation.cgi"−−>

    (change to the location you have the script) in your (x)HTML where you want the menus to appear

  5. That’s you! You should now have a working CGI dynamic navigation menu

Note, you can change the formatting of the menu in 2 ways. The first is to hard code it in the script. That will work, but its more flexible if you use an anchor style sheet. Then you can make it fit your site without having to change the script.

The Code

You can try to copy the Perl Breadcrumb Trail directly from the page, but I wouldn’t recommend that. There’s a lot of encoding and formatting going on there so it displays correctly on the web. if you do, copy directly to a plain text editor such as notepad or the superb Notepad++.

There are full download options below with no formatting issues.

#!/usr/bin/perl
##
## Navigation script, displays current location on site as a breadcrumb trail.
##
## V1.1 (fix for removing trailing / on some URLs on some servers - now ".shtml" not ".shtml/").
##
## Rules:
## Foldernames and filenames must be descriptive.
## Use "_" where spaces where you want spaces.
## Every folder must have an index file.

print "Content-type: text/plain\n\n";

##Variables
#Environmental variable for path script was called from.
my $location = $ENV{DOCUMENT_URI};
#This will be used as the URL for each link in the naviation.
my $url_nav;
#Will contain the full path (minus home), including xhtml.
my $full_nav;
#The description we show for each link.
my $desc_nav;


##Sort our data
#Remove the leading "/" on folders, need this so we dont get a blank elemement when we do the spilt.
$location =~ s/\///;
#Split location into array by "\". Each element will be level in menu.
@nav = split(/\//, $location);

##Creating the menu.
#Loop through every element in the array  
foreach $desc_nav (@nav)
	{
		#Check to see if its an index file, if it is, dont display it. You can add any here e.g. index.php, home.htm etc. 
		if ($desc_nav eq "index.shtml") {
			# Do nothing.
		} else {
			#Add one more menu level each time we loop. For the URL. Add trailing / for directories.
			$url_nav = "$url_nav$desc_nav/";
			
			#Replace "_" with " " on folders.
			$desc_nav =~ s/_/ /gi;
			#Make each word start with capital.
			$desc_nav =~ s/\b(\w+)\b/ucfirst($1)/ge;
			#Remove file extensions on filename, including ".".
			$desc_nav =~ s/\..*//;
			
			#Add one more menu level each time we loop. For xhtml.
			$full_nav = "$full_nav >> <a href=\"/$url_nav\">$desc_nav</a>";
		}
	}

#Remove the trailing / from file extension.
$full_nav =~ s/shtml\//shtml/gi;

#Print the finsihed xhtml menu.
print "<a href=\"/\">Home</a>$full_nav";

Download the Code

Download as plain text .txt file

Download as compressed .rar file

Download as compressed .zip file