Gossamer Forum
Home : General : Perl Programming :

Why won't this work?

Quote Reply
Why won't this work?
Argh...I've spent literally an hour trying to get this little bit of code working! Can anyone see why this code won't remove a line of code in folder.pl.

Code:
sub delete_directory {

open(FOLDER_LIST,"folder.pl") || &error("Unable to open folder.pl. Reason: $!");
@list = <FOLDER_LIST>;
close(FOLDER_LIST);


foreach $line (@list) {

if ($line ne "$folder\:\:$title") { $put_back .= $line; }

}

print $put_back;

open(FOLDER_BACK,">folder.pl") || &error("Unable to open folder.pl. Reason: $!");
print FOLDER_BACK $put_back;
close(FOLDER_BACK);

$message = "Directory '$title', deleted successfully...";

&show_page($message);

}

The format of the folder.pl file is;

/folder_path/something::title of area
/folder_path/something2::title2 of area
/folder_path/something3::title3 of area

I would imagine its something really obviouse I'm missing...but I can't put my finger on it!

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Why won't this work? In reply to
Well you are doing it a bit of an icky way but I'm guessing it won't work seeing as you never chomp the lines.
Quote Reply
Re: [Paul] Why won't this work? In reply to
Errr...where would I be without you Paul? Wink

Worked great...now I can see why people don't like switching between languages Tongue

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Why won't this work? In reply to
>>
Errr...where would I be without you Paul?
<<

This looks like a job for me, so everybody, just follow me, if you need a little controversy, it'd be so empty without me :)
Quote Reply
Re: [Paul] Why won't this work? In reply to
LMAO! Wink
Quote Reply
Re: [Andy] Why won't this work? In reply to
use File::Path;

;-)

- wil
Quote Reply
Re: [Wil] Why won't this work? In reply to
LOL....you and your modules Wink
Quote Reply
Re: [Andy] Why won't this work? In reply to
Well, my point is, if you would have used a module in the first place, you wouldn't of have to worry about chompin any new lines or whatever the problem was. It would of taken care of it for you.

Cheers

- wil
Quote Reply
Re: [Wil] Why won't this work? In reply to
Do you understand what a module is?

How do you think the module works?...it only does the same job as normal perl code except it is hidden in a module...lol...its not a magic wand.

Whether you write the code yourself or "use" a module you'll end up with the same result ;)

...and if you write the code yourself you don't compile lots of other code that you don't want/need.

Last edited by:

Paul: Jun 15, 2002, 10:13 AM
Quote Reply
Re: [Paul] Why won't this work? In reply to
But the module's author and the community have thought of all these little arreas where you might run into problems and programmed in workarounds for these pitfalls.

Jeesez, imagine if everyone wrote their own param parser to deal with CGI input? Imagine how insecure the web would be?????.

The whole point is that they have considered security issues, logical issues, potential problems etc. etc. so you don't have to (as much).

- wil
Quote Reply
Re: [Wil] Why won't this work? In reply to
>>
Jeesez, imagine if everyone wrote their own param parser to deal with CGI input? Imagine how insecure the web would be?????.
<<

Thats not what we/I are/am debating though...Im not saying everyone should go and write their own CGI.pm

The point I'm making is that to use a module for a simple task like making a directory or reading a file is just going to lead to a slow(er) script and in a way is poor programming.

I bet if you wanted you could use a module for most functions in your code but you'll end up waiting 15 seconds for it to compile.

Larry included functions like mkdir and chomp for people to use them, not for them to say "ah well, i'll use a 30kb module instead".

Last edited by:

Paul: Jun 15, 2002, 10:25 AM
Quote Reply
Re: [Paul] Why won't this work? In reply to
> The point I'm making is that to use a module for a simple task like making a directory or
> reading a file is just going to lead to a slow(er) script and in a way is poor programming.

I disagree. Allowing people to have access to your filesystem from a world wide web browser is putting great confidence in your ability as a programmer.

I wouldn't take that risk. I would use a module, as I know I have not thought of all the security precautions of doing this. And I know there are many pitfalls that I have not thought about - while the author of the module and the Perl community at large have thrashed out a zillion times before me.

- wil
Quote Reply
Re: [Wil] Why won't this work? In reply to
>>
Allowing people to have access to your filesystem from a world wide web browser is putting great confidence in your ability as a programmer.
<<


Guess I have confidence and you don't then.

Unless you are totally braindead (like Cliff or Matt) then opening/closing files isn't exaclty security risk of the century.

(Let us forget I forgot how to open a file using the correct precedence yesterday Smile)
Quote Reply
Re: [Paul] Why won't this work? In reply to
OK then. Pop quiz, hot shot :-)

Let's say we were opening files in a directory. The filename is passed to the script as user input. Very similar to what I've been arguing.

The programmer decided he is 'too good' for CPAN, and goes to write his own file opening, reading and closing sub.

The file is called with something similar to:

http://localhost/cgi-bin/insecure.cgi?file=insecure.cgi

That's great. But I'm feeling a bit naughty, so I request the script with the following parameter instead:

http://localhost/cgi-bin/insecure.cgi?file=insecure.cgi%00loser!

I'm using warnings, script and my script is taint-checked too. So what can we expect to happen here?

This is just one example of an unbelievably common pitfall, and once again, a reason why you should use a module - which has thought of all these scenarios before.

- wil

Last edited by:

Wil: Jun 17, 2002, 2:57 AM
Quote Reply
Re: [Wil] Why won't this work? In reply to
Mmm..got another problem Wink Well, its not such a problem...more just an annoyance! If a folder does not exist, then I want this to be shown from the Telnet command. The same as if an error was to come up for their not being a valid database for the MySQL database backup. Do you understand what I mean? The problem is, that with the code I have at the moment, it won't print out whatever is returned from the server! This is really annoying me, as if I use a simple test script to show the output of `ps -aux`, then that works fine. However, the following code will not Frown

Code:
# first of all do the
open(FOLDER_LIST,"folder.pl") || &error("Unable to open folder.pl. Reason: $!");
@folders = <FOLDER_LIST>;
close(FOLDER_LIST);

foreach (@folders) {

chomp;
@divided = split /\:\:/, $_;
$folder_path = $divided[0];
$folder_name = $divided[1];

$results_database .= "Results for folder '$folder_name'<BR>";

$query = `tar -cf $folder_name.tar $folder_path`;
$query .= `gzip -c $folder_name.tar > $folder_path$folder_name.tar.gz`;
print $query;
$results_database .= "$query <BR>";
}

Anyone got any bright ideas as to why this isn't working?

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Wil] Why won't this work? In reply to
LOL...don't suppose there is a module that will let me run system calls, and get the results properly is there? Wink I had a look at PerlDoc.com, but couldn't find one.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Why won't this work? In reply to
What's the 'system' command you're trying to do?

- wil
Quote Reply
Re: [Wil] Why won't this work? In reply to
$query = `tar -cf $folder_name.tar $folder_path`;
$query .= `gzip -c $folder_name.tar > $folder_path$folder_name.tar.gz`;

and

$query = `mysqldump -u$username -p$password $database_name > $save_to.sql.dump`;

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Why won't this work? In reply to
Archive::Tar would be my suggestion here.

For the MySQL Backups, look into Replication in MySQL if you've got the resources.

http://www.mysql.com/...R/e/Replication.html

Cheers

- wil

Last edited by:

Wil: Jun 17, 2002, 7:39 AM
Quote Reply
Re: [Andy] Why won't this work? In reply to
File descriptors 0,1,2 = stdin, stdout, stderr

To make stderr go to stdout and stdout to go to stderr you could use:

COMMAND 3>&2 2>&1 1>&3

There may be no output in the first place though.

Last edited by:

Paul: Jun 17, 2002, 7:45 AM
Quote Reply
Re: [Paul] Why won't this work? In reply to
In Reply To:
File descriptors 0,1,2 = stdin, stdout, stderr

To make stderr go to stdout and stdout to go to stderr you could use:

COMMAND 3>&2 2>&1 1>&3

There may be no output in the first place though.

I'm lost Unsure

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Why won't this work? In reply to
What I mean is that well firstly there may be no output to show in the first place, and if there is you may need to direct it to STDOUT (or STDERR if you want)...to do that you have to use something like 2>&1 after your command, the same way you may sometimes do with cron.