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

Re: Language features affect style

by Arunbear (Prior)
on Jun 08, 2009 at 12:36 UTC ( #769498=note: print w/replies, xml ) Need Help??


in reply to Language features affect style

A contrived example showing that private methods can be simulated with lexical coderefs:
use strict; use warnings; package Foo; sub new { bless {salary => 100} } my $private_method = sub { my $self = shift; my $factor = shift || 2; $self->{salary} *= $factor; }; sub get_salary { my $self = shift; $self->$private_method(10); return $self->{salary}; } package main; my $foo = Foo->new; printf "salary is %s\n", $foo->get_salary;
Admittedly the boilerplate issue is still there.

Replies are listed 'Best First'.
Re^2: Language features affect style
by TimToady (Parson) on Jun 08, 2009 at 15:17 UTC
    Indeed, which is why we've gotten rid of most of Perl 5's (not so) beloved boilerplate in Perl 6. Here's the same code (more or less) in Perl 6:
    class Foo { has $!salary; method !private_method ($factor = 2) { $!salary *= $factor } method salary { self!private_method(10); $!salary } } my Foo $foo .= new(:salary(100)); say "salary is ", $foo.salary;
    This may or may not tweak the refactoring knob back toward methods; Perl 6 is still not Java, after all.
      method salary { self!private_method(10); $!salary }
      Doesn't $!salary have to be called with an object? self!salary. Or has this been changed to figure that out for itself (implies self or $_ ?) now? I found it:
      For a call on your own private method, you may also use the attribute-ish form:
      $!think($pinky) # short for $(self!think($pinky))
      I'm guessing you put this in after you got STD in fine enough shape and knew that could actually be made to work?

      With analogous meaning for the other sigils (that's not stated in S12)?

      —John

        Huh? $!salary is simply the name of the private storage area for the attribute. No calling involved, since the method already knows its own self, and whole point of $!salary is to tell the method it can access the storage directly rather than through a virtual method. The extra information is only necessary when calling something private in another object.
      Perl 6 is still not Java, after all.

      Is it gasoline? No, perl is Camelia :)

        Camelia?


        holli

        When you're up to your ass in alligators, it's difficult to remember that your original purpose was to drain the swamp.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-01-18 17:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (53 votes). Check out past polls.

    Notices?