Solved

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

Posted on 2006-07-04
2
218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now