Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^4: Array of operators ...

by AnomalousMonk (Archbishop)
on Sep 14, 2013 at 11:38 UTC ( [id://1054096]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Array of operators ...
in thread Array of operators ...

... tighter encapsulation ...

I doubt any of this will be new to you, but just to clarify my thoughts for myself, I suppose what I really had in mind was actually two separate and disparate effects:

  • If a 'private' closure is not further encapsulated in a use-d module, pesky order-of-evaluation effects can manifest:
    >perl -wMstrict -le "print 'before: ', defined S() ? S() : 'undefined'; ;; { my $x = 42; ;; sub S { return $x; } } ;; print 'after: ', defined S() ? S() : 'undefined'; " before: undefined after: 42
  • If such a closure is encapsulated in a used-d module, the order-of-evaluation problems go away (assuming you're not doing any fancy-schmancy CTFE in the module, I think this is reliably true), but the 'tighter' aspect, strictly speaking, then kicks in: a state variable really is private to the function in which it's defined, but 'private' closures allow the sharing of access to variables among several functions — in itself, a highly useful property in certain circumstances!
    >perl -wMstrict -le "print M::get_x(); ;; use M; ;; print M::get_x(); " 42 42
    Where M.pm is:
    package M; { my $x = 42; sub get_x { return $x; } sub set_x { return $x = $_[0]; } } my $y = 1729; sub cannot_access_x { return $y; } 1;

Oh, and:

... consistently 2% faster ...

I'm not sure I really believe in a 2% performance difference reported by Benchmark, but as long as it's you...!

Replies are listed 'Best First'.
Re^5: Array of operators ...
by kcott (Archbishop) on Sep 14, 2013 at 12:30 UTC

    Thanks for the feedback. I agree with your points. I have been tripped up by that before/after effect in the past and needed to reorder my code to deal with it.

    I had expected a minimal overhead and 2% reflects this. Obviously, being an integer percentage, it doesn't exhibit a huge amount of precision. I usually run Benchmark several times, throw away any outliers and report an average or range. In this instance, all 3 or 4 runs produced 2%. It indicates an overhead but I wouldn't be setting my watch by it. :-)

    -- Ken

Log In?
Username:
Password:

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

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

    No recent polls found