Rather than try to juggle files and paths yourself, you could employ the File::Basename and File::Spec core modules to do the heavy lifting. They have the advantage of being portable and well-tested. I have written a script which I think is doing what you are after but I use those modules to manipulate the paths and I use regular expressions with captures (see perlretut, perlre and perlreref) rather than your split.
use strict; use warnings; use Cwd qw{ abs_path }; use File::Basename; use File::Spec; my $scriptPath = abs_path( $0 ); my $path = ( fileparse( $scriptPath ) )[ 1 ]; my $configFileName = File::Spec->catfile( $path, q{spw750353.ini} ); open my $configFH, q{<}, $configFileName or die qq{open: < $configFileName: $!\n}; my $fileFromConfig = q{}; my $pathFromConfig = q{}; while( <$configFH> ) { if( m{^myfile=(.+)} ) { $fileFromConfig = $1; } elsif( m{^path=(.+)} ) { $pathFromConfig = $1; } else { warn qq{$_ : line not recognised\n}; } } close $configFH or die qq{close: < $configFileName: $!\n}; my $fullPath = File::Spec->catfile( $pathFromConfig, $fileFromConfig ); print qq{$fullPath\n};
Here is the .ini file, shown both from the Cygwin (*nix-like) environment and from the command prompt under Windows XP.
$ cat spw750353.ini myfile=bbbbbbb.txt path=aaaaaaaaaaaaaaa $
C:\cygwin\home\johngg\perl\Monks>type spw750353.ini myfile=bbbbbbb.txt path=aaaaaaaaaaaaaaa C:\cygwin\home\johngg\perl\Monks>
Here is the script running unaltered in both environments.
$ ./spw750353 aaaaaaaaaaaaaaa/bbbbbbb.txt $
C:\cygwin\home\johngg\perl\Monks>perl spw750353 aaaaaaaaaaaaaaa\bbbbbbb.txt C:\cygwin\home\johngg\perl\Monks>
Note how the modules use the path separator appropriate to the environment without programmer intervention.
I hope this is helpful.
Cheers,
JohnGG
In reply to Re: Weird string
by johngg
in thread Weird string
by AoZus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |