Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^2: Stuck on packages

by Yoda_Oz (Sexton)
on Jan 04, 2007 at 04:46 UTC ( [id://592866]=note: print w/replies, xml ) Need Help??

in reply to Re: Stuck on packages
in thread Stuck on packages

how would i write a "getter" method?

Replies are listed 'Best First'.
Re^3: Stuck on packages
by duckyd (Hermit) on Jan 04, 2007 at 06:25 UTC
    A simple getter method might look like:
    sub get_foo { my $self = shift; return $self->{foo}; }
    use strict; use warnings; package Foo; sub new { my $class = shift; return bless { foo => "bar!\n" }, $class; } sub get_foo { my $self = shift; return $self->{foo}; } 1; my $foo = Foo->new; print $foo->get_foo;
    The above code will print bar! There are many modules on CPAN that can create getter and setter methods for you, Class::Accessor and Class::MethodMaker are a couple of examples
      ok i understand now!
      so what would i need to code if i wanted something like this?
      sub new { my $class = shift; return bless { engine => "engine", wheels => "wheels", etc...}, $c +lass; } sub get_foo { my $self = shift; return $self->{engine,wheels,etc...}; }
      how do i make it an array or hash or whatever?
        Within the getter method you can access any of the attributes, so you just have to put them in the data structure you are wanting to return, such as:
        sub get_as_array { my $self = shift; return [ $self->{engine}, $self->{wheels} ]; }
        Is there something else you were wanting to do? If you dump the return of this, you'll see it's in an array.
Re^3: Stuck on packages
by SheridanCat (Pilgrim) on Jan 04, 2007 at 07:22 UTC
    You've already had Damian's book recommended to you. For instant gratification, look at perlboot. You seem to be lacking some basic understanding of how OOP works in Perl.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-04-15 11:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found