Sasa Stupar wrote:
>
> Hi!
Hello,
> I am new to perl. I have written a script to automatically subscribe to
> the mailing list. The part of the script is here:
> ---------------------------------------------
[changed formating to make it more readable]
> open (FILE,"/var/www/mlusers.tab");
You should ALWAYS verify that the file opened correctly.
open FILE, '/var/www/mlusers.tab' or die "Cannot open
'/var/www/mlusers.tab' $!";
> @linesaddr=<FILE>;
You don't really need to read the whole file into an array.
> $countaddr=@linesaddr;
> if ($countaddr!=0) {
There is no need for the $countaddr variable, you can use the array
directly.
if ( @linesaddr ) {
> foreach $lineaddr (@linesaddr) {
> @itemaddr=split(/\t/,$lineaddr);
> my $testn = @itemaddr[0];
^^^^^^^^^^^^
If you had warnings enabled this would have triggered one. You should
use a scalar not an array slice here.
my $testn = $itemaddr[0];
> $testn1 = "\"".$Config{'email'}."\"";
You could use an alternate quoting method so you don't have to escape
the double quotes.
$testn1 = qq("$Config{'email'}");
> if ($testn eq $testn1){
> $alrd = 1;
> }
> }
> }
> close (FILE);
> if ($alrd == 1) {&already_subscribed}
^
You shouldn't use an ampersand to call subs.
if ($alrd == 1) {already_subscribed()}
perldoc perlsub
> else {
> open(STAT,">> /var/www/mlusers.tab");
You should ALWAYS verify that the file opened correctly.
open STAT, '>> /var/www/mlusers.tab' or die "Cannot open
'/var/www/mlusers.tab' $!";
> print STAT "\"".$Config{'email'}."\""."\t"."\"RW\""."\n";
You could use an alternate quoting method so you don't have to escape
the double quotes.
print STAT qq("$Config{'email'}"\t"RW"\n);
Is there any particular reason that you enclose both fields in double
quotes?
> close(STAT);
> &return_html;
return_html();
> }
> -------------------------------------------------------
I would probably write it like this:
use warnings;
use strict;
use Fcntl qw(:seek);
my $file = '/var/www/mlusers.tab';
my $alrd;
open my $fh, '+<', $file or die "Cannot open $file: $!";
while ( <$fh> ) {
if ( /^"\Q$Config{'email'}\E"\t/ ) {
$alrd = 1;
last;
}
}
if ( $alrd ) {
already_subscribed();
}
else {
seek $fh, 0, SEEK_END or die "Cannot seek on $file: $!";
print $fh qq("$Config{'email'}"\t"RW"\n);
close $fh;
return_html();
}
> Now I would like to also write to automatically unsubscribe from the
> mailing list but I am stuck. I don't know and I didn't find how to
> remove one complete line from the text file.
> Here the text file is mlusers.tab. So each line has to have <NEWLINE>
> mark and in between the filled lines there should not be empty lines.
> Can somebody help me with this ?
You could use perl's in-place edit feature:
use warnings;
use strict;
( $^I, @ARGV ) = ( '.bak', '/var/www/mlusers.tab' );
while ( <> ) {
next if /^"\Q$Config{'email'}\E"\t/;
print;
}
Or you could use the Tie::File module:
use warnings;
use strict;
use Tie::File;
my $file = '/var/www/mlusers.tab';
tie my @data, 'Tie::File', $file or die "Cannot open $file: $!";
@data = grep !/^"\Q$Config{'email'}\E"\t/, @data;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]