Sagacity has asked for the wisdom of the Perl Monks concerning the following question:

For the benefit of those who didn't get to see the original post of this question

I was confused about the movement of the actual data and how it is accessed in sub-routines within a package.

I was also confused about the structure/style I could use to write code that I didn't have to continually use my within those sub-routines, since the constructor created those variables to start with. This was answered by kyle with a simple change of scope, or the use of $self->{variable}.

The second part of the node is still up for grabs as far as a good visual for the arrangement of data flowing to the package.

The reason for this question is: When I use sub-routines in a package. If I wanted to get the value that was passed, I had to use the my $var = shift;, otherwise, the value passed didn't get assigned. So to clarify -- the question really is; What are we shifting?

I believe it is the array @_ .

Any pointers to docs, or answers will be greatly appreciated.

If there is a way that one of you gurus could help draw a simplistic picture of a few things in OOP Perl, it would be wonderful.

Since Kyle showed me the visual for encapsulation, Thanks Kyle!

I am still pondering the visual on the look of the data as if it were maybe an array like so:

information_heading_to_server{Classname, first_piece_of_data, second_piece, third_piece}.

This may seem trivial, but it helps me to understand the

my $self = shift; followed by my $whatever = shift;

I get the self shift call to handle the first argument being the classname, but what about the others? Is the classname also the first element there too?

Replies are listed 'Best First'.
Re: Something I'd really like to know about OOP Perl.
by cdarke (Prior) on Jan 10, 2007 at 13:13 UTC
    Some of your questions relate to the way that Perl modules work, and I suggest you get some experience of writing ordinary modules before you dive into OO. Play with namespaces, my, and our, but ignore exporting.
    Now read the references mentioned by daveorg. I would add Daminan Conway's books.
    The magic that creates an "object" is the bless command, and all that does is attach a tag to a reference indicating which package methods should be called from at runtime. That package need not even exist for bless to work, the tag is used when a method is called on that object (or ref is used).
    A constuctor subroutine is often called 'new', but it need not. The first argument passed is the class name, subsequent arguments passed are application specific. So, when you say that Perl complained about $self->{link_file}, it looks to me that $self has been constructed elsewhere, as is/should be a reference to a hash - but you do not say what the error message was.
    An object method gets the object reference (a blessed reference) as its first argument. What variables should be set in a method depends on the application, but remember there is no persistence of my variables declared within the sub unless you grab a reference to them.
      Thank you for the reply!
Re: Something I'd really like to know about OOP Perl.
by davorg (Chancellor) on Jan 10, 2007 at 12:24 UTC

    It's not really clear what you're asking. But perhaps reading the perlboot, perlobj and perltoot manual pages might help you.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thank you for the reply! And it was reading those that got me thinking about this.
Re: Something I'd really like to know about OOP Perl.
by kyle (Abbot) on Jan 10, 2007 at 15:05 UTC

    How can I create a package that allows me to use scalars within that package that are Global to the package, but Encapsulated from the rest of the cruel mean world?

    Have a look at inside out objects. Modules Class::Std or Object::InsideOut make them easy. This might give you the idea:

    package SafeHaven; { my $glow_ball; sub get_glow_ball { return $glow_ball; } sub set_glow_ball { my $self = shift; $glow_ball = shift; } }

    The lexically scoped $glow_ball is available to every sub inside its scope. The rest of your code can stay the same (but also go in the lexical scope) and still work, but you can also change it to get other advantages of inside-out objects.

      Thank You Kyle! This is what I was looking for. Wouldn't you know it was simply using this style of package layout. It didn't occur to me, Thanks for the visual:{)
Re: Something I'd really like to know about OOP Perl.
by shmem (Chancellor) on Jan 10, 2007 at 14:43 UTC

    First, the formatting of your post is poor, go read Writeup Formatting Tips. This link is displayed within the tips displayed below the node composition textbox. Read those tips.

    Read perltoot, which is a fine OO tutorial and will answer most if not all of your questions.

    How can I create a package that allows me to use scalars within that package that are Global to the package, but Encapsulated from the rest of the cruel mean world?

    Choose any of these:

    package Records_tagged; $Records_tagged::link_file = '/path/to/some/file'; # fully qualified s +calar our $link_file; # lexical scoped gl +obal use vars qw($link_file); # package scoped gl +obal

    See our.

    But from your code it is not clear wether you need package globals at all.

    How you call the nethod new?

    package Records_tagged; use strict; sub new { my $class = shift; my $self = shift; # this only makes sense with Records_tagged->ne +w(\%hash) $self->{link_file} = undef;

    Are you passing in a hash reference? If not, you want to say

    sub new { my $class = shift; my $self = {}; # create an anonymous hash $self->{link_file} = undef;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Sorry for the format!
      I had to run out the door and do the daily "Pay the bills thing". I actually wrote the question in my text editor, then cut and pasted it. Needless to say, the clearly edited text editor format didn't hold after the paste, and I was simply out time.
Re: Something I'd really like to know about OOP Perl.
by kyle (Abbot) on Jan 10, 2007 at 16:08 UTC

    Looking at your code, I think one thing that you're missing is that the $self you create in new is also available in display. You could do this:

    sub display { my $self = shift; my $link_file = $self->{link_file}; my $adate = $self->{adate}; # etc. }

    That's true as long as display is called as an instance method (i.e., as you say, $whatever->display()). Called as a class method (Records_tagged->display()), all it gets for $self is the string, 'Records_tagged'.

      Yep, I get it now. And again, thank all of you for the posts. As I said I am self taught. I do and will read the references all of you have given, including the one about the postings. I don't want to be offensive. Thanks! By the way, confused is my normal state of mind!