Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Perl module
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====''Hello/World.pm''==== <syntaxhighlight lang="perl"> # In Perl there is no special 'class' definition. A namespace is a class. package Hello::World; use strict; use warnings; our $VERSION = "1.00"; =head1 NAME Hello::World - An encapsulation of a common output message =head1 SYNOPSIS use Hello::World; my $hello = Hello::World->new(); $hello->print; =head1 DESCRIPTION This is an object-oriented library which can print the famous "H.W." message. =head2 Methods =head3 new my $hello = Hello::World->new(); my $hello = Hello::World->new( target => $target ); Instantiates an object which holds a greeting message. If a C<$target> is given it is passed to C<< $hello->target >>. =cut # The constructor of an object is called new() by convention. Any # method may construct an object and you can have as many as you like. sub new { my($class, %args) = @_; my $self = bless({}, $class); my $target = exists $args{target} ? $args{target} : "world"; $self->{target} = $target; return $self; } =head3 target my $target = $hello->target; $hello->target($target); Gets and sets the current target of our message. =cut sub target { my $self = shift; if ( @_ ) { my $target = shift; $self->{target} = $target; } return $self->{target}; } =head3 to_string my $greeting = $hello->to_string; Returns the $greeting as a string =cut sub to_string { my $self = shift; return "Hello, $self->{target}!"; } =head3 print $hello->print; Outputs the greeting to STDOUT =cut sub print { my $self = shift; print $self->to_string(), "\n"; } =head1 AUTHOR Joe Hacker <joe@joehacker.org> =cut 1; </syntaxhighlight> ----
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)