Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Weird string

by AoZus (Initiate)
on Mar 13, 2009 at 09:30 UTC ( #750353=perlquestion: print w/replies, xml ) Need Help??
AoZus has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am a beginner to perl. I am so sorry about the terrible formatting earlier. I am trying to read from a file, for some fields. The code is as follows: ********************************************************************************
use strict; use warnings; use FileHandle; use Cwd 'abs_path'; #====================== main() ======================== my $FH = new FileHandle; my $configFile = ""; my $file= ""; my $path = ""; my $fullpath = ""; my $buffer = ""; #get full path + configFile $configFile = abs_path($0); $configFile =~ s!/!\\!g; $char=chop($configFile); while($char ne '\\') {$char=chop($configFile);} $configFile=$configFile."\\config.ini"; $buffer = ""; #to read arguments from config.ini open(FH, "<$configFile") or die "Error: $!\n"; binmode FH, ':raw'; #copy configFile contents my @array; while (<FH>) { @array = (); $buffer = ""; $buffer = $_; if (length($buffer)>2) { chomp($buffer); @array = split(/=/, $buffer); if (lc($array[0]) eq lc("myfile")) { $file = $array[1]; } elsif (lc($array[0]) eq lc("path")) { $path = $array[1]; } } } $buffer = ""; close(FH); my $fullPath = $path . "\\" . $file; print STDOUT "$fullPath\n";
******************************************************************************** And here are the contents for the config.ini: <file> myfile=bbbbbbb.txt path=aaaaaaaaaaaaaaa </file> ******************************************************************************** Comments: The output I got is: bbb.txtaaaaaaaaaabbbbb But I am expecting: aaaaaaaaaaaaaaa\bbbbbbb.txt It seems that the string wraps around itself, but I don't understand why. When I did print $file and print $path individually, they are fine. Can someone please help me?

Replies are listed 'Best First'.
Re: Weird string
by moritz (Cardinal) on Mar 13, 2009 at 09:35 UTC
    Please read Writeup Formatting Tips and update your node. It's not readable right now.

    The code you pasted doesn't include the portion where $buffer_seed is initialized - maybe that's an error? You assign to $buffer but only read from $buffer_seed.

    Also please always use strict; use warnings; and declare your variables with my.

Re: Weird string
by ig (Vicar) on Mar 13, 2009 at 11:36 UTC

    Your configuration file is a text file. You should not be setting binmode on FH. Try it without binmode FH and you will probably get the output you are expecting.

Re: Weird string
by manoj_speed (Prior) on Mar 13, 2009 at 09:51 UTC
    Hi friend,

    Please put your code with proper alignment. In your code you missed the close parenthesis also ")".[ near the code, if (lc($array[0]) eq lc("myfile") ].

Re: Weird string
by johngg (Abbot) on Mar 13, 2009 at 10:06 UTC

    It would also be very useful if you gave us an example of the data in the file you read on the FH filehandle. Without that all we can do is guess at your intent.



Re: Weird string
by johngg (Abbot) on Mar 13, 2009 at 17:26 UTC

    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.



      Hi all... Its working now... Thank you! =)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://750353]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2018-07-16 07:24 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (333 votes). Check out past polls.