Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Creating an object of class main, useful?

by davido (Archbishop)
on Jul 17, 2004 at 04:08 UTC ( #375189=perlquestion: print w/ replies, xml ) Need Help??

Today I was thinking about a not-so-recent post from Abigail-II where he created a tied scalar, tied to package main. I remembered thinking, "Oh, that's nifty." It hadn't really occurred to me (though the ability to do so does make perfect sense) that package main can be used as a class of which objects can be created.

But aside from this sort of quick-and-dirty tieing of a variable to an ad-hoc class in package main, is there any other reasonable use of the fact that main can, itself, be a class?

Consider the following contrived and dumb code:

package main; use strict; use warnings; sub new { my $class = shift; my $self = { Class => $class }; bless $self, $class; } sub myprint { my $self = shift; print "This is a method of class $self->{Class}.\n"; } my $obj = main->new(); $obj->myprint(); __OUTPUT__ This is a method of class main.

Ok, now I've created a more traditional object (more traditional than a tied variable), and have created it as class of main. And it has its own blessed hash for portable and compartmentalized namespace. But of what use is this?

My first thought when searching for a use of such a construct is that with this construct it becomes trivial to write script setup 'stuff' into the constructor sub, and cleanup stuff into the deconstructor sub. Why do it this way? ...not sure, but I had a brief and vague notion that in scripts that remain resident such as mod_perl it might somehow be useful for ensuring that each run has clean workspace.

But that's what lexical scoping does automatically anyway, and as for constructors and deconstructors, even without OO there exist BEGIN{ ... } and END{ ... } blocks.

Another thought I had is in the direction of the new() constructor spawning threads, but I don't know enough about threads to know if this is a useful way of handling them.

Of course another use of using main as a class might be for operator overloading, like tie, on a quick-and-dirty basis. But this is similar to the tie usage in a way, and feels unrewarding. Surely there is (or definitively is not) a good reason to create an object based on class main. Can anyone give a good usage of such a construct? Or is it just a feature that doesn't really have a good use?


Comment on Creating an object of class main, useful?
Download Code
Re: Creating an object of class main, useful?
by Zaxo (Archbishop) on Jul 17, 2004 at 04:55 UTC

    I suspect that Abigail was emphasizing the arbitrary nature of perl namespaces. The best use of main as a class is to avoid being in a different class. It seems to me to be mostly a stunt, but I too would be edified to see a useful counterexample.

    After Compline,

      It took some careful thought on what criteria to feed Super Search, but I re-found the post by Abigail-II (from a few months ago) that popped back into my mind out of the blue today, leading me to post this thread.

      Here's the link: Re: s/\w/random character/g. In his post, he ties a hash to package main as a cheap / quick and dirty way of implementing a cool solution.

      Of course the tied hash isn't the point to my post, but the fact that he tied it to main lead me to realize that there's no reason main can't be a class just like any other package. ...and of course that leaves the original question in tact: why would I want to do that? *grin*


package main
by rinceWind (Monsignor) on Jul 17, 2004 at 08:45 UTC
    As an aside, you don't actually need to say package main;.

    However, we might start seeing this appearing in perl 5 scripts in future, see Ensuring forward compatibility.

    I'm Not Just Another Perl Hacker

      Not only for forward compatibility, but also for clarity. I know that you can look at something you wrote 3 days ago and have no idea what it does! If we're doing something weird like this, it's probably the best idea to make this disgusting hack as clear as possible to the reader. =)

      mhoward - at -

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://375189]
Approved by Limbic~Region
Front-paged by meredith
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-26 01:45 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (163 votes), past polls