Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: running same bit of code twice

by pfaut (Priest)
on May 04, 2003 at 17:45 UTC ( #255499=note: print w/replies, xml ) Need Help??


in reply to running same bit of code twice

I would recommend reading the file in once. Then updating the array by inserting each new entry in it's proper place with splice. When finished inserting all the new entries, write the file out once.

my @reg_addrs = qw(bob@domain1.com bob@domain2.com); open FH,"<",$datafile or die "Could not read virtual file"; my @virtual = <FH>; close FH; for my $reg_addr (@reg_addrs) { my ($name,$domain) = split /@/,$reg_addr; my ($i) = 0; for my $entry (@virtual) { ++$i; if ($entry =~ /\Q$domain\E/) { splice @virtual,$i,0,"$reg_addr\t$user\n"; last; } } } open FH,">",$datafile or die "Could not write virtual file"; print FH @virtual; close FH;
90% of every Perl application is already written.
dragonchild

Replies are listed 'Best First'.
Re: Re: running same bit of code twice
by jonnyfolk (Vicar) on May 04, 2003 at 18:30 UTC
    Hi pfaut,

    I'm interested in the phrasing of the open command:

    open FH,"<",$datafile or die "Could not read virtual file";
    I habitually use:
    open FH, "$datafile" or die "Can't open $datafile: $!";
    although I'm aware of the  < being an 'open for read only' tag I would use it as follows:
    open FH, "< $datafile" or die "Can't open $datafile: $!";
    I wondered if you would talk me through your use of it and why you might consider it necessary to use   < at all.

    Thanks for your help

      3-arg open is considered more secure than 2-arg open, as perl won't be looking to change it's behavior based upon any special characters (such as < > |) (update: in the filename). Since 3-arg open has no default (you can't leave the second arg undefined), you must explicitly indicate "open for read-only". Finally, some people simply like to be explicit. Why would you write
      foreach my $apple (qw(McIntosh Empire Gala)){ $apple }
      When this would do just fine
      foreach (qw(McIntosh Empire Gala)){ $_ }

      --
      I'm not belgian but I play one on TV.

Re: Re: running same bit of code twice
by Anonymous Monk on May 04, 2003 at 17:57 UTC
    Hi
    Thanks for the info. As the bit before the @domainX.com is not going to be static, how would i change it to $username@domain1.com $username@domain2.com? I have tried  my @reg_addrs = qw($username@domain1.com $username@domain2.com); but this simply writes $username@domainX.com to the file and not the contents of $username.
      That's because qw doesn't interpolate variables. So change that line to
      my @reg_addrs = ("$username\@domain1.com", "$username\@domain2.com");
      and you're set.
        cheers guys, your both diamonds

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-02-27 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?