in reply to Seeking and Printing

Here's one way to do it, using Tie::File. A couple of caveats:

#! /usr/bin/perl use strict ; use warnings ; $|++ ; use Tie::File ; my $datafile = 'test_a.dat' ; my $username = 'terry' ; my $req_addr = 'tag@foo.com' ; my ( $name, $domain ) = $req_addr =~ /^([\w.-]+)(@[\w.-]+)$/ ; tie my @virtual, 'Tie::File', $datafile or die $! ; my $row = 0 ; $row++ until $virtual[$row] =~ $domain ; $virtual[$row] .= "\n$req_addr\t$username\n" ; untie @virtual ; __END__

_______________
DamnDirtyApe
Those who know that they are profound strive for clarity. Those who
would like to seem profound to the crowd strive for obscurity.
            --Friedrich Nietzsche

Replies are listed 'Best First'.
Re: Re: Seeking and Printing
by jimbobn (Novice) on Aug 14, 2002 at 18:04 UTC
    is there any way to do it with out the additional modules? there should be no problems with the domain not existing either.

      Yes, though Tie::File isn't really an "additional module," since it's now part of the 5.8.0 distribution. Here's how you could do it w/o Tie::File:

      #! /usr/bin/perl use strict ; use warnings ; $|++ ; my $datafile = 'test_a.dat' ; my $username = 'terry' ; my $req_addr = 'tag@foo.com' ; my ( $name, $domain ) = $req_addr =~ /^([\w.-]+)(@[\w.-]+)$/ ; open FH, $datafile or die "Couldn't read $datafile: $!" ; my @virtual = <FH> ; close FH ; open FH, ">$datafile" or die "Couldn't write $datafile: $!" ; my $wrote_it = 0 ; for ( @virtual ) { print FH $_ ; if ( $_ =~ /$domain/ && !$wrote_it ) { print FH "$req_addr\t$username\n" ; $wrote_it++ ; } } close FH ; __END__

      _______________
      DamnDirtyApe
      Those who know that they are profound strive for clarity. Those who
      would like to seem profound to the crowd strive for obscurity.
                  --Friedrich Nietzsche
        Thanks very much for your time mate. That worked a treat. Big Up DamnDirtyApe! :-)