harsha.reddy has asked for the wisdom of the Perl Monks concerning the following question:

My "test.pl" script looks like this
#!/usr/bin/perl package test; use strict; use warnings; use Switch; sub new { my $class = shift; my $self = { host => "127.0.0.1", server => "localhost", username => "user1", password => "passwd", name => undef, operation => undef }; bless $self, 'Person'; return $self; } sub echo { my $self = shift; print $self->host; } 1; use test; $eco = test->new(); $eco->echo();


perl test.pl - yields the error:
Can't locate test.pm in @INC

Is it that a Perl class has to be always inside a .pm file?

Replies are listed 'Best First'.
Re: A Class inside a Perl script?
by Corion (Patriarch) on Jun 02, 2009 at 08:45 UTC

    The use keyword wants to load the file test.pm. But you don't need the use statement at all, your program will work, because the test package has been initialized already in your script.

    You should add package main; in front of your "main program" code, to avoid the confusion of namespaces.

      I revamped the code to look like:
      #!/usr/bin/perl package test; use strict; use warnings; use Switch; sub new { my $class = shift; my $self = { host => "127.0.0.1", server => "localhost", username => "user1", password => "passwd", name => undef, operation => undef }; bless $self, 'Person'; return $self; } sub echo { my $self = shift; print $self->host; } 1; package main; $eco = test->new(); $eco->echo();

      No luck

      Can't locate object method "echo" via package "Person" at ./test line 34.

      Is the error that I am getting.
      Why is it taking the name of the package as "Person" instead of "test"?
      My perl is: perl, v5.8.8 built for i386-linux-thread-multi
      on RHEL5

        bless tells you why your program is looking for a package named Person. And don't, ever, use Switch. It is a bad idea to even mention in your source code, because it has nasty side-effects. If you use Perl 5.10, you can use the new given keyword instead, otherwise just use a chain of if..elsif..else or a dispatch table.

        Why is it taking the name of the package as "Person" instead of "test"?

        You are given the class name for bless as "Person" thats why .

        bless $self, 'Person';
        Vinoth,G
        Direct answers were already told by other monks, if you need insight on OO perl, see perlmod, perltoot, perlmodlib
        and this Object Oriented Perl can also guide you.

        Vivek
        -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: A Class inside a Perl script?
by arc_of_descent (Hermit) on Jun 02, 2009 at 09:24 UTC

    You're package is named test, but you are blessing into Person.

    You should use the following constructor in your packages, which takes care of inheritance too.

    sub new { my $proto = shift; my $class = ref $proto || $proto; my $self = {}; # ... # ... bless $self, $class; return $self; }

    --
    Rohan
      my $class = ref $proto || $proto; ... bless $self, $class;

      Don't do that. It generates a function/method that implies both constructor and clone semantics. new should be a class function that generates a new initialised object. An explicit copy or clone method (not class function) should be used to generate a cloned instance of an existing object. new() function: incorrect? and its replies discuss the topic at some length.


      True laziness is hard work
Re: A Class inside a Perl script?
by harsha.reddy (Acolyte) on Jun 02, 2009 at 10:28 UTC
    mommy :'( my quality of attention to detail is fading...

    It works!!
    Thanks you all :)