Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

open file error

by t-rex (Scribe)
on Jul 19, 2016 at 10:47 UTC ( [id://1168039]=perlquestion: print w/replies, xml ) Need Help??

t-rex has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am extracting a file name from a string and trying to open this file but i get error no such file or directory.

my $run_cmd = "./tpsm -rules tpsm_rulesfile_ubuntu -looprf 0 -stop 10 + -pspc 1 >logfile.txt "; # I extract logfile.txt from this string my ( $logfile ) = ( split />/,$run_cmd )[-1]; chomp($logfile); my $pagal = "$rundir_path/$logfile"; # this doesn't work-- case1 my $pagal = "$rundir_path/logfile.txt"; # this works fine -- case 2 open ( my $fh,"<",$pagal ) or die "No such logfile found\n";

case 1 it doesn't work and case-2 it works , i tried printing the two cases they were exactly the same. Please help

Replies are listed 'Best First'.
Re: open file error
by Corion (Patriarch) on Jul 19, 2016 at 10:49 UTC

    Note that when you use split, your variable will have a space at the end. You can find that by printing the values:

    print "[$pagal]\n";

    In the case that does not work, you will find that there is the additional space from $run_cmd.

    Personally, I would restructure the code so that the log file is in a separate variable from the start:

    my $logfile = '/tmp/some/logfile.txt'; my $run_cmd = "... >$logfile"; ... open my $log, '<', $logfile or die "Couldn't open log file '$logfile': $!";

      I used chomp on $logfile to get rid of any space. Actually I think you didn't get my problem, I am extracting the file name from the $run_cmd string and using it further along with a $rundir_path variable. I am unable to figure our why after using chomp , why this is happening.

        Feel free to use different approaches.

        As a hint, maybe you want to re-read chomp - it will not remove space (chr(32)) from the end of a string. But as I already showed you how to diagnose the problem, you certainly have found out by using my advice that whitespace at the end of $logfile was not the case for the difference between the hardcoded filename working and the dynamic filename not working.

        Please show us again the code that works and the code that fails, together with the diagnostic that ascertains that there is no whitespace at the end of the filename you use in the dynamic case.

Re: open file error
by haukex (Archbishop) on Jul 19, 2016 at 11:17 UTC
Re: open file error
by jellisii2 (Hermit) on Jul 19, 2016 at 16:57 UTC
    I would also suggest use of File::Spec for building out path names. I don't know that it would have cleaned up your variable or not, but its a good habit to use it when assembling paths.

    That said, I may be one of those weirdos that has had to write code that works on multiple platforms and been bitten by this. :)

      File::Spec makes two assumptions:

      1. File system rules depend only on the operating system
      2. A set of file system rules choosen for an operating system is valid for all mounted file systems.

      Both assumptions are wrong at least for Linux (see "Filesystem Specific Mount Options" in mount(8)), probably all modern Unix derivates, and so File::Spec can return wrong results.

      See also Re^5: Unify windows filenames.

      Alexander

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

      It has been a long time since I've had to write a Perl program that had to support an OS other than Linux/Unix/POSIX, MS Windows and Mac OSX.

      Internally, MS Windows accepts / as a path separater. It is cmd.exe that has a problem because it treats / as the option introducer. So, c:/path/to/file or even c:\path\to/file are valid.

      Of course, Linux/etc and Mac OSX don't use volume designaters ("drive letters"), so would try to treat "c:" as a directory in the current directory, but, so far, in the Perl programs I've written, the only source of volume designaters is from user input (either directly or through a file dialog box).

      In the rare case I have to start cmd.exe from Perl, I have used File::Spec::canonpath to insure the file path is acceptable.

      Otherwise, I usually treat file paths as POSIX style and have no problems.

      For example, to split a path, I just use

      @dirs = split qr{[\\/]}, $path; $file = pop @dirs unless -d $path;

      And to assemble a path

      $path = '/' . join '/', @dirs, ($file // '');

      Mostly, I don't care about the volume. If I need it, it's in $dirs[0].

Re: open file error
by RonW (Parson) on Jul 19, 2016 at 21:18 UTC

    (edited to fix unclear statement)

    Update 2: somehow, I never noticed splitdir

    As jellisii2 said, File::Spec can be helpful.

    In this case, File::Spec::splitpath would still have given "logfile.txt " because it can't know the extra space is not part of the file name.

    Otherwise, File::Spec::splitpath is a good way to break down paths.

    The potential negative to File::Spec::splitpath is that it only splits a path into volume, directory-path and filename. If you need to further split the directory path, you have to use a loop:

    # this is NOT tested my ($vol, $dir, $file) = File::Spec::splitpath($path); my $temp_path = $path; my $temp_dir; my $temp_vol; my @dirs; while ($dir neq $temp_path) { $temp_path = $dir; ($temp_vol, $dir, $temp_dir) = File::Spec::splitpath($temp_path); unshift @dirs, $temp_dir; } unshift(@dirs, $dir) unless (@dirs > 0);
      It's File::Spec->splitpath, and you're forgetting File::Spec->splitdir.
Re: open file error
by RonW (Parson) on Jul 19, 2016 at 21:26 UTC

    Further explaining the problem Corion pointed out.

    In your code, the string you assign to $run_cmd

    "./tpsm -rules tpsm_rulesfile_ubuntu -looprf 0 -stop 10 -pspc 1 >logfile.txt "

    has a trailing space.

    That trailing space is the source of the extra space in $logfile when you do the split.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found