SPUG: Looking for a graceful way to deal with file handles and modules

Christopher Maujean trance at drizzle.com
Tue Sep 28 18:02:23 CDT 1999


try the FileHandle package...
##
## I have used this package before, and it works like a charm.
##
## Warning:
## *******untested code ahead....

use FileHandle;

my $filename = "/etc/passwd";
my $dictfile = "/tmp/trance/passdict";

my $fh = new Filehandle;
my $dh = new Filehandle;

open ($dh, $dictfile) or die "$!";
my @dict = <$dh>;
close $dh;

open ($fh, $filename) or die "$!";

WLOOP:
while (<$fh>) {
	my ($name, $pwd) = (split /:/, $_);
	foreach $word (@dict) {
		print "$name \= $pwd\n" if crypt($word) eq $pwd;
		next WLOOP;
	}
} 			

close $fh;
	
## use $fh wherever you would use a "normal" filehandle
## except its easier to pass copies to subs, etc...
## but I wouldnt try to simultaneously write to this file
## from different threads or forked children, unless you really
## know what you are doing..(or if the file is unimportant)
## 


__END__

On Tue, 28 Sep 1999 caseyt at metaip.checkpoint.com wrote:

> Hi again Perl gurus and generally smart people,
> 
> I have a question about file handles and modules and how they can get all
> mixed up. Here is what I have:
> 
> Main Script (many of these)
> 	Opens and writes to a file handle.
> 
> Perl Module (has all of the subroutines for all the main scripts)
> 	Also needs to write to the file.
> 
> The solutions I can think of are:
> 
> 1. Open and close the file handle every time I write to it.
> 2. Have file handles open to the file in the module and the script at the
> same time.
> 
> Is there some graceful way that I can reference an open filehandle in my
> main script from the Perl module that has been called? Something pretty like
> main::filehandle? 
> 
> Thanks again,
> 
> Casey
> 
> 
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
>         Email to majordomo at pm.org: ACTION spug-list your_address
> 
> 


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list