Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Perl substitute

by Don Coyote (Pilgrim)
on Apr 15, 2013 at 13:52 UTC ( #1028743=note: print w/replies, xml ) Need Help??

in reply to Perl substitute

This will do what you need, however, you will need to adapt the untainting regex to your particular requirements. I was a little uncertain about processing a regex with backticks, so I used taintmode.

#!usr/bin/perl -Tw use warnings; use strict; opendir my $dh, './testdir/'; #assume all files, no sub dirs my @filelist = readdir($dh); close($dh); my $rep = qr'<>'; while (@filelist){ my $basefile = shift @filelist; next unless $basefile =~ m/^((\w+\s*)+\.(pl|txt))$/; $basefile = $1; my $pathfile = './testdir/'.$basefile; open (my $rfh, '<', $pathfile); my @filelines; while (<$rfh>){ $_ =~ s/$rep/\`<>\`/g; push @filelines, $_; } close($rfh); open (my $wfh, '>', $pathfile); while (@filelines){ my $line = shift @filelines; print $wfh $line; } close($wfh); } exit 0;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028743]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-06-27 00:43 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (595 votes). Check out past polls.