Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Perl Sub with ok / cancel buttons - should be really simple...

Hi All,

Ok i am really, really new to perl and could really do with some help on this.

I am writing a script which will have a number of subs all opened by links.

So for example:

sub test{

#Insert code here#
        }

Would be called by function:

                function test(){
                        $alert
                        var page="$script?action=test";
                        show(page, 110,370);
                }

And the link for this function would be:

<a href="javascript:parent.head.test()"></a> (The script is in frames)


So that how the script will operate.

I want this particular sub to find files in a directory and delete them depending on their name.

The script to do this is:

#!/usr/bin/perl
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

$word_to_delete = "pages";

print "content-type: text/html \n\n";      #The header
opendir (DIR,"/home/mysite.com/public_html/folder/");
@files = grep(/$word_to_delete/, readdir (DIR));
closedir (DIR);

foreach $file (@files)
{
      unlink $file;
      }
if (unlink($file) == 0) {
    print "File deleted successfully.";
} else {
    print "File was not deleted.";
}


Where i get stuck is when i execute the "delete script" on it's own, it will obviosuly instantly run and perform the operation.

What i want to do is insert this code into the "sub test" and add in an ok button.

So when you click the link

<a href="javascript:parent.head.test()"></a> the sub will open "show(page, 110,370);" and then you have the option to click ok and run the script.

So to sumarise,

I could do with someone posting the full sub test containing the "delete script" and adding in an OK button which once clicked will run the delete script.

Basically i am used to Javascript where you post the function in the header and then call it with a javascript link. So Perl is totally alien to me :-)


If anyone can post the full source for this sub i would be very grateful.

Regards

Harry.
0
harry_bela
Asked:
harry_bela
1 Solution
 
Perl_DiverCommented:
this is not an answer to your question, which I really am not sure of what the question is, but this part of the perl code you posted is not correct:

foreach $file (@files)
{
     unlink $file;
     }
if (unlink($file) == 0) {
    print "File deleted successfully.";
} else {
    print "File was not deleted.";
}
 

it should be:

foreach $file (@files) {
   if (unlink $file) {
      print "File deleted successfully.\n";
   }
   else {
      print "File was not deleted.\n";
   }
}

also, you must either be in the directory that the files are stored in or $file must contain the full machine path to the files for the unlink() function to work.
0
 
harry_belaAuthor Commented:
Many thanks Perl_Diver that will point me in the right direction.

Cheers

Harry Bela
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now