Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make private functions in a Perl module?
    primarykey
    data
    text
    <p>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...</p> <p>I saw one place that said to put a semicolon after the closing brace of your "private" function, like this:</p> <pre><code>sub my_private_function { ... }; </code></pre> <p>I tried that, but my driver script could still access the function I wanted to be private.</p> <p>I'll make up something that will be a shorter example, but here's what I'm after:</p> <p><strong>Module TestPrivate.pm:</strong></p> <pre><code>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-&gt;private_function("THIS SHOULD BE PRIVATE"); $self-&gt;{public_variable} = "This is public"; return $self; } sub public_function { my $self = shift; my $new_text = shift; $self-&gt;{public_variable} = $new_text; print "Public Variable: $self-&gt;{public_variable}\n"; print "Internal Variable: $self-&gt;{internal_variable}\n"; } sub private_function { my $self = shift; my $new_text = shift; $self-&gt;{internal_variable} = $new_text; } </code></pre> <p><strong>Driver: TestPrivateDriver.pl</strong></p> <pre><code>#!/usr/bin/perl use strict; use TestPrivate 'public_function'; my $foo = new TestPrivate(); $foo-&gt;public_function("Changed public variable"); $foo-&gt;private_function("I changed your private variable"); $foo-&gt;public_function("Changed public variable again"); $foo-&gt;{internal_variable} = "Yep, I changed your private variable again!"; $foo-&gt;public_function("Changed public variable the last time"); </code></pre> <p><strong>Driver output:</strong></p> <pre><code>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! </code></pre> <p>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:</p> <pre><code>caller eq __PACKAGE__ or die; </code></pre> <p>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?</p> <p>Thanks for helping me learn!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload