use sed in perl

P

perry zhou

I used some legacy perl code to change a configuration file, it is
working and it replaces the line starts with XT_SCREEN_COUNT=1 with
the a new SCREEN_COUNT variable. But I don't quite understand what's
the meaning of the ' . in front of $numscreens and after $numscreens.
Could anyone give me a hint?

Thanks in advance!

$rc = system('sed -e
"s/^XT_SCREEN_COUNT=1/XT_SCREEN_COUNT='.$numscreens. '/g" '.
$resultdir.'tetexec.cfg>' . $resultdir. 'tetexec.cfg.tmp');

$numscreens is a variable which equals to a number, for example 2.
=======================================================================
#Part of the configuration file:

# available on the display, as returned by XScreenCount.
# the code above will change the 1 to 2.
XT_SCREEN_COUNT=1
 
S

Simon Taylor

Hello,
I used some legacy perl code to change a configuration file, it is
working and it replaces the line starts with XT_SCREEN_COUNT=1 with
the a new SCREEN_COUNT variable. But I don't quite understand what's
the meaning of the ' . in front of $numscreens and after $numscreens.
Could anyone give me a hint?

Thanks in advance!

$rc = system('sed -e
"s/^XT_SCREEN_COUNT=1/XT_SCREEN_COUNT='.$numscreens. '/g" '.
$resultdir.'tetexec.cfg>' . $resultdir. 'tetexec.cfg.tmp');

The code is concatenating single-quoted strings together and
interpolating variables to form an argument to the perl system() function.

You can read it as:

'some text in single quotes' . $somevariable . 'more text'

where the binary '.' operator is doing the string concatenation that the
'+' operator does in some other languages.

If the values of the perl variables $numscreens and $resultdir
were, say, 2 and '/tmp/', respectively, then the system function
would receive an argument that looked like:

'sed -e "s/^XT_SCREEN_COUNT=1/XT_SCREEN_COUNT=2/g" /tmp/tetexec.cfg>
/tmp/tetexec.cfg.tmp'

Needless to say, the original author of this code would find
that the same functionality is more easily done directly in Perl.
There's no need to use sed via a system call for this.

I hope this helps,

Simon Taylor
 
J

John W. Krahn

perry said:
I used some legacy perl code to change a configuration file, it is
working and it replaces the line starts with XT_SCREEN_COUNT=1 with
the a new SCREEN_COUNT variable. But I don't quite understand what's
the meaning of the ' . in front of $numscreens and after $numscreens.
Could anyone give me a hint?

Thanks in advance!

$rc = system('sed -e
"s/^XT_SCREEN_COUNT=1/XT_SCREEN_COUNT='.$numscreens. '/g" '.
$resultdir.'tetexec.cfg>' . $resultdir. 'tetexec.cfg.tmp');

$numscreens is a variable which equals to a number, for example 2.
=======================================================================
#Part of the configuration file:

# available on the display, as returned by XScreenCount.
# the code above will change the 1 to 2.
XT_SCREEN_COUNT=1

You don't need sed, you can do that in perl:

open IN, '<', "$resultdir/tetexec.cfg" or die "Cannot open
$resultdir/tetexec.cfg: $!";
open OUT, '>', "$resultdir/tetexec.cfg.tmp" or die "Cannot open
$resultdir/tetexec.cfg.tmp: $!";

while ( <IN> ) {
s/(?<=^XT_SCREEN_COUNT=)1\b/$numscreens/;
print OUT
}


John
 
P

perry zhou

Simon Taylor said:
Hello,


The code is concatenating single-quoted strings together and
interpolating variables to form an argument to the perl system() function.

You can read it as:

'some text in single quotes' . $somevariable . 'more text'

where the binary '.' operator is doing the string concatenation that the
'+' operator does in some other languages.

If the values of the perl variables $numscreens and $resultdir
were, say, 2 and '/tmp/', respectively, then the system function
would receive an argument that looked like:

'sed -e "s/^XT_SCREEN_COUNT=1/XT_SCREEN_COUNT=2/g" /tmp/tetexec.cfg>
/tmp/tetexec.cfg.tmp'

Needless to say, the original author of this code would find
that the same functionality is more easily done directly in Perl.
There's no need to use sed via a system call for this.

I hope this helps,

Simon Taylor

Thanks a lot for detailed explanation. As I don't know anything about
sed, and just started learning perl, I feel confused about the single
quotes used in the system call, now I understand it.

Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,798
Messages
2,569,651
Members
45,384
Latest member
GOLDY

Latest Threads

Top