code_junkie

How do I make private functions in a Perl module

Nov 13th, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. sub my_private_function {
  2. ...
  3. };
  4.  
  5. package TestPrivate;
  6.  
  7. require 5.004;
  8.  
  9. use strict;
  10. use warnings;
  11. use Carp;
  12. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  13.  
  14. require Exporter;
  15.  
  16. @ISA = qw(Exporter AutoLoader);
  17.  
  18. our @EXPORT_OK = qw( public_function );
  19. our @EXPORT = qw( );
  20.  
  21. $VERSION = '0.01';
  22.  
  23. sub new {
  24. my ( $class, %args ) = @_;
  25. my $self = {};
  26. bless( $self, $class );
  27. $self->private_function("THIS SHOULD BE PRIVATE");
  28. $self->{public_variable} = "This is public";
  29. return $self;
  30. }
  31.  
  32. sub public_function {
  33. my $self = shift;
  34. my $new_text = shift;
  35. $self->{public_variable} = $new_text;
  36. print "Public Variable: $self->{public_variable}n";
  37. print "Internal Variable: $self->{internal_variable}n";
  38. }
  39.  
  40. sub private_function {
  41. my $self = shift;
  42. my $new_text = shift;
  43. $self->{internal_variable} = $new_text;
  44. }
  45.  
  46. #!/usr/bin/perl
  47. use strict;
  48. use TestPrivate 'public_function';
  49. my $foo = new TestPrivate();
  50. $foo->public_function("Changed public variable");
  51. $foo->private_function("I changed your private variable");
  52. $foo->public_function("Changed public variable again");
  53. $foo->{internal_variable} = "Yep, I changed your private variable again!";
  54. $foo->public_function("Changed public variable the last time");
  55.  
  56. Public Variable: Changed public variable
  57. Internal Variable: THIS SHOULD BE PRIVATE
  58. Public Variable: Changed public variable again
  59. Internal Variable: I changed your private variable
  60. Public Variable: Changed public variable the last time
  61. Internal Variable: Yep, I changed your private variable again!
  62.  
  63. caller eq __PACKAGE__ or die;
  64.  
  65. my $priv_func1 = sub { my $self = shift; say 'func1'; };
  66.  
  67. sub public_sub {
  68. my $self = shift;
  69.  
  70. $priv_func1->( $self );
  71. }
  72.  
  73. my $priv_func1 = sub {
  74. my $self = shift; say 'func1';
  75. };
  76.  
  77. sub public_sub {
  78. my $self = shift;
  79.  
  80. $self->$priv_func1(@_);
  81. }
  82.  
  83. package My;
  84.  
  85. sub new {
  86. return bless { }, shift;
  87. }
  88.  
  89. sub private_func {
  90. my ($s, %args) = @_;
  91. die "Error: Private method called"
  92. unless (caller)[0]->isa( ref($s) );
  93.  
  94. warn "OK: Private method called by " . (caller)[0];
  95. }
  96.  
  97. sub public_func {
  98. my ($s, %args) = @_;
  99.  
  100. $s->private_func();
  101. }
  102.  
  103. package main;
  104.  
  105. my $obj = My->new();
  106.  
  107. # This will succeed:
  108. $obj->public_func( );
  109.  
  110. # This will fail:
  111. $obj->private_func( );
Add Comment
Please, Sign In to add comment