Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Not able to replace to directory paths

by GrandFather (Saint)
on Mar 10, 2011 at 21:24 UTC ( [id://892537]=note: print w/replies, xml ) Need Help??


in reply to Not able to replace to directory paths

When I run the code you supplied (using <DATA> and putting the input file contents in the __DATA__ section) it generates:

Name "main::OUTPUT" used only once: possible typo at ... . missing E:/qdepot_automation/addfiles.txt Unrecognized escape \q passed through in regex; marked by <-- HERE in +m/E:\q <-- HERE depot_automation/ at ... . missing E:/qdepot_automation/AMSS/products/build/textfiles.txt

which is different than the result you report. Your reported result is obtained by not using strictures! If you actually used strictures as you are pretending to you'd have been further on toward an answer to your problem: you are using \ instead of / in your match string. You should probably also meta quote the match string in case a . or other interesting character is part of the path you are matching. Consider:

use strict; use warnings; my $client_root = "E:/qdepot_automation"; my $perforce_root_dir = "//depot/code"; my $line; while ($line = <DATA>) { if ($line =~ /missing/) { $line =~ s/\\/\//g; $line =~ s/\Q$client_root\E/$perforce_root_dir/g; print "$line\n"; } } __DATA__ missing E:\qdepot_automation\addfiles.txt same E:\qdepot_automation\AMSS\products\build\ms\files.data same E:\qdepot_automation\AMSS\products\build\ms\build.cmd missing E:\qdepot_automation\AMSS\products\build\textfiles.txt same E:\qdepot_automation\AMSS\products\build\ms\lib.min

prints:

missing //depot/code/addfiles.txt missing //depot/code/AMSS/products/build/textfiles.txt
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Not able to replace to directory paths
by Anonymous Monk on Mar 10, 2011 at 22:26 UTC

    I have a interesting problem now,the below code dumps "$stdout" output of the $cmd command run to "p4diff.log",what I noticed is it's not entering the while loop if I do this where as if I use a pre-existing p4diff.log without generating through the program it enters the program,any idea what could be wrong in the below code

    $cmd = [ (qw(p4 diff -f -sl), $options{v}) ]; run3($cmd, \$stdin, \$stdout, \$stderr); #print "\n$stderr\n"; #Output the diff to p4diff.log open(my $P4DIFF, '+>', "p4diff.log") or die $!; print $P4DIFF $stdout; $input = <p4diff.log>; #Open teh p4diff.log to get the missing files and output to dele +tefiles.txt open(my $OUTPUT, '+>', "deletefiles.txt") or die $!; open my $DATA, '<', $input or die "could not open '$input' $!"; while ($line = <$DATA>) { print "IN WHILE LOOP\n";#not printing if ($line =~ /missing/) { $line =~s/\\/\//g; $line =~s/\Q$client_root\E/$perforce_root_dir/g; print "$line\n"; print $OUTPUT $line; } } close $OUTPUT;

      Maybe you should close $P4DIFF before you open the associated file?

      Depending on how much you've written, the IO buffer (size 4k) might not have been flushed yet, so the file behind $DATA could be empty.  In other words, you'd get EOF with the loop quitting right away...

      This doesn't print anything (i.e. while loop not entered):

      open(my $P4DIFF, '+>', "p4diff.log") or die $!; print $P4DIFF "foo\n" x 3; # close $P4DIFF; my $input = "p4diff.log"; open my $DATA, '<', $input or die "could not open '$input' $!"; while (my $line = <$DATA>) { print ">> $line"; }

      while if you uncomment the close, you'd get:

      >> foo >> foo >> foo

      What does $input contain? What do you expect it to contain? What does the file of the name in $input contain? Why are you not using strictures?

      True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found