Solved

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

Posted on 2006-07-04
2
217 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
Comment Utility
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
Comment Utility
Many thanks Perl_Diver that will point me in the right direction.

Cheers

Harry Bela
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

18 Experts available now in Live!

Get 1:1 Help Now