[ create a new paste ] login | about

Link: http://codepad.org/BFpIwVtz    [ raw code | output | fork ]

Perl, pasted on Jan 25:
use strict;
use warnings;
use File::Find;

my $URL_First = "http://examplesite.net/some/path/";
my $URL_Last = "/example/example.xml";

my %files;

find(\&wanted, '.');

# This loop is for demonstration only
for my $file (keys %files) {
	my $url = $URL_First . $files{$file};
	my $file2 = $URL_Last;
	# Since we use a lookahead, we don't need to put back $URL_Last
	$file2 =~ s/(?=\Q$URL_Last\E)/$url/g;
	#           ^^^--- lookahead assertion
	print "File: $file => $URL_Last -> $file2\n";
}

exit;  # Comment out this line to execute changes
{ 
	@ARGV = keys %files;
	local $^I = ".org";  # extension of backup file
	while (<>) {
		my $url = $URL_First . $files{$ARGV};
		s/(?=\Q$URL_Last\E)/$url/g;
		print;
	}
}

sub wanted {
# Find files that contain the keyword
# Save filename and its directory name in a hash
# Note that directory is relative, so if '.' is used as argument to find()
# the directory name will start with "./"
	return unless -f;
	open my $fh, '<', $_ or die "$File::Find::name : $!";
	while (<$fh>) {
		if (/\Q$URL_Last/) {
			$files{$File::Find::name} = $File::Find::dir;
			last;
		}
	}
}


Output:
1
Disallowed system call: SYS_chdir


Create a new paste based on this one


Comments: