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

Hi, This is my small portion of a bigger program. I need to concatenate 3 variables.The details are here.But output is r +esulting in 2 seperate lines rather than on a single line or in an another way I need to add \dir2 to the main directory path C: +\shyam\dir1 and final path should be "C:\shyam\dir1\dir2" when printed or accessed in program but i am get +ting as C:shyam\dir1 and \dir2 in 2 seperate lines.Because of this my + copy is failing. Please help
$\ = "\n" ; $, = "\t" ; #$|=1; use strict; use warnings; use POSIX qw[ _exit ]; use MIME::Lite; use Sort::External; use Net::Ping; use Win32::DriveInfo; $source="C:\shyam\dir1; $sym="\"; $file="dir2"; ## please note that this value is not directly assigned +but being extracted from another directory path using slicing.Ex: C:\ +sundar\dir2 $source_mod=join '',$source,$sym,$file; print "$source_mod"; Note: I have also used . opeartor but result is still the same.I am su +specting about $\="\n" in the beginning.But this is required as the o +ther part of my program needs this. Please help

Replies are listed 'Best First'.
Re: concatenation of variables resulting in 2 seperate lines
by Corion (Patriarch) on Jul 25, 2012 at 10:32 UTC

    What are these lines supposed to do:

    $source="C:\shyam\dir1; $sym="\";

    These do not look like they are syntactically correct. Please reduce your program to a short, runnable program that still reproduces the problem.

    Also note that backslashes in double quotes likely do not mean what you think they do. Double quotes interpret backslashes.

      those 2 are two variables needed for concatenation final my $source_mod should contain "C:\shyam\dir1\dir2" that is the $source is concatenated with two variables \ and dir2.I am + not sure how backslash can be used to concatenate as a variable

        There is at least one quote missing in this line:

        $source="C:\shyam\dir1;

        Again, please fix your program, or post the real program and not some vague interpretation.

Re: concatenation of variables resulting in 2 seperate lines
by BillKSmith (Monsignor) on Jul 25, 2012 at 13:21 UTC
    I am suspecting about $\="\n" in the beginning. But this is required as the other part of my program needs this.

    To prevent this problelm, perl special variables (such as $\) should be declared with local in the smallest possible block where they are needed. Refer perldoc -f local

Re: concatenation of variables resulting in 2 seperate lines
by tfredett (Sexton) on Jul 25, 2012 at 13:51 UTC

    Overall good question, although its not a huge deal, please do read How do I post a question effectively?. It will help us here, read your question a little easier, and thus answer it faster, and who doesn't love speed? :)

      Here is the detailed Problem statement and my approach: config.txt contains 2 section DIR and FILE. DIR has list of directory path and FILE section has file path I need to read the file line by line and check if each line is a file +or a directory. It is required to copy the entire directory content to source director +y. Similarly mentioned file needs to be copied to source directory I need to use robocopy to copy large files.The program is working for +copying file but not for copying directory. For this i thought to split the last part of directory path and attach + this to source path inorder to copy using robocopy. the syntax of robocpy command is robocopy $target_file $source_dir_mod ## here target_file=C:\shyam\di +r1 and i need to get source_dir_mod=C:\sundar\directory1\dir1 Then I can copy dir1 to the source directory [DIR] C:\shyam\dir1 [FILE] C:\shyam\output.txt $\ = "\n" ; $, = "\t" ; #$|=1; use strict; use warnings; use POSIX qw[ _exit ]; use MIME::Lite; use Sort::External; use Net::Ping; use Win32::DriveInfo; my $source_dir = `pwd`;#C:\sundar\directory my $source_dir_mod; my $file_config = "config.txt"; my $sym = '\\' ; open FH_config, "$file_config" or die "$!"; while (my $line = <FH_config>) { chomp $line; next if $line eq ""; if (($line eq "[DIR]") or ($line eq "[FILE]")) { #print "no action"; } else { my $target_file = $line; #print $target_file; if (-d $target_file) { print("it is directory"); (my $path,my $file) = $target_file =~ m|^(.*[/\\]) +([^/\\]+?)$|; $source_dir_mod = join '',$source_dir,$sym,$file; print ($source_dir_mod); `robocopy $target_file $source_dir_mod`; #print "$path"; ##C:\shyam #print "$file"; ##dir1 } elsif(-f $target_file) { print("is a file"); (my $path,my $file) = $target_file =~ m|^(.*[/\\]) +([^/\\]+?)$|; #print "$path"; ##C:\shyam #print "$file"; ##output.txt `robocopy \/Z $path $source_dir $file`; } else { print("no action"); } } } close FH_config;