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

  • Home
  • SEARCH
  • 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 112869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:36:46+00:00 2026-05-11T02:36:46+00:00

I am working on a little Perl module and for some reason I had

  • 0

I am working on a little Perl module and for some reason I had the test driver script that was using my new module call one of the functions that I thought would be private, and it was successful. I was surprised, so I started searching google and I couldn’t really find any documentation on how to make private functions in Perl modules…

I saw one place that said to put a semicolon after the closing brace of your ‘private’ function, like this:

sub my_private_function { ... };  

I tried that, but my driver script could still access the function I wanted to be private.

I’ll make up something that will be a shorter example, but here’s what I’m after:

Module TestPrivate.pm:

package TestPrivate;  require 5.004;  use strict; use warnings; use Carp; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);  require Exporter;  @ISA = qw(Exporter AutoLoader);  our @EXPORT_OK = qw( public_function ); our @EXPORT    = qw( );  $VERSION = '0.01';  sub new {     my ( $class, %args ) = @_;     my $self = {};     bless( $self, $class );     $self->private_function('THIS SHOULD BE PRIVATE');     $self->{public_variable} = 'This is public';     return $self; }  sub public_function {     my $self     = shift;     my $new_text = shift;     $self->{public_variable} = $new_text;     print 'Public Variable: $self->{public_variable}\n';     print 'Internal Variable: $self->{internal_variable}\n'; }  sub private_function {     my $self     = shift;     my $new_text = shift;     $self->{internal_variable} = $new_text; } 

Driver: TestPrivateDriver.pl

#!/usr/bin/perl use strict; use TestPrivate 'public_function'; my $foo = new TestPrivate(); $foo->public_function('Changed public variable'); $foo->private_function('I changed your private variable'); $foo->public_function('Changed public variable again'); $foo->{internal_variable} = 'Yep, I changed your private variable again!'; $foo->public_function('Changed public variable the last time'); 

Driver output:

Public Variable: Changed public variable Internal Variable: THIS SHOULD BE PRIVATE Public Variable: Changed public variable again Internal Variable: I changed your private variable Public Variable: Changed public variable the last time Internal Variable: Yep, I changed your private variable again! 

So I added a semicolon after the last closing brace in the module, but the output is still the same. The only thing I really found was to add this line as the first line to my private_function:

caller eq __PACKAGE__ or die; 

But that seems pretty hacky. I don’t have a lot of experience writing Perl modules, so maybe I am setting my module up incorrectly? Is it possible to have private functions and variables in perl modules?

Thanks for helping me learn!

  • 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. 2026-05-11T02:36:46+00:00Added an answer on May 11, 2026 at 2:36 am

    From perldoc perltoot (about a quarter way through the document):

    Perl doesn’t impose restrictions on who gets to use which methods. The public-versus-private distinction is by convention, not syntax. (Well, unless you use the Alias module described below in ‘Data Members as Variables’.) Occasionally you’ll see method names beginning or ending with an underscore or two. This marking is a convention indicating that the methods are private to that class alone and sometimes to its closest acquaintances, its immediate subclasses. But this distinction is not enforced by Perl itself. It’s up to the programmer to behave.

    Therefore, I recommend you put an underscore or two at the beginning of your ‘private’ methods to help dissuade usage.

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

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer If Not pictureBox.Image is Nothing Then //do stuff here Else… May 11, 2026 at 1:35 pm
  • added an answer You will want to generalize this function for the delimiter… May 11, 2026 at 1:35 pm
  • added an answer I tried the other answers left here and ran into… May 11, 2026 at 1:35 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.