For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2008 > Where is $main defined?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Where is $main defined?
Jonathan Mast

2008-01-22, 7:08 pm

I have a perl module that extensively uses a variable named "$main", which
is apparently bound to the script that calls the library.

I can't find where the exact semantics of this automagic variable defined.

thanks,
jhmast

Chas. Owens

2008-01-22, 7:08 pm

On Jan 22, 2008 10:42 AM, Jonathan Mast <jhmast.developer@gmail.com> wrote:
> I have a perl module that extensively uses a variable named "$main", which
> is apparently bound to the script that calls the library.
>
> I can't find where the exact semantics of this automagic variable defined.

snip

There is no variable named $main unless you create it. You may be
mistaking the main package in variables like $main::foo. Also, if the
module is not using the strict pragma then it is not required to
"create" the variable. It will be created on the first reference to
it (whitch is why you should always use the strict pragma).
John W. Krahn

2008-01-22, 7:08 pm

Chas. Owens wrote:
> On Jan 22, 2008 10:42 AM, Jonathan Mast <jhmast.developer@gmail.com> wrote:
> snip
>
> There is no variable named $main unless you create it. You may be
> mistaking the main package in variables like $main::foo.


Or variables like $main'foo.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Jenda Krynicky

2008-01-22, 10:07 pm

From: "John W. Krahn" <krahnj@telus.net>
> Chas. Owens wrote:
>
> Or variables like $main'foo.


I don't think I've ever seen this outside Obfu(scation) or golf.
It's good to know something like that is possible, but please don't
use that. Unless of course you do want to confuse someone ;-)

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Chas. Owens

2008-01-22, 10:07 pm

On Jan 22, 2008 8:25 PM, Jenda Krynicky <Jenda@krynicky.cz> wrote:
snip
>
> I don't think I've ever seen this outside Obfu(scation) or golf.
> It's good to know something like that is possible, but please don't
> use that. Unless of course you do want to confuse someone ;-)

snip

You mean you haven't heard of Damian's Klingon module
Lingua-tlhInganHol-yIghun*? It is currently being checked for
correctness by the Klingon Language Institute**, so it is not
available on CPAN. What follows is an implementation of the Sieve of
Eratosthenes (at least I think it is) using it. There is also
Acme::Don't*** and D'oh****.

use Lingua::tlhInganHol::yIghun
# "yImugh",
# "yIQIj",
# "tera'nganHol",
;

###### mI'wa'DIchvaD 'eratoSHeneS HeghqaD #####

<mI' tIn law': > yIghItlh!
mI'tInwIj laDDaqvo'Hal yIlaD nob!
mI'wa'DIchmeywIj cha' mI'tIn chen nob!
{
mI'wa'DIchmeyvaD { 'oH gheD chuv! } mI'wa'DIchmey tIwIv yInob!
gheD <<\n>> ghItlh!
} gheDvaD mI'wa'DIchmeyvaD yInIH yInob teHtaHvIS!

* http://www.csse.monash.edu.au/~dami...l-yIghun.tar.gz
** http://www.kli.org/
*** http://search.cpan.org/dist/Acme-Don-t/t.pm
**** http://search.cpan.org/dist/D-oh/lib/D/oh.pm
John W. Krahn

2008-01-22, 10:07 pm

Jenda Krynicky wrote:
> From: "John W. Krahn" <krahnj@telus.net>
>
> I don't think I've ever seen this outside Obfu(scation) or golf.
> It's good to know something like that is possible, but please don't
> use that. Unless of course you do want to confuse someone ;-)


So I take it that you have never used Per4? :-)


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
John W. Krahn

2008-01-23, 7:06 pm

Jonathan Mast wrote:
> Yeah it is apparently the main package.
>
> So this is the implicit package all scripts belong to or what?


Yes.

> It is used extensively in a perl module I'm having to deal with.
>
> Still, does anyone know where this "feature" is documented?
>
> I searched the web for "$main::" but didn't find anything useful.


perldoc perlmod



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com