Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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:

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

Comment on error handling using symlink
Download Code
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
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? | Other CB clients
Other Users?
Others pondering the Monastery: (19)
As of 2014-08-27 15:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (242 votes), past polls