Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

error handling using symlink

by doubledecker (Scribe)
on Jun 18, 2012 at 07:07 UTC ( #976771=perlquestion: print w/replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:


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:
      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!


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://976771]
Approved by rovf
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-12-18 21:07 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (498 votes). Check out past polls.