Welcome to WebmasterWorld Guest from 54.163.100.58

Forum Moderators: coopster & jatar k & phranque

The unlink function

problem

   
3:32 pm on Jul 18, 2008 (gmt 0)

5+ Year Member



Im new to Perl and i came across the unlink fuction which is suppose to delete files. The problem Im having is when i add unlink to my scropt along with onther command it dosn't work.

Example:

if i have

unlink "file";
unlink "file.txt";

it will delete the file(s) like its suppose to.

However if i have another command along with unlink (example print)

print "hi";
unlink "file";
unlink "file.txt";

It won't delte the file(s)

When you reply can u please make it easy to understand.

6:21 pm on Jul 18, 2008 (gmt 0)

5+ Year Member



try:

print "Hi\n";

You need a newline at the end of a print command when running a script in the terminal otherwise the program sits and waits for user input.

8:16 pm on Jul 18, 2008 (gmt 0)

5+ Year Member



it didn't work but its ok because i found another way of doing it and it worked and its set up like this

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

10:08 pm on Jul 18, 2008 (gmt 0)

5+ Year Member



Your code is wrong. Are you sure you are using perl? Any function that returns 0 (zero) means the function failed, if it returns 1 (one) it succeeded.
10:23 pm on Jul 18, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], medranoenrique!

perhaps that "unlink($file $)" actually failed because it looks like a list with a blank item in it?

10:25 pm on Jul 18, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



first: you don't need a \n at the end of a print ... it just won't print a linebreak if you don't put one there (and you might get buffered output which won't be printed right away)

in unlink's case, it returns the number of files deleted, so 0 means that no files were deleted. that "solution" you posted really looks weird, especially the unlink($file $)-part, which should result in a syntax error.

2:08 pm on Jul 21, 2008 (gmt 0)

5+ Year Member



yeah perl diver your right the newer print function didn't work i still had the same problem

and to janharders your saying that the number should not equal 0

2:12 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



yes, that's what i'm saying ;)

are you sure the files are not deleted when you just add the print-statement?

can you simplify it, put just the minimum into a script, no other stuff, try it and if it does not work, post that?

did you create those files yourself or did a script create them?
maybe the script you're running (is it a cgi-script?) is not allowed to delete those files.

2:37 pm on Jul 21, 2008 (gmt 0)

5+ Year Member



my boss made them and its not a cgi script and what im trying to do is delete the fies that were created.

is there a way to tell a sript to run a sript?

2:47 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



yes, a script can run a command, use system() in perl. but it's priviledges won't change, therefore it probably can't delete those files either. can you delete the files via ftp?

put
print $> . "\n" . $< . "\n";
into the script to get the userids ($< = real user id, $> = effective user id) your script is run with. than you can figure out if that user is allowed to delete those files.

3:32 pm on Jul 21, 2008 (gmt 0)

5+ Year Member



i found out what i needed to do to make the unlink function work i needed to re-open and close a file handle that i had created and then i could unlink it. I'm not sure y but it worked.
4:05 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



was it the file-handle to one of the files you wanted to delete?
6:01 pm on Jul 21, 2008 (gmt 0)

5+ Year Member



yes
9:28 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



print out the value of $! immediately after the failed function to find the error number.
2:51 pm on Jul 22, 2008 (gmt 0)

5+ Year Member



ok it worked
3:17 pm on Jul 22, 2008 (gmt 0)

5+ Year Member



in unlink's case, it returns the number of files deleted, so 0 means that no files were deleted. that "solution" you posted really looks weird, especially the unlink($file $)-part, which should result in a syntax error.

Yes, in this case unlink is a list operator and the return value is the number of times the unlink function was successful, not simply 1 or 0 to indicate success or failure like other functions that don't expect a list.

4:56 pm on Jul 22, 2008 (gmt 0)

5+ Year Member



well when i tried it i tried it differntly

i did it by my original way of unlinking a file like this
print "hi";
unlink "file";
unlink "file.txt";

but what i did was this
print "hi";
then i would open and close "file"
then when i unlinked it, it worked

7:37 am on Jul 23, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



try this:
print "hi";
unlink "file" or print "unlink file error:$!";
unlink "file.txt" or print "unlink file.txt error:$!";
7:48 pm on Jul 25, 2008 (gmt 0)

5+ Year Member



thanks alot everyone i now have my problem solved so what happends with this fourm dose it get deleted
7:55 pm on Jul 25, 2008 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



it stays around "forever" to help the next person with the same problem as yours...
3:09 pm on Jul 28, 2008 (gmt 0)

5+ Year Member



ok thanks for telling me
 

Featured Threads

Hot Threads This Week

Hot Threads This Month