Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to approach this:</p> <h2>1. Package variables</h2> <p>Not each animal instance has an UA, but the UA is part of being an Animal. You can initialize the UA on startup. The animals can access the UA via an accessor, or a variable:</p> <pre><code>package Animal; my $ua = LWP::UserAgent-&gt;new(); sub ua { $ua } </code></pre> <p>and then</p> <pre><code>my $ua = $self-&gt;ua(); </code></pre> <p>or</p> <pre><code>package Animal; our $ua = LWP::UserAgent-&gt;new(); </code></pre> <p>and then</p> <pre><code>my $ua = $Animal::ua; </code></pre> <p>Technical terms for this are <em>class variable</em>, or in languages like Java <em>static method</em> or <em>static variable</em>.</p> <p>Java translations:</p> <pre><code>class Animal { private static LWP.UserAgent ua = new LWP.UserAgent(); public static LWP.UserAgent getUa() { return ua } ... } </code></pre> <p>and</p> <pre><code>class Animal { public static LWP.UserAgent ua = new LWP.UserAgent(); ... } </code></pre> <h2>2. <code>state</code> variables</h2> <p>In newer Perls (>= v5.10?) you can declare variables with <code>state</code>. They declare lexical variables. The variable is initialized once during the lifetime of the program, not every time the statement is executed (as it would be with <code>my</code>).</p> <pre><code>use feature 'state'; package Animal; sub new { my ($class, %args) = @_; state $ua = LWP::UserAgent-&gt;new; bless { ua =&gt; $ua, %args } =&gt; $class; } </code></pre> <p>and then</p> <pre><code>my $ua = $self-&gt;{ua}; </code></pre> <p>It may be useful to use a <code>do</code> block to perform wider intialization</p> <pre><code>state $ua = do { my $ua = LWP::UserAgent-&gt;new; ...; $ua; }; </code></pre> <p>or to offload initialization into a sub: <code>state $ua = make_ua();</code></p> <p>If you have to target earlier perls, you can enclose the <code>new</code> sub inside a seperate scope, and declare the variable there as a lexical for similar effect:</p> <pre><code>package Animal; { my $ua = LWP::UserAgent-&gt;new; sub new { my ($class, %args) = @_; bless { ua =&gt; $ua, %args } =&gt; $class; } } </code></pre> <p>The only difference here is <em>when</em> the UA is initialized. (And, of course, the number of irritating braces.) The intialization can be deffered, as <a href="https://stackoverflow.com/users/40285/len-jaffe">Len Jaffe</a> mentioned:</p> <pre><code>package Animal; { my $ua; # just declare scope here sub new { my ($class, %args) = @_; $ua ||= LWP::UserAgent-&gt;new; # $ua is false until initialized bless { ua =&gt; $ua, %args } =&gt; $class; } } </code></pre> <p>Because objects are references, all animals will get the same UA.</p> <p>Especially in C/C++, this is also called a <em>static variable</em>. The <code>state</code> modifier could be seen as a non-OO singleton constructor.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload