Solved

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

Posted on 2006-07-04
2
221 Views
Last Modified: 2012-05-05
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
Comment
Question by:harry_bela
2 Comments
 
LVL 8

Accepted Solution

by:
Perl_Diver earned 250 total points
ID: 17038786
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
 

Author Comment

by:harry_bela
ID: 17121796
Many thanks Perl_Diver that will point me in the right direction.

Cheers

Harry Bela
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl program to obtain a machine's memory usage 6 22
use perl to insert into MySQL database 9 135
perl split by | 2 97
Vb script to unzip a files and rename the files 5 90
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question