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 4238340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:53:31+00:00 2026-05-21T02:53:31+00:00

I have a Perl class file (package): person.pl package person; sub create { my

  • 0

I have a Perl class file (package): person.pl

package person;

sub create {
  my $this = {  
    name => undef,
    email => undef
  }

  bless $this;
  return $this;
}  

1;

and i need to use this class in another file: test.pl

(note that person.pl and test.pl are in the same directory)

require "person.pl";

$john_doe = person::create();
$john_doe->{name} = "John Doe";
$john_doe->{email} = "johndoe@example.com";

but it didn’t come to success.

I’m using XAMPP to run both PHP & Perl.

I think it doesn’t seem right to use “require” to get
the code of the class ‘person’, but i don’t know
how to resolve this problem. Please help…

  • 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-21T02:53:31+00:00Added an answer on May 21, 2026 at 2:53 am

    First of all, you should name the file person.pm (for Perl Module). Then you can load it with the use function:

    use person;
    

    If the directory where person.pm is located is not in @INC, you can use the lib pragma to add it:

    use lib 'c:/some_path_to_source_dir';
    use person;
    

    Secondly, Perl doesn’t have special syntax for constructors. You named your constructor create (which is ok, but non-standard), but then tried to call person::new, which doesn’t exist.

    If you’re going to be doing object-oriented programming in Perl, you should really look at Moose. Among other things, it creates the constructor for you.

    If you don’t want to use Moose, here are some other improvements you can make:

    package person;
    
    use strict;   # These 2 lines will help catch a **lot** of mistakes
    use warnings; # you might make.  Always use them.
    
    sub new {            # Use the common name
      my $class = shift; # To allow subclassing
    
      my $this = {  
        name => undef;
        email => undef;
      }
    
      bless $this, $class; # To allow subclassing
      return $this;
    }
    

    Then call the constructor as a class method:

    use strict;   # Use strict and warnings in your main program too!
    use warnings;
    use person;
    
    my $john_doe = person->new();
    

    Note: It’s more common in Perl to use $self rather than $this, but it doesn’t actually matter. Perl’s built-in object system is very minimal and places few restrictions on how you use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class like this: package MyClass; use vars qw/$ME list of
I have Perl script and need to determine the full path and filename of
I have this Perl script with many defined constants of configuration files. For example:
Let's say I have a perl module file and I want to include and
I have a Moose class that i would like to store using Apache::Session::File. However,
I have installed the Net_Geo pear package on my PHP Server. This is the
This seems to have most started since I upgrade my DBIx::Class and I can't
I have a file that I need to reformat and remove extra blank lines.
I have Perl code which relies on Term::ReadKey to get the terminal width. My
I have Perl on Mac, Windows and Ubuntu. How can I tell from within

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.