Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 08 Apr 2007 00:26:37 -0700
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: script perl with sed command
Message-ID:  <4618992D.5050106@u.washington.edu>
In-Reply-To: <20070407171120.GA70957@kobe.laptop>
References:  <4617B954.7010507@steelbox.org> <20070407171120.GA70957@kobe.laptop>

next in thread | previous in thread | raw e-mail | index | archive | help
Giorgos Keramidas wrote:
> On 2007-04-07 17:31, Olivier Regnier <oregnier@steelbox.org> wrote:
>> Hello,
>> I have a problem with my perl script with the command sed. Here is a
>> example of my code:
> 
> Don't use system("sed ...") in Perl.  It's considered poor style, since
> Perl can do the same without having to fork a shell/sed process.
> 
>> # Selecting the fast server
>> print "Using the server called $server";
>> system(`/usr/bin/sed 's|\*default host=\(.*\)|\*default host=${server}|' 
>> $standard_supfile > $standard_supfile.copy`);
>> system('/bin/mv $standard_supfile.copy $standard_supfile');
> 
> Try using Perl only, instead of forking sed(1), like this:
> 
> ,-----------------------------------------------------------------------
> |
> | #!/usr/bin/perl -Tw
> |
> | use strict;
> |
> | #
> | # supfile_set_default_host($supfile, $newhost)
> | #       Set the default host used by the supfile $supfile to the
> | #       host name supplied as $newhost.
> | #
> |
> | sub supfile_set_default_host($$);
> | sub supfile_set_default_host($$)
> | {
> |     my $tmpsupfile;
> |     my $supfile = shift;
> |     my $newhost = shift;
> |
> |     if (!defined($supfile) || !defined($newhost)) {
> |         return undef;
> |     }
> |
> |     $tmpsupfile = "tmp-" . $supfile;
> |     open(SUP, "$supfile") or die "$!";
> |     open(TMP, "> $tmpsupfile") or die "$!";
> |
> |     my $line;
> |     while (defined($line = <SUP>)) {
> |         chomp $line;
> |         $line =~ s/^(\*[ \t]*default[ \t][ \t]*host[ \t]*=).*/$1${newhost}/;
> |         print TMP "$line\n";
> |     }
> |     close(TMP) or die "$!";
> |     close(SUP) or die "$!";
> |     rename("$tmpsupfile", "$supfile") or die "$!";
> |     return 1;
> | }
> |
> | supfile_set_default_host('standard-supfile', 'cvsup.example.net');
> |
> `-----------------------------------------------------------------------
> 
> This is slightly more complex than forking a sed(1) utility run, but
> it's easier to understand (at least it is for me).
> 
> A very brief run of the script seems to work here:
> 
> ,-----------------------------------------------------------------------
> |
> | $ pwd
> | /tmp
> | $ cp /usr/share/examples/cvsup/standard-supfile .
> | $ grep 'default host' standard-supfile
> | *default host=CHANGE_THIS.FreeBSD.org
> | $ perl -Tw supfile.pl
> | $ grep 'default host' standard-supfile
> | *default host=cvsup.keramida
> | $
> |
> `-----------------------------------------------------------------------
> 
> - Giorgos

Interesting. Is that old perl syntax (v4, etc)? Just curious because
most of the documentation and examples switched to:

sub supfile_set_default_host
{

syntax as opposed to:

sub supfile_set_default_host($$);

sub supfile_set_default_host($$);
{

partly for abbreviation's sake and because I think they moved the
meaning of $$ to the running PID of the script, correct?

I'm rather new to the Perl game though to be honest (been playing around
with Perl for only the past 3 years -- and in particular over the past
2~3 months), so my take on the language could be off.

Thanks for the history lesson in advance :),

-Garrett



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4618992D.5050106>