in reply to "Global" filehandle without using global variables
sub log_it { state $fh = do { open my $x, '>>', 'mylog.txt'; $x }; my $message = shift; my $timestamp = time; print $fh $timestamp . ": " . $message . "\n"; }
The above opens the file handle inside the sub. But because it uses a state variable, it keeps the filehandle open between calls.
One object oriented way would be something like this:
{ package Logger; use Any::Mo; has handle => (is => 'ro'); sub log_it { my ($self, $message) = @_; my $timestamp = time; print {$self->handle} $timestamp . ": " . $message . "\n"; } } my $logger = Logger->new(handle => $fh); $logger->log_it("Hello"); $logger->log_it("World");
A little overload magic can make things more concise...
{ package Logger; use Any::Mo; use overload '&{}' => sub { my $self = shift; return sub { $self->log_it(@_) }; }; has handle => (is => 'ro'); sub log_it { my ($self, $message) = @_; my $timestamp = time; print {$self->handle} $timestamp . ": " . $message . "\n"; } } my $logger = Logger->new(handle => $fh); $logger->("Hello"); $logger->("World");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "Global" filehandle without using global variables
by Anonymous Monk on Jun 18, 2012 at 21:44 UTC |