in reply to How do I escape a :: in s///?

Actually you should use File::Tie module. It is a better module to use. Try to avoid running shell commands if you can.

#!/usr/bin/perl use warnings; use strict; use Tie::File; use File::Copy; use Sys::Hostname; my $host = hostname; my $group = 'sbo'; my $newstring = 'sbo::18659:x0929,x1465,x1368,x5552'; my $backup = "/tmp/groups"; copy ("$backup", "$backup.tmp") or die "can not copy $backup"; tie my @lines, 'Tie::File', "$backup" or die "can not open $backup $!" +; for (@lines) { if (/$group/) { $_ .= $newstring; last; } } untie @lines; print "\n $host \n";

Replies are listed 'Best First'.
Re^2: How do I escape a :: in s///?
by ikegami (Patriarch) on Aug 14, 2009 at 19:56 UTC

    I disagree. It's a huge waste of resources and it doesn't simplify anything. Compare

    copy($groups_qfn, $backup_qfn) or die("Can't backup $groups_qfn\n"); tie(my @lines, 'Tie::File', $groups_qfn) or die("can not open $groups_qfn: $!\n"); for (@lines) { if (/^$group:/) { $_ = "$newstring\n"; last; } } untie @lines;
    with
    copy($groups_qfn, $backup_qfn) or die("Can't backup $groups_qfn\n"); open(my $in_fh, '<', $backup_qfn) or die("Can't open $backup_qfn: $!\n"); open(my $out_fh, '>', $groups_qfn) or die("Can't open $groups_qfn: $!\n"); while (<$in_fh>) { if (/^$group:/) { $_ = "$newstring\n"; } print $out_fh $_ }