I am an experienced Perl programmer, but I have not done a lot of object programming except for this type of recent perl module: workers.pm
package tom; sub new { my $self = "This is tom"; bless ($self, $class); } # (other subroutines for tom go here) package dick; sub new { my $self = "This is dick"; bless ($self, $class); } # (subroutines for dick go here) package harry; sub new { my $self = "This is harry"; bless ($self, $class); } # (routines for harry go here) 1;
So I use 1 PM file and create 3 different objects that an outside script can use and the package xxx; statement seems to work to separate the different objects.

My new task:

I need to write a single-file perl script that can be copied to a virgin Linux box and when run, it will scan through lots of stuff to sanity-check the system. Things like users/directories/services/etc.

I want to create a "validate_users" object that I can pass various names and options to and it will validate or explain what is wrong.

Then I want to create a "validate_directory" object that will do similar things for different directories.

But - I want these, and the main code, to all live in a single .pl file to make it easy to pass around and use. (Imagine running this on 800+ different linux box's - each with only standard RedHat linux perl installed).

I have tried creating a .pl file and the keyword "package" throws errors. I have tried creating a .pm file, then putting main code (even "print 'Hello World', before the "1;", and this throws different errors.

I am getting the feeling that I am trying to do something "against the rules", but thought I would ask the Monks before giving up and writeing linear code.

What is the correct syntax to create tom/dick/harry objects all in a single .pl file and then use them?


In reply to Can I do single-file, Object oriented perl? by FatDog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.