Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

system and -e question

by dicty (Sexton)
on Dec 29, 2012 at 00:17 UTC ( #1010768=perlquestion: print w/ replies, xml ) Need Help??
dicty has asked for the wisdom of the Perl Monks concerning the following question:

Question1: I use system call to create a soft link in my Perl code below:

system("ln -s $file1 $file2");

$file2 is not created, why? The same system call works for some other files within the same perl script, Why?

Question2, For $file2 above, when I use code below for testing,

"if (-e $file2) { print "testing output"; }"

"testing output" is printed, looks like $file2 does exit, but when I cannot file $file2 with "ls", and the following perl code cannot find $file2 either.

Is there any explanation for that?

Thanks a lot!

Dicty

Comment on system and -e question
Re: system and -e question
by toolic (Chancellor) on Dec 29, 2012 at 00:40 UTC
Re: system and -e question
by syphilis (Canon) on Dec 29, 2012 at 04:36 UTC
    Is there any explanation for that?

    One explanation is that $file2 exists, but it wasn't created where you expected and you're looking in the wrong place for it.

    You can use File::Spec to determine the absolute path of $file2 - untested:
    use File::Spec; use warnings; use strict; my $file2 = 'whatever it is'; my $abs_path = File::Spec->rel2abs($file2); print $abs_path, "\n";
    Cheers,
    Rob

      I just used File::Spec to print out the absolute path, but I cannot find the path with "ls" command. Below is the error message

      ls: /home/dicty/test_programs/test/30min_run/30min_run.scoredata.XML: No such file or directory

Re: system and -e question
by 7stud (Deacon) on Dec 29, 2012 at 04:58 UTC

    Question2, For $file2 above, when I use code below for testing,

    if (-e $file2) { print "testing output"; }

    "testing output" is printed

    That sort of belies your claim that $file2 doesn't exist. From the information you gave, there is no way to tell which directory the soft link was created in and which directory you did the ls in. If the file name of the soft link is "/a/b/my_link", and you ran your program in the directory /programming/perl/ and you did the ls in the same directory as the one in which you ran your program, then ls won't list the link. But when you do the test -e "/a/b/my_link", it will return true.

    You might want to read the details of man ln to see if you can spot what happened. Or you might simply want to try an ls -al.

Re: system and -e question
by 2teez (Priest) on Dec 29, 2012 at 05:23 UTC

    Hi dicty,

    "Question1: I use system call to create a soft link in my Perl code"

    Just a suggestion....
    If your system support symbolic links why not use symlink function instead.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      2teez++. Using symlink avoids messing with the unknown system default shell:

      Passing unquoted variables to a single-string system really begs for trouble, as soon as variables contain meta characters. Quoting rules differ with the shell used (and become a real nightmare on Windows), and there is no safe way to find which rules work as intended.

      Using symlink also avoids the overhead of creating a new, complex process for just a single kernel call, and gives a clear error code in $! instead of returning some obscure exit code and some random text on STDERR of the child process.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010768]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-07-31 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (248 votes), past polls