>>>>> "Raj" == Raj Karadakal <[EMAIL PROTECTED]> writes:

Raj> Hi,
Raj>    I am trying to modify some file sin place usinh the -i switch of
Raj> perl. In each of the files however I need to replace certain strings with
Raj> file name it self.

Raj> When I try the following command
Raj> perl -i.orig -pe 's/<NAME>/$0/' `ls -1`

Raj> Will replace '<NAME>' with '-e' in all the files

Raj> How can I get the file names in my script?

>From 'perldoc perlrun':

            From the shell, saying

                $ perl -p -i.orig -e "s/foo/bar/; ... "

            is the same as using the program:

                #!/usr/bin/perl -pi.orig
                s/foo/bar/;

            which is equivalent to

                #!/usr/bin/perl
                $extension = '.orig';
                LINE: while (<>) {
                    if ($ARGV ne $oldargv) {
                        if ($extension !~ /\*/) {
                            $backup = $ARGV . $extension;
                        }
                        else {
                            ($backup = $extension) =~ s/\*/$ARGV/g;
                        }
                        rename($ARGV, $backup);
                        open(ARGVOUT, ">$ARGV");
                        select(ARGVOUT);
                        $oldargv = $ARGV;
                    }
                    s/foo/bar/;
                }
                continue {
                    print;  # this prints to original filename
                }
                select(STDOUT);

            except that the -i form doesn't need to compare $ARGV
            to $oldargv to know when the filename has changed.
            It does, however, use ARGVOUT for the selected file-
            handle.  Note that STDOUT is restored as the default
            output filehandle after the loop.

Note the $ARGV there.  Yes, the filename is in $ARGV.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to