homepage Welcome to WebmasterWorld Guest from 54.242.140.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
The unlink function
problem
medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 3:32 pm on Jul 18, 2008 (gmt 0)

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.

 

perl_diver

5+ Year Member



 
Msg#: 3701727 posted 6:21 pm on Jul 18, 2008 (gmt 0)

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.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 8:16 pm on Jul 18, 2008 (gmt 0)

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.";
}

perl_diver

5+ Year Member



 
Msg#: 3701727 posted 10:08 pm on Jul 18, 2008 (gmt 0)

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.

phranque

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



 
Msg#: 3701727 posted 10:23 pm on Jul 18, 2008 (gmt 0)

welcome to WebmasterWorld [webmasterworld.com], medranoenrique!

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

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3701727 posted 10:25 pm on Jul 18, 2008 (gmt 0)

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.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 2:08 pm on Jul 21, 2008 (gmt 0)

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

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3701727 posted 2:12 pm on Jul 21, 2008 (gmt 0)

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.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 2:37 pm on Jul 21, 2008 (gmt 0)

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?

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3701727 posted 2:47 pm on Jul 21, 2008 (gmt 0)

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.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 3:32 pm on Jul 21, 2008 (gmt 0)

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.

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3701727 posted 4:05 pm on Jul 21, 2008 (gmt 0)

was it the file-handle to one of the files you wanted to delete?

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 6:01 pm on Jul 21, 2008 (gmt 0)

yes

phranque

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



 
Msg#: 3701727 posted 9:28 pm on Jul 21, 2008 (gmt 0)

print out the value of $! immediately after the failed function to find the error number.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 2:51 pm on Jul 22, 2008 (gmt 0)

ok it worked

perl_diver

5+ Year Member



 
Msg#: 3701727 posted 3:17 pm on Jul 22, 2008 (gmt 0)

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.

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 4:56 pm on Jul 22, 2008 (gmt 0)

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

phranque

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



 
Msg#: 3701727 posted 7:37 am on Jul 23, 2008 (gmt 0)

try this:
print "hi";
unlink "file" or print "unlink file error:$!";
unlink "file.txt" or print "unlink file.txt error:$!";

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 7:48 pm on Jul 25, 2008 (gmt 0)

thanks alot everyone i now have my problem solved so what happends with this fourm dose it get deleted

phranque

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



 
Msg#: 3701727 posted 7:55 pm on Jul 25, 2008 (gmt 0)

it stays around "forever" to help the next person with the same problem as yours...

medranoenrique

5+ Year Member



 
Msg#: 3701727 posted 3:09 pm on Jul 28, 2008 (gmt 0)

ok thanks for telling me

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved