Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Unable to resolve links...

by czech (Sexton)
on Nov 05, 2010 at 01:02 UTC ( [id://869607]=perlquestion: print w/replies, xml ) Need Help??

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

I am having trouble resolving links. When I hardcode the filename and path in the code, any symlinks get resolved. But when I read them from a file, the links are ignored. I have tried various functions to resolve the links. I show two here. Both succeed with the hardcoded path and fail with the path read from the file. My code:
use strict; use Cwd 'abs_path'; use File::Spec::Link; use vars qw ($LIST_SEPARATOR); my $path = "/home/czechar/foo/bar/myfile.xml"; print STDOUT "Original path: $path\n"; my $new_path = File::Spec::Link->resolve_all($path); print STDOUT "Resolved path(1): $new_path\n"; my $abs_path = abs_path($path); print STDOUT "Resolved path(2): $abs_path\n"; # # now try to read from a file... # open (FILE_LIST, "< ./filenames"); my $path = <FILE_LIST>; chomp $path; print STDOUT "path from file: $path\n"; $abs_path = abs_path($path); print STDOUT "abs path: $abs_path\n"; $new_path = File::Spec::Link->resolve_all($path); print STDOUT "new path: $new_path\n"; close (FILE_LIST); exit;
Thanks in advance for your help. Corwin

Replies are listed 'Best First'.
Re: Unable to resolve links...
by ELISHEVA (Prior) on Nov 05, 2010 at 08:34 UTC

    First,welcome to Perl Monks!

    Second, you are much more likely to get enthusiastic help if you surround your code with code tags (<code>....</code>). That way you can preserve all of the indenting in your original code.

    use strict; use Cwd 'abs_path'; use File::Spec::Link; use vars qw ($LIST_SEPARATOR); my $path = "/home/czechar/foo/bar/myfile.xml"; print STDOUT "Original path: $path\n"; my $new_path = File::Spec::Link->resolve_all($path); print STDOUT "Resolved path(1): $new_path\n"; my $abs_path = abs_path($path); print STDOUT "Resolved path(2): $abs_path\n"; # # now try to read from a file... # open (FILE_LIST, "< ./filenames"); my $path = <FILE_LIST>; chomp $path; print STDOUT "path from file: $path\n"; $abs_path = abs_path($path); print STDOUT "abs path: $abs_path\n"; $new_path = File::Spec::Link->resolve_all($path); print STDOUT "new path: $new_path\n"; close (FILE_LIST); exit;
    Now isn't that MUCH easier to read? :-) Here's how to do it:
    My code: <code> [ cut and paste your code here ] </code>

    As for the bug and tracking down its causes:

    • always surround variable names with delimiters when you print out variables to check their value. Thus print STDERR "path from file: <$path>\n";.

      A lot of subtle bugs can develop from trailing or leading white space. Unexpected trailing whitespace could certainly be a reason that a final path segment didn't resolve properly. White space is significant in *nix and windows systems so to the operating system, "foo " is a different file from "foo".

    • when you are printing debugging output, it is best to use print STDERR rather than print STDOUT. This makes all of your debugging code easier to find when you are ready to comment it out.

    Note: Please don't rely on my formatting above in lieu of fixing your original post. People may not take the time to look at the replies if they see your original post has no formatting. To edit and save changes, click on your original post, scroll down below to see the text box (make changes there), scroll down below the text box to see the update button (click to save). Thanks!

    Best, beth

      Hi Beth - Thanks for the pointer about <code> tags. You were right about the whitespace. I had a single whitespace character - a space - that screwed up abs_path and resolve_all, resolve_path, etc. And thanks for the tip about printing debug output to STDERR instead of STDOUT. You rock!
Re: Unable to resolve links...
by ikegami (Patriarch) on Nov 05, 2010 at 01:38 UTC

    But when I read them from a file, the links are ignored.

    So you're saying that abs_path and resolve_all someone determine the string came from a file and behave different based on that? That's preposterous.

    Look elsewhere. Maybe the name is different. Maybe the code is different.

Re: Unable to resolve links...
by aquarium (Curate) on Nov 05, 2010 at 02:04 UTC
    is your file list (in the file) the bare and unadorned filename?..as surely there's a difference when filename supplied by hand and that which comes in read from the file. when you manually hardcode a filename, is it single quoted or such, so that perl doesn't do any (helpful) evaluation on your behalf?
    the hardest line to type correctly is: stty erase ^H
      Changing the double quotes to single quotes in the hardcoded string has no effect. The hardcoded string still succeeds. The read from the file still fails. Is there a way to expose control characters? Perhaps I can use od on the file. Thanks for your reply. Corwin
        on unix/linux/cygwin you can "cat -vet filename" to see special characters. control characters will show up as carret ^ followed by a capital letter.
        the hardest line to type correctly is: stty erase ^H
Re: Unable to resolve links...
by apl (Monsignor) on Nov 05, 2010 at 11:53 UTC
    Try chomp on what you read from the file.
      sorry my formatting was so poor. If you look closely at the reformatted post, you will find chomp is there. Thanks for the reply. Corwin
        In the end, it turned out there was a whitespace character at the end of the string. I guess the moral of this story is - you can never be too careful about testing your input, and never assume you know what's in your data. Thanks for all your help everyone. Corwin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-03-19 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found