Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: "Global" filehandle without using global variables

by tobyink (Canon)
on Jun 18, 2012 at 21:22 UTC ( [id://976906]=note: print w/replies, xml ) Need Help??


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");
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: "Global" filehandle without using global variables
by Anonymous Monk on Jun 18, 2012 at 21:44 UTC

    Thanks for that. The OO method is very interesting (and educational) but using state within the subroutine is probably the best solution for me and does exactly what I was looking for.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found