Re: script perl with sed command

2007-04-08 Thread Giorgos Keramidas
On 2007-04-08 00:26, Garrett Cooper <[EMAIL PROTECTED]> wrote:
> Giorgos Keramidas wrote:
> > Try using Perl only, instead of forking sed(1), like this:
> > [...]
> > | sub supfile_set_default_host($$);
> > | sub supfile_set_default_host($$)
> > | {
[...]
> 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($$);
> {

That's actually a function prototype.  Prototypes are explained in:

man perlsub

in far more detail than I could ever describe in an email message :)

HTH,
Giorgos

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Giorgos Keramidas
On 2007-04-08 09:28, Garrett Cooper <[EMAIL PROTECTED]> wrote:
> Olivier Regnier wrote:
> The file has to exist that you're trying to modify, otherwise it'll give
> up :). Permissions issue?
> 
> Better to do that section may be:
> 
>  my $tmpsupfile;
>  my $supfile = "/etc/standard-supfile";
>  my $newhost = "cvsup.fr.freebsd.org";
> 
>  if (!defined($supfile) || !defined($newhost)) {
>  return undef;
>  }
> 
>  $tmpsupfile = new File::Temp();
>  die "$!" unless(defined($tmpsupfile);
>  open(SUP, "$supfile") or die "$!";

Ah, much better, thanks :)

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Giorgos Keramidas
On 2007-04-08 11:40, Olivier Regnier <[EMAIL PROTECTED]> wrote:
> Giorgos Keramidas a ?crit :
> >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 = )) {
> >| 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');
> >|
> >`---
> [...]
>
> Hello and thanks for this perl script. I'm new in perl and when i test
> him, i have an error that says:
>
> No such file or directory at myscript.pl line 18
> 
> line 18 = open(TMP, "> $tmpsupfile") or die "$!";

Line 18 is not an open command, so something odd is happenning when you
copy/paste the script from your mailer.  Try downloading a copy of teh
script from:

http://people.freebsd.org/~keramida/files/supfile.perl

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Garrett Cooper
Olivier Regnier wrote:
> Giorgos Keramidas a écrit :
>> On 2007-04-07 17:31, Olivier Regnier <[EMAIL PROTECTED]> 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 = )) {
>> | 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
>>
>>   
> Hello and thanks for this perl script. I'm new in perl and when i test
> him, i have an error that says:
> No such file or directory at myscript.pl line 18
> 
> line 18 = open(TMP, "> $tmpsupfile") or die "$!";
> 
> #!/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 = "/etc/standard-supfile";
> my $newhost = "cvsup.fr.freebsd.org";
> 
> 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 = )) {
> 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');
> 
> 
> Thanks again for your help.

The file has to exist that you're trying to modify, otherwise it'll give
up :). Permissions issue?

Better to do that section may be:

 my $tmpsupfile;
 my $supfile = "/etc/standard-supfile";
 my $newhost = "cvsup.fr.freebsd.org";

 if (!defined($supfile) || !defined($newhost)) {
 return undef;
 }

 $tmpsupfile = new File::Temp();
 die "$!" unless(defined($tmpsupfile);
 open(SUP, "$supfile") or die "$!";


You need to add:

use File::Temp;

to the top of your script after "use strict;" though, and the Perl
module *should* be available with standard installations.

Cheers,
-Garrett
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Olivier Regnier

Giorgos Keramidas a écrit :

On 2007-04-07 17:31, Olivier Regnier <[EMAIL PROTECTED]> 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 = )) {
| 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

  
Hello and thanks for this perl script. I'm new in perl and when i test 
him, i have an error that says:

No such file or directory at myscript.pl line 18

line 18 = open(TMP, "> $tmpsupfile") or die "$!";

#!/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 = "/etc/standard-supfile";
my $newhost = "cvsup.fr.freebsd.org";

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 = )) {
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');


Thanks again for your help.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Josh Carroll

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


No, he's using a function prototype. In this particular case, he's
saying the supfile_set_default_host function will take two scalars as
arguments.

For more info:

perldoc perlsub

Josh
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Garrett Cooper
Giorgos Keramidas wrote:
> On 2007-04-07 17:31, Olivier Regnier <[EMAIL PROTECTED]> 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 = )) {
> | 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
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-08 Thread Giorgos Keramidas
On 2007-04-07 17:31, Olivier Regnier <[EMAIL PROTECTED]> 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 = )) {
| 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

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-07 Thread Garrett Cooper

Olivier Regnier wrote:

Hello,

I have a problem with my perl script with the command sed. Here is a 
example of my code:


# 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');

But in console i have this message:
sed: 1: "s|*default host=(.*)|*d ...": unescaped newline inside 
subsitute pattern


Can you help me please ?

Thank you :)
   You forgot "chomp $server;" All commands with backticks ('`') have 
newlines inserted after them.
   There should only be one reference to "*default host=" though, so 
why are you going through the trouble of using sed?

   This can be done inline with perl as well.
-Garrett
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-07 Thread Olivier Regnier

Warren Block a écrit :

On Sat, 7 Apr 2007, Olivier Regnier wrote:

I have a problem with my perl script with the command sed. Here is a 
example of my code:


# 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');

But in console i have this message:
sed: 1: "s|*default host=(.*)|*d ...": unescaped newline inside 
subsitute pattern


Most likely there is a newline at the end of $server because it was 
output from backticks.  To fix that, you'd use chomp:


chomp(my $server = `fastest_csvsup -Q -c us`);

Just a general note: Perl's s/ command does more than sed's, with less 
hassle, and you wouldn't have to mess with shell escapes.


-Warren Block * Rapid City, South Dakota USA
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to 
"[EMAIL PROTECTED]"

Ok thank you for your answer. The script work well.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: script perl with sed command

2007-04-07 Thread Warren Block

On Sat, 7 Apr 2007, Olivier Regnier wrote:

I have a problem with my perl script with the command sed. Here is a example 
of my code:


# 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');

But in console i have this message:
sed: 1: "s|*default host=(.*)|*d ...": unescaped newline inside subsitute 
pattern


Most likely there is a newline at the end of $server because it was 
output from backticks.  To fix that, you'd use chomp:


chomp(my $server = `fastest_csvsup -Q -c us`);

Just a general note: Perl's s/ command does more than sed's, with less 
hassle, and you wouldn't have to mess with shell escapes.


-Warren Block * Rapid City, South Dakota USA
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


script perl with sed command

2007-04-07 Thread Olivier Regnier

Hello,

I have a problem with my perl script with the command sed. Here is a 
example of my code:


# 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');

But in console i have this message:
sed: 1: "s|*default host=(.*)|*d ...": unescaped newline inside 
subsitute pattern


Can you help me please ?

Thank you :)
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"