I
ioneabu
I was trying to put together a simple example of a couple of classes in
a single file in order to experiment with basic perl OO concepts. My
example did not work, so I tried a more basic example right out of the
Perl Cookbook:
#!/usr/bin/perl
use strict;
use warnings;
package Alpha;
$name = "first";
package Omega;
$name = "last";
package main;
print "Alpha is $Alpha::name, Omega is $Omega::name.\n";
I only added the first three lines to make it a proper executable
program. Result of trying to execute:
Global symbol "$name" requires explicit package name at ./test3.pl line
7.
Global symbol "$name" requires explicit package name at ./test3.pl line
10.
Execution of ./test3.pl aborted due to compilation errors.
My class example, which suffers from the same problem is the following:
#!/usr/bin/perl
use warnings;
use strict;
my $a = one->new;
my $b = two->new;
print $a->get_member,"\n";
print $b->get_member,"\n";
#class one
package one;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub set_member
{
$member = shift; #not sure if this is right
}
sub get_member
{
return $member;
}
$member = 10;
#class two
package two;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub set_member
{
$member = shift;
}
sub get_member
{
return $member;
}
$member = 20;
(Preview in Google messed up some of my indenting. If it appears that
I did not indent, it is not how I typed in my code.)
a single file in order to experiment with basic perl OO concepts. My
example did not work, so I tried a more basic example right out of the
Perl Cookbook:
#!/usr/bin/perl
use strict;
use warnings;
package Alpha;
$name = "first";
package Omega;
$name = "last";
package main;
print "Alpha is $Alpha::name, Omega is $Omega::name.\n";
I only added the first three lines to make it a proper executable
program. Result of trying to execute:
Global symbol "$name" requires explicit package name at ./test3.pl line
7.
Global symbol "$name" requires explicit package name at ./test3.pl line
10.
Execution of ./test3.pl aborted due to compilation errors.
My class example, which suffers from the same problem is the following:
#!/usr/bin/perl
use warnings;
use strict;
my $a = one->new;
my $b = two->new;
print $a->get_member,"\n";
print $b->get_member,"\n";
#class one
package one;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub set_member
{
$member = shift; #not sure if this is right
}
sub get_member
{
return $member;
}
$member = 10;
#class two
package two;
sub new
{
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub set_member
{
$member = shift;
}
sub get_member
{
return $member;
}
$member = 20;
(Preview in Google messed up some of my indenting. If it appears that
I did not indent, it is not how I typed in my code.)