Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

error handling using symlink

by doubledecker (Scribe)
on Jun 18, 2012 at 07:07 UTC ( [id://976771]=perlquestion: print w/replies, xml ) Need Help??

doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm checking the return code of symlink using the following code. But, it is not reporting any error if source link is not existing. Is there any better way of handling this?

use strict; my ($link1, $link2); # source $link1 = '/var/tmp/sample.txt; #destination $link2 = '/tmp/sample.txt'; my $stat = symlink("$link1", "$link2"); if ( $stat ) { print "Link got created \n"; } else { print "Link not created \n"; }

Replies are listed 'Best First'.
Re: error handling using symlink
by cheekuperl (Monk) on Jun 18, 2012 at 07:19 UTC
      Perhaps I interpreted it(i.e. your question) wrong the first time.
      If the original file is oldfile and the link you are creating is newfile, the call would be:
      $stat=symlink("oldfile","newfile");
      This is known to us.
      newfile is created even if oldfile does not exist. It is not an error. Is that what you wanted to ask?
      You might verify if oldfile exists using -e test.
      if(-e $oldfile) { #create symlink } else { #Report $oldfile doesnt exist!
      }

        cheekuperl,

        you're right now. Even though old file doesn't exists, it is creating the symlink which I don't want it. Yes, we can use filetest operations before creating the symlink.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://976771]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-23 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found