1

I have a perl module module.pm and a script.pl file which calls the module.pm is it possible that module.pm knows about files declared inside script.pl for example :

I have a file defination under script.pl something like this

use module;
open FILE,'>','file.txt' or die 'can\'t open file';

inide module.pm i want to use FILE handle for example :

package module

print FILE $value;

is it possible to do something like this and, if yes, how ?

1
  • You could create a method that would return the filehandle...
    – user554546
    Jan 20, 2012 at 16:50

3 Answers 3

5

If you use lexical filehandles instead of Barewords it is possible.

use module ;
open( my $filehandle , '>' , 'output.txt' ) or die 'Cannot open.' ;
module::some_fun( $filehandle ) ;

and in module.pm

package module ;
sub some_fun {
  my ( $filehandle ) = @_ ;
  print $filehandle "Hello world.\n" ;
}
1 ;

Hope that helps.

3
  • 2
    +1 for lexical filehandles. They've been around for more than a decade. Use 'em! :)
    – Robert P
    Jan 20, 2012 at 17:43
  • Direct filehandles can be used in the same way by passing a reference to the glob: module::some_fun(\*FILE);
    – Borodin
    Jan 20, 2012 at 21:35
  • @BradGilbert Yes, technically bareword filehandles would work as well since bareword filehandles are global. And thats why one should use lexical filehandles.
    – dgw
    Jan 20, 2012 at 21:46
3

It may be possible to do this if you don't use strict, but it's really bad practice. The better option is to pass the file handle to your module.pm method/function(s).

use module;
open my $FILE,'>','file.txt' or die 'can\'t open file';

somesub($FILE);

Then:

package module;

sub somesub {
    my $FILE = shift;
    ... 
}
3
  • You don't even need to drop use strict; I'm pretty sure Exporter can do this. Though, as you said, you shouldn't do this.
    – derobert
    Jan 20, 2012 at 17:01
  • @derobert: Maybe strict isn't necessary, but Exporter is for going the other direction. Jan 20, 2012 at 17:03
  • Once the symbol-table entries are aliased, I don't think the direction matters. Will test to be sure...
    – derobert
    Jan 20, 2012 at 17:11
3

As Flimzy answers, you should not do this. There are better ways to accomplish whatever it is you're trying to accomplish. I'll cover some in a second.

You can open a file in a different namespace than your package—or print to one:

package module;
print main::FILE "some text\n";

alternatively, in your program:

open(module::FILE, '>', '/tmp/file');

You could use caller to find the caller's package (instead of just assuming main). That way, you could always use the caller's FILE.

Better ways

Anonymous filehandles stored in variables

You really should be using globs stored in scalars nowadays. In other words, your open call should look like this:

open my $file, '>', '/tmp/file'

Then $file is an ordinary scalar, and you can pass it around like you would any other variable. So, maybe you'd have this code in your module:

package module;
sub foo {
    my $dest = shift;
    print $dest "hello, world\n";
}
1;

And then in your program, after that open, you'd call module::foo($file).

Anonymous filehandles and OO

If you have a bunch of routines in module that all take the same output filehandle, it can get tedious to pass it around all the time. Instead, create an object:

package module;
use Moose;

has output_fh => (
    is => 'rw',
    required => 1,
);

sub foo {
    my $self = shift;
    print $self->output_fh "hello, world\n";
}

Then, in your non-module code, you do something like this:

open my $fh, … ;
my $obj = module->new(output_fh => $fh);
$obj->foo();

the advantage of this approach grows as you start to carry around more state in $obj.

Anonymous filehandles and localized package variables

If you don't want to go the OO approach (which is generally cleaner than this approach) you can do this in your module:

package module;
our $OUTPUT_FH = undef; # or sensible default

sub foo {
    print $OUTPUT_FH "hello, world\n";
}

and then in your non-module code:

open my $fh, … ;
local $module::OUTPUT_FH = $fh;

module::foo();

The local-ization will make sure that when your current block exits, the previous value of $module::OUTPUT_FH is restored. This will make subroutine calls work (without trampling each other's settings) as long as each subroutine that changes it localizes it first.

If you must work with pre-5.6 perl:

Perl 5.6 (released 2000-03-22, almost 12 years ago) introduced filehandle autovivification that makes that open my $fh, … line work. In earlier versions, you need to make the glob yourself. The Symbol module provides an easy way:

use Symbol qw(gensym ungensym);
my $fh = gensym; # not sure if pre-5.6 had geniosym; if so, use that instead
open $fh, …
⋮
close $fh;
ungensym $fh;

If you want to go before perl 5, which became stable in 1995, then you're on your own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.