Why doesn't perl inplace work if I read user input before this?

I am trying to edit the cfg file inside inside a perl script, but it does not work if I read user input before that. Here is a test script to recreate the problem that I am seeing.

#!/usr/bin/perl -w

use strict;

my $TRACE_CFG = "trace.cfg";

print "Continue [y/N]> ";
my $continue = <>;

{
    local $^I = '.bak';
    local @ARGV = ($TRACE_CFG);

    while (<>) {
        s/search/replace/;
        print;
    }

    unlink("$TRACE_CFG.bak");
}

Editing works if I comment on "my $ continue = <>;" line. If I read the user input, it looks like the @ARGV setting for the trace.cfg file does not take effect, and the while loop is waiting for input from STDIN. I can get around this using sed or using a temporary file and renaming it, but I would like to know the reason for this behavior and the correct way to do it.

+4
source share
2 answers

Try

my $continue = <STDIN>;

instead

my $continue = <>;

<> , - , @ARGV, .

+5

perlop:

< > , , . , : < > , @ARGV , , $ARGV [0] "-", . @ARGV .

@ARGV < > , .

+5

Source: https://habr.com/ru/post/1539879/


All Articles