Search code examples
javaperlmoose

What is the Perl equivalent of this Java snippet? (Java explanation in Perl terms)


Can anybody help me "translate" this java snippet into perl/Moose terms?. Trying understand java object-syntax/logic and I know only perl.

EDIT based on comments: The snippet is coming form the xwiki package - http://platform.xwiki.org/xwiki/bin/view/DevGuide/WritingComponents... It is too big for analyze, so what about the remaining of the code? Is possible (for the explanation only - ignore the @lines? What is the general meaning of the '@something'?

@Component("hello")
public class HelloWorldScriptService implements ScriptService
{
    @Requirement
    private HelloWorld helloWorld;

    public String greet()
    {
        return this.helloWorld.sayHello();
    }
}

Looking for something like the next perl fragment, but have no idea about "@Component, @Requirement - etc :(

package HelloWorldScriptService;
use Moose;
sub greet {
    return $self->
}

Exists some docs where java is explained with perl-ish terms? (at least, some basics)


Solution

  • not sure about the @Component, but here's some more:

    package HelloWorld;
    use Moose;
    
    sub say_hello {
       print "Hello";
    }
    
    1;
    
    package HelloWorldScriptService;
    use Moose;
    
    has 'hello_world' => ( is => 'rw', isa => 'HelloWorld' );
    
    # TODO - will need to instantiate the hello_world object somewhere...
    
    sub greet {
       my ($self) = @_;
       return $self->hello_world->say_hello();
    }
    
    1;
    

    since helloWorld is a private attribute in the original, you might want to prepend the name 'hello_world' with an underscore (and add init_args => undef) - this doesn't enforce privacy like Java but does show to anyone looking at the code that it's conceptually private (and prevents setting on new())