5

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.

antyrat
  • 27,479
  • 9
  • 75
  • 76
eouti
  • 5,338
  • 3
  • 34
  • 42
  • 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 08: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 09:00

3 Answers3

4

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";
AAT
  • 3,286
  • 1
  • 22
  • 26
  • 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
  • @eouti: how to create perl data structures and refer them you may read [here](http://stackoverflow.com/questions/23649118/perl-return-hash-from-subroutine/35792849#35792849) – Eugen Konkov Mar 06 '16 at 18:27
3

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
daotoad
  • 26,689
  • 7
  • 59
  • 100
2

try with:

sub set_tab {
my ($self, @tab) = @_;
$self->{ tab } = \@tab;
}
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72