Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

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

the text gets into the object as shown by Data::Dumper.

Yes it does ... doesn't mean there is an accessor created (accessors are just "methods" aka "subroutines")

without has bar

use Data::Dump qw/ dd /; dd( my $f = Foo->new(qw/ bar 1 /) ); $f->bar; {package Foo; use Moo; } __END__ bless({ bar => 1 }, "Foo") Can't locate object method "bar" via package "Foo" at - line 3.

you can cheat and use $f->{bar} ... this is cheating / breaking encapsulation / peeking inside object / not using official api / so if sometime in the future you internally rename bar attribute to barps, anyone using ->{bar} would have to update program, anyone using the documented api ->bar wouldn't (because you'd update that too) ... yeah seems convoluted example but thats how they work :)

with has bar Without BEGIN block there is no bar accessor (accessor is subroutine ... all $obj->whatever is subroutines)

use Data::Dump qw/ dd /; dd( my $f = Foo->new(qw/ bar 1 /) ); $f->bar; {package Foo; use Moo; has( qw/ bar is rw / ); } __END__ bless({ bar => 1 }, "Foo") Can't locate object method "bar" via package "Foo" at - line 3.

with has bar and With BEGIN block no death :)

use Data::Dump qw/ dd /; dd( my $f = Foo->new(qw/ bar 1 /) ); $f->bar; BEGIN{package Foo; use Moo; has( qw/ bar is rw / ); } __END__ bless({ bar => 1 }, "Foo")

with has bar and Without begin block no death (moved to line 1)

{package Foo; use Moo; has( qw/ bar is rw / ); } use Data::Dump qw/ dd /; dd( my $f = Foo->new(qw/ bar 1 /) ); $f->bar; __END__ bless({ bar => 1 }, "Foo")

got that? See also perlobj, A Method is Simply a Subroutine, Attributes


In reply to Re^3: Moo error message not understood (BEGIN block missing) by Anonymous Monk
in thread Moo error message not understood by davies

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 rifling through the Monastery: (10)
As of 2024-04-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found