Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:44:34+00:00 2026-05-14T02:44:34+00:00

I need some help regarding the arrays in Perl This is the constructor I

  • 0

I need some help regarding the arrays in Perl

This is the constructor I have.

BuildPacket.pm

     sub new {
            my $class = shift;    
            my $Packet = {
                _PacketName => shift,
                _Platform  => shift,
                _Version => shift,
                _IncludePath => [@_],
            };

            bless $Packet, $class;
            return $Packet;
        }

        sub SetPacketName {
            my ( $Packet, $PacketName ) = @_;
            $Packet->{_PacketName} = $PacketName if defined($PacketName);
            return $Packet->{_PacketName};
        }

       sub SetIncludePath {
            my ( $Packet, @IncludePath ) = @_;
            $Packet->{_IncludePath} = \@IncludePath;
        }

         sub GetPacketName {
            my( $Packet ) = @_;
            return $Packet->{_PacketName};
        }

        sub GetIncludePath {
           my( $Packet ) = @_;
           @{ $Packet->{_IncludePath} };
        }

(The code has been modified according to the suggestions from ‘gbacon’, thank you)

I am pushing the relative paths into ‘includeobjects’ array in a dynamic way. The includepaths are being read from an xml file and are pushed into this array.

# PacketInput.pm
if($element eq 'Include')
            {
             while( my( $key, $value ) = each( %attrs ))
                {
                if($key eq 'Path')
                    push(@includeobjects, $value);
                        }
                }

So, the includeobject will be this way:

@includeobjects = (
    "./input/myMockPacketName",
    "./input/myPacket/my3/*.txt",
    "./input/myPacket/in.html",
);

I am using this line for set include path

 $newPacket->SetIncludePath(@includeobjects);

Also in PacketInput.pm, I have

sub CreateStringPath
{
    my $packet = shift;
    print "printing packet in CreateStringPath".$packet."\n";
    my $append = "";
    my @arr = @{$packet->GetIncludePath()};
    foreach my $inc (@arr)
    {
        $append = $append + $inc;
        print "print append :".$append."\n";
    }
}

I have many packets, so I am looping through each packet

# PacketCreation.pl
my @packets = PacketInput::GetPackets();
foreach my $packet (PacketInput::GetPackets())
{
    print "printing packet in loop packet".$packet."\n";
    PacketInput::CreateStringPath($packet);
    $packet->CreateTar($platform, $input);
    $packet->GetValidateOutputFile($platform);
}

The get and set methods work fine for PacketName. But since IncludePath is an array, I could not get it to work, I mean the relative paths are not being printed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:44:34+00:00Added an answer on May 14, 2026 at 2:44 am

    If you enable the strict pragma, the code doesn’t 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.

    Don’t use @ unquoted in your keys because it will confuse the parser. I recommend removing them entirely to avoid confusing human readers of your code.

    You seem to want to pull all the attribute values from the arguments to the constructor, so continue peeling off the scalar values with shift, and then everything left must be the include path.

    I assume that the components of the include path will be simple scalars and not references; if the latter is the case, then you’ll want to make deep copies for safety.

    sub new {
      my $class = shift;
    
      my $Packet = {
        _PacketName  => shift,
        _Platform    => shift,
        _Version     => shift,
        _IncludePath => [ @_ ],
      };
    
      bless $Packet, $class;
    }
    

    Note that there’s no need to store the blessed object in a temporary variable and then immediately return it because of the semantics of Perl subs:

    If no return is found and if the last statement is an expression, its value is returned.

    The methods below will also make use of this feature.

    Given the constructor above, GetIncludePath becomes

    sub GetIncludePath {
      my( $Packet ) = @_;
      my @path = @{ $Packet->{_IncludePath} };
      wantarray ? @path : \@path;
    }
    

    There are a couple of things going on here. First, note that we’re careful to return a copy of the include path rather than a direct reference to the internal array. This way, the user can modify the value returned from GetIncludePath without having to worry about mucking up the packet’s state.

    The wantarray operator allows a sub to determine the context of its call and respond accordingly. In list context, GetIncludePath will return the list of values in the array. Otherwise, it returns a reference to a copy of the array. This way, client code can call it either as in

    foreach my $path (@{ $packet->GetIncludePath }) { ... }
    

    or

    foreach my $path ($packet->GetIncludePath) { ... }
    

    SetIncludePath is then

    sub SetIncludePath {
      my ( $Packet, @IncludePath ) = @_;
      $Packet->{_IncludePath} = \@IncludePath;
    }
    

    Note that you could have used similar code in the constructor rather than removing one parameter at a time with shift.

    You might 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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to all the anonymous features and need some help. I have
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help from the shell-script gurus out there. I have a .txt
Hi I need some help with the following scenario in php. I have a
I'm working with jQuery for the first time and need some help. I have
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help ... I'm a bit (read total) n00b when it comes
I am getting a little confused and need some help please. Take these two
I've gone through most of the example code and I still need some help.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.