zentara has asked for the wisdom of the Perl Monks concerning the following question:

A question just asked at use lib "/..." and sub parms.. and sub parms.. got me thinking about something I was trying the other day. I wanted to pass a $pid to the module through (). It just wouldn't do it. I could pass a constant, but it wouldn't take $$ or $pid. Is this because the use Module is compiled as a BEGIN statement and $pid dosn't exist yet? I know that the module will share the same pid as the script, but this is just an academic question.
package Module; use warnings; use strict; use vars qw(@ISA @EXPORT_OK ); @ISA = qw(Exporter); # inherit the import() from Exporter.pm @EXPORT_OK = qw(); # list of things to export if asked to sub import(\@_) { print "You gave me (@_ )\n"; print "$_[0] $_[1]\n"; } 1;

Test code

#!/usr/bin/perl BEGIN { unshift(@INC,'.')}; $|=1; my $pid = $$; use Module($pid); # I can pass a constant here, but no $pid print "ztest->$$\n"; while(1){ print 1; sleep 1; }

I'm not really a human, but I play one on earth. flash japh

edited: Tue Mar 16 16:22:44 2004 by jeffa - removed HTML link and replaced with In-House [id://] tag

Replies are listed 'Best First'.
Re: passing a $var thru a "use module($var)"
by chip (Curate) on Mar 16, 2004 at 15:11 UTC
    Yes, the implicit BEGIN{} around use is killing you. But it's easy enough to kill it first:

    my $pid; BEGIN { $pid = $$ } use Module $pid; # ta da

    As a separate matter, you should beware about a numeric first argument to use because Perl may interpret it as a minimum required version for the given module.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      As a separate matter, you should beware about a numeric first argument to use because Perl may interpret it as a minimum required version for the given module.
      Not exactly because use isn't exactly a function (use(CGI=>3);).
      $$ perl use CGI 3; # what you're talking about use CGI ( 66 ); # a numeric literal, 1st argument, statement succeeds use CGI 66; # dies as should CGI version 66 required--this is only version 3.04 at - line 3. BEGIN failed--compilation aborted at - line 3.
Re: passing a $var thru a "use module($var)"
by jeffa (Bishop) on Mar 16, 2004 at 15:22 UTC

    Glad chip answered the question ... but why are you doing this? Sure it is neat, but are you planning on using this in production code? Or are you just experimenting with import/export? Personally, while i do find exporting to be handy, i think that importing is a Bad Idea™ Why? Because you can do the same thing in a cleaner fashion with OO.

    my $foo = Foo->new({pid => $$}); print $foo->pid; package Foo; use Class::MethodMaker new_hash_init => 'new', get_set => [ qw /foo bar pid/ ] ;
    Now, what's going on behind the scene with that code is another story. But i don't have to maintain that, now do i? ;)

    But thanks for asking ... i learned a little something today because of it.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Object orientation is a tool. It is not the tool. There are many things that need not be OO, and for which OO would actually be an unwelcome complication.

      /me imagines File::Basename->new and shudders

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        Woah ... i never said that OO was the tool ... those are your words, not mine. Please do not take my node out of context.

        Secondly, i said that i didn't like Import, not Export ... where in File::Basename is importing required to use it procedurally? It isn't. Only Export is needed for that.

        Maybe i should clarify ... only Export is needed by the client.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
        /me imagines File::Basename->new and shudders
        That's too late for OO. I could only ever imagine basename as a method of a File(Name)? object. If there existed such a class, there'd be no need for File::Basename (FileName->new("/foo/bar/baz")->basename() eq FileName->basename("/foo/bar/baz")).

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

Re: passing a $var thru a "use module($var)"
by zentara (Cardinal) on Mar 16, 2004 at 15:42 UTC
    Thanks Jeffa, for pointing that out. I'm finally starting to realize the benefits of modules and objects(after 2 years of functional hacking :-) ). So I was just experimenting with setting up simple modules. But your posted code makes me think about taking the next step forward toward OO-modules. By the way, what I was doing was trying to convert my code snippet at linux memory leak monitor linux memory leak monitor to a module.

    I found it easy to make a non-OO module with:

    use MeMmonitor; &MeMmonitor::init($$);
    or
    use MeMmonitor1; my $m = MeMmonitor1->new($$);

    but my idea was just to use

    use MeMmonitor;
    and have that do it all with no other statements. Well I started to play with passing the $pid to the module, then realized that both module and script shared the same pid, so it just became an academic question. I can't sleep at night when I have unresolved Perl questions :-) .

    I'm not really a human, but I play one on earth. flash japh
Re: passing a $var thru a "use module($var)"
by zentara (Cardinal) on Mar 16, 2004 at 15:17 UTC
    Thanks....it's so obvious I should have thought of it. I love Perl. :-)

    I'm not really a human, but I play one on earth. flash japh