How to make online songs repository using Perl

Problem Scenario:
Write a Perl script which will search all sub directories and its files under a given sub-directory and would do the following:

  1. Display all directories on a web page
  2. When user clicks on any of the list directories show files under it (toggle things)
  3. When user clicks on file, it should play that song using yahoo media player
  4. You should use CSS3, JavaScript and Perl only

 I wrote a Perl script for this which will do whatever is pointed out in the problem. Here is the fully working code and you can check the live demo here.
Here is the step by step explanation of the script.

  1. I wrote CSS code which will take care of background color, anchor tag , dir id and file class.
    • It makes background color black and any text font color as white.
    • Anchor tag’s color is kept light violet-blue mix colored.
    • Directory’s color is kept green under dir id


<br />
&lt;style type=&#39;text/css&#39;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; body {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; background-color: black;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color: #FFFFFF;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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&nbsp; .file {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; display:none;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #dir a{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color:#00FF00;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp; &lt;/style&gt;<br />
  1. Then I wrote one JavaScript code to make toggling things work out while clicking on directory. This script will take the id name and will check for the class name “file”. If it exists then while clicking, remove that class else add that class. By default it is made to display nothing using css.


<br />
&lt;script type=&#39;text/javascript&#39;&gt;<br />
&nbsp;&nbsp;&nbsp; function showFiles(dirname) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var el = document.getElementById(dirname);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; el.className = (el.className == &#39;file&#39;) ? &#39;&#39; : &#39;file&#39;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&lt;/script&gt;<br />


  1. Next I added javascript to make yahoo media work with our script.


<br />
&lt;script type=&#39;text/javascript&#39; src=&#39;;&gt;&lt;/script&gt;<br />


  1. Then finally wrote a Perl script to do the main work associated with all the above steps. Our Perl code should do few things:
    • Open the main song directory to read files and directories under it
    • Browse through each directory and files under it and save it in a hash having directory name as key and files name as value
    • While displaying, display the keys of hash i.e. directory names using div tag
    • And display the values associated with eack key when user clicks on directory list
    • It should show the count (no of songs under each directory) adjacent to directory name

To browse through the given directory and store sub directory names as key and its files as values in a hash.


<br />
opendir(DIR, $dirtoget) || die(&quot;Cannot open directory&quot;);<br />
for(readdir DIR) {<br />
&nbsp;&nbsp;&nbsp; next if /^\.{1,2}$/;<br />
&nbsp;&nbsp;&nbsp; my $path = &quot;$dirtoget/$_&quot;;<br />
&nbsp;&nbsp;&nbsp; if (-f $path){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (exists $songs_list-&gt;{$dirtoget}) { push @{ $songs_list-&gt;{$dirtoget} }, $_;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else { $songs_list-&gt;{$dirtoget} = [&quot;$_&quot;]; }<br />
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp; if (-d $path){<br />
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;printFile($path);<br />
&nbsp;&nbsp;&nbsp; }<br />
}<br />
closedir(DIR);&nbsp;<br />

Now, to display the directory with songs count, we need to write below code snippets. It will even fetch the files related to directories at the same time. If you wish to avoid it and wish to fetch the songs detail only after clicking on specific directory, then you need to use AJAX call here. I leave it up to you to modify and make it working according to your requirements.

<br />
foreach my $dir (keys %{$songs_list}){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $dir =~ m /(.*)?\/(.*)$/;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $count++;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;&lt;div id=&#39;dir&#39;&gt;&lt;a href=\&quot;javascript:showFiles($count);\&quot;&gt;$2 ( &quot;.scalar @{$songs_list-&gt;{$dir}}.&quot; )&lt;/a&gt;&lt;/div&gt;\n&quot;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;&lt;div id=&#39;$count&#39; class=&#39;file&#39;&gt;&quot;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getFiles($dir);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;&lt;/div&gt;\n&quot;;<br />
}<br />

And code subroutine getFiles:

<br />
sub getFiles{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my $dir = shift;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my @song_lists = @{$songs_list-&gt;{$dir}};<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (@song_lists){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print &quot;&lt;a href =\&quot;$dir/$_\&quot;&gt;$_&lt;/a&gt;&lt;br /&gt;\n&quot;; &nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
}<br />

Now, you can assemble all pieces of code and can run as a Perl script to make it happening. Here is the fully working code for it. If you have any ideas or suggestions to ponder, please post as comment.
Note: I used CSS , JavaScript codes altogether in the same script with Perl. If you wish you can write separate file for CSS and JavaScript and even use jQuery to do all these things but this script is not dependent on anything and it is at the simplest way to make it working.

Sanjeev Jaiswal

He loves web programming and security and co-founder of Alien Coders. He usually shares and helps engineering students and IT professionals in academics and jobs. An avid reader and quick learner.

Share your comment

%d bloggers like this: