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

Hi,

I'm just wondering if it is possible to have two different files to interact and produce a third file? For example I have a HTML and a perl file and I want those two to interact and create a third file, is that possible?

Another problem I have at the moment is using JavaScript within my perl program. I was hoping to change the window components using JavaScript within my perl but unfortunately its not working.

Here's my perl code hopefully someone out there can help me.

#!/usr/bin/perl #VLC.cgi use warnings; use strict; use CGI; my $dir = 'e:/development/mb10_bachelor_of_medicine_degree'; my $q = new CGI; my $query = lc $q->param( "code" ); my @Path; my @Status; my @list_of_files; my @list_of_subDirectory; my @list_of_Dir; my @list_of_txt; my @FileExt; my @FileName; my @Name; my @Author; my @Header; my @Ignore; my $FilePath; my $FileName; my $path_translated = $ENV{path_info}; my $path; my $referer = $ENV{http_referer}; my $NameCounter = 0; my $count = 0; my $Num = 0; my $Found = 0; my %Ext = ( pl => "Perl", cgi => "CGI", htm => "HTML Document", ppt => "PowerPoint", pdf => "ADOBE", doc => "Word Document", pdf => "Adobe Acrobat Document", asp => "Active Server Page", gif => "GIF Image", js => "JavaScript File", bak => "Back-up", "" => "Folder" ); print "Content-type:text/html\n\n"; print "<html><head><title> Directory Contents</title></head><body link +='#FFFFFF'vlink='#FFFFFF'alink='#FFFFFF'>"; print "<script language='Javascript'><!--var newWin;function fullscree +n(location){var screen1=window.open(location,'Screen','directories=no +,left=0,top=0,height=490,width=760,location=no,menubar=no,scrollbars= +auto,status=yes,toolbar=no');screen1.opener = top;}//--></script>"; search($dir); if($Num ne 0){ while($Num ne 0){ $Num--; $dir = @list_of_subDirectory[$Num]; search($query); } } if($Found eq 0){ print "Couldn't find <b>'$query'"; } FindReferer($path); DirContents($path); PrintItems($query); sub search{ opendir DIR, $dir or die "couldn't open the default directory: $!" +; @list_of_Dir = readdir(DIR); closedir DIR; foreach (@list_of_Dir){ #skip the files that has "." or ".." next if $_ eq "." or $_ eq ".."; FindSubDirectory(@list_of_Dir); #if query match the file name if(grep{/$query/} lc($_) and -d "$dir/$_"){ $Found = 1; $path = "$dir/$_"; } } } sub FindSubDirectory{ # if file is a folder if(-d "$dir/$_"){ #save path @list_of_subDirectory[$Num] = "$dir/$_"; #increment $Num++; } } sub DirContents{ my $Counter = 0; opendir DIR2, $path or die "couldn't open the default directory: $ +!"; @list_of_files = grep{!/.bak/} readdir(DIR2); closedir DIR2; FileExtention(@Ignore); @list_of_txt = grep{/.txt/} @list_of_files; foreach(@Ignore){ @list_of_files = grep{!/.@Ignore[$Counter]/} @list_of_files; $Counter++; } foreach (@list_of_files){ next if $_ eq "." or $_ eq ".."; FindPath(@list_of_files); SplitFileName(@list_of_files); $count++; } } sub FileExtention{ my $value; my $Counter = 0; foreach $value( $q->param( "ext" ) ) { @Ignore[$Counter] = $value ; $Counter++; } @Ignore[$Counter] = "txt" ; } sub SplitFileName{ my @SplitFile = split /\./, $_; @FileExt[$count] = lc($SplitFile[1]); @FileName[$count] = $SplitFile[0]; foreach(@list_of_txt){ my @txt = split /\./, $_; my $txt = @txt[0]; if (@FileName[$count] eq $txt){ $FilePath = $_; FindFile($FilePath); } } } sub FindFile{ my $Name; my $Author; my $lineNo = 1; my $file = "$path/$FilePath"; open FILE, $file or die "Couldn't open File: $!"; while (<FILE>){ if ($lineNo eq 1){ $Name = "$_"; } else { $Author = "$_"; } $lineNo++; } my @SplitName = split /\=/, $Name; @Name[$NameCounter] = @SplitName[1]; my @SplitAuthor = split /\=/, $Author; @Author[$NameCounter] = @SplitAuthor[1]; close FILE; $NameCounter++; } sub FindPath{ my $Extention; my $Counter = 0; if (-d "$path/$_"){ foreach(@Ignore){ $Extention = "ext=@Ignore[$Counter]&$Extention"; $Counter++; } @Status[$count] = "Explore Folder"; @Path[$count] = "http://mcucharlie$path_translated?code=$_&$Ex +tention"; } else{ @Path[$count] = "$path/$_"; @Status[$count] = "View File"; } } sub FindReferer{ my $LastNum = 0; #split the http referer my @referer =split /\//, $referer; #count the number of item in the array foreach(@referer){ $LastNum++; } #store the folder name my $Header = uc (@referer[$LastNum-2]); #break folder name every time we see "_" @Header = split /\_/, $Header; } sub PrintItems{ my $Counter = 0; my $Fields = 0; print "<b>Resources - Below is a selection of files which have bee +n compiled by your lecturers and which you may find helpful.</b></P>" +; foreach(@Header){ $Counter++; print "<b>@Header[$Counter] "; } print "<table width=100% border=0 cellspacing=0 cellpadding=0>"; print "<tr bgcolor=#4982AB>" ; print "<td width='40%'><b><font color=#000000>Name</font></b>< +/td>"; print "<td width='20%'><b><font color=#000000>Author</font></b +></td>"; print "<td width='20%'><b><font color=#000000>File Type</font> +</b></td>"; print "<td width='20%'><b><font color=#000000>Link</font></b>< +/td></tr>"; print "<TR><TD><p></p></TD><TD></TD><TD></TD></TR>\n"; my $foo = 0; while($foo ne $count){ if (@FileExt[$foo] eq "txt"){ $foo++; } if($Fields eq 0){ print "<tr bgcolor=#66ccff><td width='45%'><font color=#00 +0000 face=Arial>@Name[$foo]</font></td><td width='20%'><font color=#0 +00000 face=Arial>@Author[$foo]</font></td><td width='20%'><font color +=#000000 face=Arial>$Ext{@FileExt[$foo]}</font></td><td width='15%'>< +a href='JavaScript:fullscreen('@Path[$foo]')'><font color=#000000 fac +e=Arial>@Status[$foo]</font></a></td></tr>"; $Fields++; } else{ print "<tr bgcolor=#ccffff><td width='45%'><font color=#00 +0000 face=Arial>@Name[$foo]</font></td><td width='20%'><font color=#0 +00000 face=Arial>@Author[$foo]</font></td><td width='20%'><font color +=#000000 face=Arial>$Ext{@FileExt[$foo]}</font></td><td width='15%'>< +a href='JavaScript:fullscreen('@Path[$foo]')'><font color=#000000 fac +e=Arial>@Status[$foo]</font></a></td></tr>"; $Fields--; } $foo++; } }

edited: Tue Jul 2 13:27:41 2002 by jeffa - added code tags, formatting, and readmore tag

Replies are listed 'Best First'.
(jeffa) Re: File Included
by jeffa (Bishop) on Jul 02, 2002 at 15:24 UTC
    First question - yes, it is possible - but, you have many problems with this script:
    1. You are using CGI.pm, yet you are explicitly printing the content header - use $q->header instead.
    2. You are also using multiple print statements to generate HTML - use CGI.pm's methods or a templating tool such as HTML::Template instead.
    3. You are trying to access an array's element with the wrong syntax: @foo[$bar] should be $foo[$bar] fix the 40 or so occurrences in your code and you will get better results.
    4. You are using subroutines, but you are not passing variables to them, opting for globals where globals are not needed.
    I hate to say it, but you have so many errors that if i were you, i would start over. What are your requirements? Write these down and work from there. Design! :)

    Here is an example that prints the contents of the current directory and generates links to each of the files. It only scans one level deep, for more check out File::Find. Each link displays the contents of that file as text - care is taken to make sure that the user cannot view files outside of the directory. Be careful with this - it is a loaded gun! And always, always, use the -T (taint option) on CGI scripts. Also, mod_proxy might just be a better solution (thanks for the link grinder).

    #!/usr/bin/perl -Tw use strict; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); my $file; if ($file = param('file')) { # only allow files in this dir if ($file =~ /[\/~]/ or $file =~ /^\./) { print header; die "access to $file not allowed" } unless (open(FH,$file)) { print header; die "$file does not exist"; } print header(-type=>'text/plain'); print while <FH>; } else { print header,start_html('Contents of current dir'); print ul(li([ map { a({-href=>qq|@{[script_name]}?file=$_|},$_) } <*> ])); }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)