If you enable strict pragma, the code does not even compile:
Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 15.
Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 29.
Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 30.
Global symbol "@_IncludePath" requires explicit package name at Packet.pm line 40.
Do not use @ without quotes in your keys, because this confuses the parser. I recommend deleting them completely so as not to confuse readers with the people of your code.
It seems you want to output all attribute values โโfrom the arguments to the constructor, so keep discarding scalar values โโwith shift , and then everything else should be included in the path.
I assume that the components of the include path will be simple scalars, not links; if so, you will want to make deep copies for security.
sub new { my $class = shift; my $Packet = { _PacketName => shift, _Platform => shift, _Version => shift, _IncludePath => [ @_ ], }; bless $Packet, $class; }
Note that there is no need to store the blessed object in a temporary variable, and then immediately return it due to the semantics of Perl subnets :
If there is no return and if the last statement is an expression, its value is returned.
The methods below will also use this function.
Given the constructor above, GetIncludePath becomes
sub GetIncludePath { my( $Packet ) = @_; my @path = @{ $Packet->{_IncludePath} }; wantarray ? @path : \@path; }
Here are a few things going on. First, note that we are trying to return a copy of the include path, not a direct link to an internal array. This way, the user can change the value returned from GetIncludePath without worrying about resetting the state of the package.
The wantarray allows a subordinate to determine the context of their call and respond accordingly. In the context of the list, GetIncludePath will return a list of values โโin the array. Otherwise, it returns a link to a copy of the array. So client code can call it either in
foreach my $path (@{ $packet->GetIncludePath }) { ... }
or
foreach my $path ($packet->GetIncludePath) { ... }
SetIncludePath then
sub SetIncludePath { my ( $Packet, @IncludePath ) = @_; $Packet->{_IncludePath} = \@IncludePath; }
Note that you could use the same code in the constructor and not delete one parameter at a time using shift .
You can use the class defined above, as in
#! /usr/bin/perl use strict; use warnings; use Packet; sub print_packet { my($p) = @_; print $p->GetPacketName, "\n", map(" - [$_]\n", $p->GetIncludePath), "\n"; } my $p = Packet->new("MyName", "platform", "v1.0", qw/ foo bar baz /); print_packet $p; my @includeobjects = ( "./input/myMockPacketName", "./input/myPacket/my3/*.txt", "./input/myPacket/in.html", ); $p->SetIncludePath(@includeobjects); print_packet $p; print "In scalar context:\n"; foreach my $path (@{ $p->GetIncludePath }) { print $path, "\n"; }
Output:
Myname
- [foo]
- [bar]
- [baz]
Myname
- [./input/myMockPacketName]
- [./input/myPacket/my3/*.txt]
- [./input/myPacket/in.html]
In scalar context:
./input/myMockPacketName
./input/myPacket/my3/*.txt
./input/myPacket/in.html