CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Issue in doing soft link inside perl script

 



cmplin
New User

Jan 8, 2016, 9:45 PM

Post #1 of 2 (860 views)
Issue in doing soft link inside perl script Can't Post

I have a file ppp_cs_v0.rdl in the dir. Trying to create a soft link for this file in perl. After running the script, this is the message
****************************************************
ln: failed to create symbolic link ./ppp_cs_v0.rdl: File exists
sh: line 1: test.rdl: command not found
***************************************************
----------------------------------------------------
@files = `ls -1 *.rdl`;


if ( $#files != 0 ) {
$old = "$files[0]";
}
print $old;
`ln -s $old test.rdl`;

-------------------------------------------------------
Can someone help me to correct it?
thx


(This post was edited by cmplin on Jan 8, 2016, 9:46 PM)


Laurent_R
Veteran / Moderator

Jan 9, 2016, 4:52 AM

Post #2 of 2 (855 views)
Re: [cmplin] Issue in doing soft link inside perl script [In reply to] Can't Post

Although the error message is not very clear, I would suspect that the problem is due to a carriage returns within your file name (at the end of the name).

Try this:


Code
@files = `ls -1 *.rdl`; 


if ( $#files != 0 ) {
$old = "$files[0]";
}
print $old;
chomp $old;
`ln -s $old test.rdl`;


Or, better, you may want to use a Perl built-in function to retrieve the list of file names rather than firing a shell command:


Code
@files = glob ("*.rdl"); 

if ( $#files != 0 ) {
$old = "$files[0]";
}
print $old;
`ln -s $old test.rdl`;


Also, besides that immediate error, this line:

Code
if ( $#files != 0 ) {

is probably wrong: if you retrieve only one file name, the index of the last element will be 0, so that you will not create the link in that case. That's not what you want, I would guess. You most probably want something this:

Code
if ( @files != 0 ) {

which will work fine if you retrieve only one file name (@files will be 1 in scalar context in this case).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives