Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The closest thing to what you describe would be to make "PrintToScreen" a singleton object. Your new method would be something like:

my $instance; sub new { my ($class, $loglevel) = @_; $instance ||= do { print "$class $VERSION started with loglevel $loglevel\n" if $loglevel >= 3; bless { _usersetloglevel => $loglevel }, $class; }; }

That way, when your code calls PrintToScreen->new it will always return the same object, at the same loglevel.

Now, it needs to be said that singletons are generally considered code smell - i.e. a sign of poorly thought out code. There are valid use cases for them, but not many. Using a singletons for logging will come back to bite you when you decide you'd like for part of your code (perhaps the network interaction but, say) to be logged in great detail, but not the rest of your code. (Perhaps because you're trying to figure out a bug in the networking stuff without being distracted by hundreds of irrelevant messages from parts of the system you know are working well.)

Better for your logger to be a normal class so you can do:

my $network_connection = NetworkConnection->new; $network_connection->set_logger( PrintToScreen->new(3) ); my $database_connection = DatabaseConnection->new; $database_connection->set_logger( PrintToScreen->new(1) );

See also: Stack Exchange: Why is Global State so Evil?

PS: not sure why you're using "recursively" in your message title. Recursion in computer science is when a function (directly or not) calls itself. A quick example of recursion to implement integer multiplication using just addition and subtraction:

use strict; use warnings; sub multiply_integers { die "two positive integers!\n" if @_ != 2 || grep { not /^[1-9][0-9]*$/ } @_; my ($x, $y) = @_; return $x if $y == 1; return $x + multiply_integers($x, $y-1); } print multiply_integers(7, 6), "\n";
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Passing a variable recursively by tobyink
in thread Passing a variable recursively by timtowtdi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found