in reply to What is the right way of concatenating strings

Sometimes, using our imense psychic powers, we can tell what the error message is without being told, but sometimes we get it wrong. You are best to just tell us. Copy and Paste is your friend. It is also good practise to include a sample script the reproduces the problem. Yours does not. I can't run your sample code and see the error. The following code does show warning and most likely it is the one that you see:

use strict; use warnings; my $input; my $temp = 'more stuff'; print $input.$temp."\n";

This shows the warning Use of uninitialized value in concatenation (.) or string.

None if this is pertinent to your question title however. What you are really asking is Is it better to interpolate variables into a string or concatenate them. There is no one answer and at the end of the day do what you can read best (Perl changes it all around internally anyway). However, you never need to do both so $input="$input"."$temp"; is bogus. That can better be written as $input = $input . $temp;, or even better as $input .= $temp;.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: What is the right way of concatenating strings
by cool (Scribe) on May 02, 2006 at 08:56 UTC
    mind blowing!! I didnt explain the problem in detail but you got it almost right hats off to you. I admit my mistake. Actully I failed to reproduced the error as I made some changes in my code and it started working. Here is my tiny code:
    #!/usr/bin/perl -w print"Pls enter the file name\n"; my $in=<STDIN>; open(AA,"$in"); my $input=''; while(<AA>) { my $temp=''; $temp=$_; $input=$input.$temp; } close(AA); open(BB,">out.txt"); print BB $input; close(BB);
    For my work I need read files and generate it in desired format, so I rely mostly on concatination as above. and error that is produced same as u stated. Pls tell me also what all mistakes I have done in this small piece of code.

      There are not really gaping errors, but there are a few things that could be cleaned up in various ways.

      You use -w in the shebang line which enables warnings, but you should add a use strict; line too.

      It is strongly recommended that you use a three parameter open, especially when you obtain the name of the file from the user (as in this case).

      You have a redundant assignment to $temp. The declaration for temp can be moved into the conditional expression for the while loop or omited all together. However that impinges somewhat on another decision - should you output in the input loop, or should you slurp (at present you are slurping).

      A better way to slurp is:

      my $input = do {local $/; <AA>;};

      That replaces your whole while loop by setting the input line seperator to null and reading everything into $input in one big slurp (you begin to see where the name comes from perhaps). That is ok for small files, but gets to be a problem if the file is large. For large files it is much better to process the file a line at a time like this:

      #!/usr/bin/perl -w use strict; print "Please enter the file name\n"; my $in = <STDIN>; open inFile, '<', $in; open outFile, '>', 'out.txt'; while (<inFile>) { my $line = $_; # ... do stuff to $line here print outFile $line; } close inFile; close outFile;

      That way only one line of data is in memory at a time so memory usage remains low. The down side is that if you need to manipulate multiple lines as a unit things can get messy. When you bump up against that situation come back and we will help again, or Super Search for 'multiple lines' and read through some of the past questions that are relevant.

      Update: s|%/|$/|g


      DWIM is Perl's answer to Gödel

        my $input = do { local $/; <AA> };
        uses twice as much memory as
        my $input; { local $/; $input = <AA>; }
        but the files I use are usually to small to cause me to care.

        Supporting evidence

        yummmi, I really like that slurping concept. Just one line code..perl is amazing, Thanx again man. Can you pl explain me what do mean by three parameter here->> " It is strongly recommended that you use a three parameter open, especially when you obtain the name of the file from the user (as in this case) " and also throgh some light on things (how this is working)inside the curlies on right hand side...please
        my $input = do {local %/; <AA>;};
        waiting...
        Just one thing, I forgot to add chomp in the 5th line of my code...
        #!/usr/bin/perl -w use strict; print "Please enter the file name\n"; my $in = <STDIN>; chomp($in); open inFile, '<', $in; open outFile, '>', 'out.txt'; while (<inFile>) { my $line = $_; # ... do stuff to $line here print outFile $line; } close inFile; close outFile;