[PLUG] Perl

Eric Wilhelm scratchcomputing at gmail.com
Mon Jul 3 19:56:12 UTC 2006


# from Roderick A. Anderson
# on Monday 03 July 2006 12:09 pm:

>sub getpassdb {
>   # Read password file creating usernames and
>   # passwords hash.
>
>   my %passworddb;
>
>   local $file_passwd = "/root/firewall/var/cgi_password" ;
s/local/my/

Why use local?  You should only do that when you need to localize a 
global variable.  Further, you should not have any global variable 
names of your own.  Only use local for things like $/ (which is why the 
OP didn't get more than one line.)

  local $/;
  my $wholefile = <$filehandle>;

>   open ( $INPASS, '<', $file_passwd" )
>     or die "Error--unable to open $file_passwd $!" ;

open(my $INPASS, '<', $file_passwd)

my $passworddb; # put this as close as possible to where you need it
>   while ( my $passline = <{ $INPASS }> ) {

while ( my $passline = <$INPASS> ) {

>     chomp $passline;
>
>     %passworddb = ( $passworddb, ( split ( '=', $passline ) ) );

No, Paul had the right thing happening here.  Were you trying to 
reassign the whole list with %passworddb = (%passworddb, %new_stuff) ?

The only suggestion I have for Paul is the lexical filehandle and limit 
the split with:

  my ($user, $pass) = split(/=/, $passline, 2);

>     return %passworddb;
>}

Remember.  Always follow best practices.  Even when golfing.

  use strict;use warnings; my %pw_db = sub {
  my $h;open($h,$_[0])?return(map({chomp;split(/=/,$_,2)} <$h>)):die $!
  }->($filename);

--Eric
-- 
"Beware of bugs in the above code; I have only proved it correct, not
tried it."
--Donald Knuth
---------------------------------------------------
    http://pdx.pm.org/
---------------------------------------------------



More information about the PLUG mailing list