Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I write perl classes, but I don't know how to have a array or a hash in my $this variable ?

I have a pack.pm :

#!/usr/bin/perl -w
use strict;
use Parallel::ForkManager;
package Pack;

our $cgi = new CGI;

sub new {
    my ($classe, $nom, $nbports, $gio) = @_;

    my $this = {
    "nom"    => $nom,
    "nbports" => $nbports,
    "gio"   => $gio
    };

    bless($this, $classe);
    return $this;   
}
    ...
1;

I would like to have a @tab, I can access via $this->tab, but I don't want to give it in arg to the instance.
How does it work in Perl ?

Thanks.

share|improve this question
    
what are you trying to do? Do you want to have an @tab which is part of $this, but which you don't pass to new()? Do you want it to be created as an empty table so you can use it later as $this->tab? –  AAT May 16 '11 at 8:52
    
@AAT : Yes, I would like a @tab part of $this, and I don't pass to new(), an empty array, created on instanciation. And I can access via $this->tab. Thx –  eouti May 16 '11 at 9:00

3 Answers 3

up vote 3 down vote accepted

Given your answer to my comments, I think you want

my($this) = {
    "nom"     =>  $nom,
    "nbports" =>  $nbports,
    "gio"     =>  $gio,
    "tab"     =>  []
};

i.e. set $this->{tab} to be a reference to a new anonymous array.

Now you can reference it as you wish, e.g.

$this->{"tab"}[0] = "new value";
print "Table contains ", scalar(@{$this->{"tab"}}), "entries\n";
share|improve this answer
    
Thanks, that's what I need, how to access the i element ? print $this->{"tab"}[i] ? –  eouti May 16 '11 at 11:54
    
Is it possible to do the same for a hash ? "tab" => {} ? –  eouti May 16 '11 at 12:07
    
@eouti -- 1) That should do it and 2) Yes it is. Your $this itself is just a reference to an anonymous hash, and there is no reason that this can't in turn contain references to other hashes created the same way. –  AAT May 16 '11 at 14:06

Consider using Moose for your OO Perl needs.

I've created a Moose version of your object that includes an attribute with an attribute featuring Array trait delegation, inlcuding currying of delegated methods. Moose offers easy ways to generate powerful, encapsulated classes without writing reams of boilerplate.

I created a class Pack with attributes: nom, nbports, gio, and tab.

nom is a read-only string and is required when the object is created. nbports is a read-only integer value and defaults to 32 when not provided. gio is an optional, read-write boolean value. tab is an array of strings. All sorts of behavior has been defined for tab:

  • all_tabs returns a list of the contents of tabs
  • add_tab pushes values onto the end of tabs
  • tab_count returns a count of the elements in tabs
  • alpha_tabs returns a list of the members of tabs alphabetical order
  • turn_tabs returns a list of the strings in tabs, but with the letters in reverse

Any attempts to set an attribute with be checked for type correctness.

Moose creates all the required methods to support these complex behaviors with the following code:

package Pack;
use Moose;

has 'nom' => (
   is => 'ro',
   isa => 'Str',
   required => 1,
);

has 'nbports' => (
   is      => 'ro',
   isa     => 'Int',
   default => 32,
);

has 'gio' => (
   is  => 'rw',
   isa => 'Bool',
   predicate => 'has_gio',
);

has 'tab' => (
   traits  => ['Array'],
   is      => 'ro',
   isa     => 'ArrayRef[Str]',
   default => sub {[]},
   handles => {
      all_tabs    => 'elements',
      add_tab     => 'push',
      turn_tabs   => [ 'map', sub { reverse } ],
      tab_count   => 'count',
      alpha_tabs  => [ 'sort', sub { lc($a) cmp lc($b) } ],
  },
);

__PACKAGE__->meta->make_immutable;
no Moose;
1;

Usable like so:

 my $p = Pack->new( nom => 'Roger', tab => [qw( fee fie foe fum )] );

 my $gio_state = 'UNSET';
 if( $p->has_gio ) {
      $gio_state = $p->gio ? 'TRUE' : 'FALSE';
 }

 print  "GIO is $gio_state\n";

 my @turned = $p->turn_tabs; # eef eif eof muf
 $p->add_tabs( 'faa', 'fim' );
 my @sorted = $p->alpha_tabls; # faa fee fie fim foe fum

 my $count = $p->tab_count;  # 6

 my $ports = $p->nbports; # 32
share|improve this answer

try with:

sub set_tab {
my ($self, @tab) = @_;
$self->{ tab } = \@tab;
}
share|improve this answer
2  
I would add perldoc perldsc for completeness. –  Dallaylaen May 16 '11 at 8:32
    
And also sub get_tab { my $this = shift; return @{ $this->{tab} }; } –  Dallaylaen May 16 '11 at 8:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.