LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2010, 03:45 AM   #1
brianmcgee
Member
 
Registered: Jun 2007
Location: Munich, Germany
Distribution: RHEL, CentOS, Fedora, SLES (...)
Posts: 399

Rep: Reputation: 40
[Perl] OOP Accessor Method for Array Attributes and Append Method


To learn about Perl and OOP, I would like to create an object that gives access to an array.

During construction of the object, the array should be empty.

The accessor method should either return the array or if you pass an array to the method, the objects array contents should be changed.

Then I would like to implement a method that appends a variable to the array.

With a regular variable-attribute this would be:

Code:
package Example;
use strict;
use warnings;

sub new {
   my ($class) = @_;
   my $self = {
      _someattribute => undef
}

sub someattribute {
   my ($self, $someattribute ) = @_;
   $self->{_someattribute} = $someattribute if defined($someattribute);
   return $self->{_someattribute};
}

return(1);
Then I could access that variable like this:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use Example;
my $example = new Example();

$example->someattribute('Test');

print $example->someattribute;
Can someone help me how this would look like for array-attributes?

/edit:

I found a solution that seems to work:

Code:
sub someattribute {
   my ( $self, @someattribute ) = @_;
   if (@_) { @{ $self->{someattribute} } = @_ }
   return @{ $self->{someattribute} };
}

Last edited by brianmcgee; 09-23-2010 at 04:16 AM. Reason: solved
 
Old 10-18-2010, 11:05 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Thanks for solution.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Not Allowed: The requested method POST is not allowed for the URL /writedhcp.p WiWa Linux - Networking 15 01-06-2011 01:20 PM
SlackWare :Method Not Allowed The requested method POST is not allowed for the URL slack31337 Linux - Software 0 04-08-2006 06:09 PM
method references in perl mschutte Programming 4 04-26-2005 09:46 AM
Java help (accessing array elemonts from another class or method) Tru_Messiah Programming 6 05-14-2004 09:20 AM
Perl undefined subroutine /can't locate object method cobien Programming 1 11-17-2003 10:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration