How to use the arguments to use() in the package being used

Howdy,=20
The subject says it all believe it or not :)

What I'm trying to figure out is how to pass an argument=20
(pragma I believe is the proper term) to use() and do=20
sonethign in the package based on it.

I've looked at CGI.pm source but can't seem to track it down.=20
(Similar idea as to CGIs -oldstyle_urls -newstyel_urls)
http://search.cpan.org/~lds/CGI.pm-3.04/CGI.pm#PRAGMAS

What I'd like to do is something like this:

# for old time's sake we'll just use our favorite module
use Foo::Monkey qw(:Foo :Bar -doamazingthings);

#then in Foo::Monkey:
package Foo::Monkey;

[mandatory module goodies snipped]

if(?????) { # IE if -doamazingthings was specified in the use statement
	# do some amazing things here
}

[mandatory module goodies snipped]

How do I do that?? Are those arguments stored in a special array =
somewhere??

TIA
DMuey
0
dmuey
2/4/2004 3:52:56 PM
📁 perl.beginners
📃 29388 articles.
⭐ 4 followers.

💬 9 Replies
👁️‍🗨️ 2685 Views

On Feb 4, Dan Muey said:

># for old time's sake we'll just use our favorite module
>use Foo::Monkey qw(:Foo :Bar -doamazingthings);

Well, depending on what you want to do with the arguments, you might want
to use the Exporter module to handle exporting functions, variables, etc.

  perldoc perlmod (look for "Perl Modules")
  perldoc Exporter

When you say

  use Module qw( args go here );

this is translated into

  BEGIN {
    require Module;
    Module->import(qw( args go here ));
  }

Thus, you need an 'import' method in Module::.

  package Module;

  sub import {
    my $class = shift;
    print "You gave me (@_)\n";
  }

It's up to you to do something with @_.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

0
japhy
2/4/2004 4:06:45 PM
>>>>> "Dan" == Dan Muey <dmuey@infiniplex.com> writes:

Dan> What I'm trying to figure out is how to pass an argument 
Dan> (pragma I believe is the proper term) to use() and do 
Dan> sonethign in the package based on it.

$ perldoc -f use

[...]
            Imports some semantics into the current package from the named
            module, generally by aliasing certain subroutine or variable
            names into your package. It is exactly equivalent to

                BEGIN { require Module; import Module LIST; }

            except that Module *must* be a bareword.

Therefore, it's calling a class method in your module called "import".
Create your own import method, instead of inheriting Exporter's import,
and you're all set.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
0
merlyn
2/4/2004 4:30:32 PM
> On Feb 4, Dan Muey said:
>=20
> ># for old time's sake we'll just use our favorite module
> >use Foo::Monkey qw(:Foo :Bar -doamazingthings);
>=20
> Well, depending on what you want to do with the arguments,=20
> you might want to use the Exporter module to handle exporting=20
> functions, variables, etc.
>=20
>   perldoc perlmod (look for "Perl Modules")
>   perldoc Exporter

I do use Exporter actually should've mentioned that sorry, I'll check =
out your perldocs to.
Thanks

>=20
> When you say
>=20
>   use Module qw( args go here );
>=20
> this is translated into
>=20
>   BEGIN {
>     require Module;
>     Module->import(qw( args go here ));
>   }
>=20
> Thus, you need an 'import' method in Module::.
>=20
>   package Module;
>=20
>   sub import {
>     my $class =3D shift;
>     print "You gave me (@_)\n";
>   }
>=20

Perfect! Exactly what I needed.

> It's up to you to do something with @_.
>=20

Thanks Jeff

> --=20
> Jeff "japhy" Pinyan
0
dmuey
2/4/2004 5:31:54 PM
Jeff 'Japhy' Pinyan wrote:
>
> When you say
>
>   use Module qw( args go here );
>
> this is translated into
>
>   BEGIN {
>     require Module;
>     Module->import(qw( args go here ));
>   }

That's an interesting point Jeff. You're right that it's

 Module->import()

instead of

 Module::import()

as 'import' will be inherited in the (common) case that
Module ISA Exporter.

Rob




0
rob
2/4/2004 6:34:13 PM
On Feb 4, Rob Dixon said:

>That's an interesting point Jeff. You're right that it's
>
> Module->import()
>
>instead of
>
> Module::import()
>
>as 'import' will be inherited in the (common) case that
>Module ISA Exporter.

And also, 'Module' is sent as the first arg to import().

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

0
japhy
2/4/2004 8:53:02 PM
Dan Muey wrote:

> Howdy,
> The subject says it all believe it or not :)
>
> What I'm trying to figure out is how to pass an argument
> (pragma I believe is the proper term) to use() and do
> sonethign in the package based on it.
>
> I've looked at CGI.pm source but can't seem to track it down.

But did you perldoc?  Munging source may sometimes be useful, but more often
than not it throws you headlong into implementation details of someone ele's
code without shedding much light on how to use it.  By entering:
perldoc -f use
at the cmmand-line, you should get direct instruction on proper usage.  I just
checked, and the returned text proceeds immediately into a detailed explanation
of the parameters and their use.

Joseph



0
rjnewton
2/5/2004 7:43:33 PM
> Dan Muey wrote:
>=20
> > Howdy,
> > The subject says it all believe it or not :)
> >
> > What I'm trying to figure out is how to pass an argument (pragma I=20
> > believe is the proper term) to use() and do sonethign in=20
> the package=20
> > based on it.
> >
> > I've looked at CGI.pm source but can't seem to track it down.
>=20
> But did you perldoc?  Munging source may sometimes be useful,=20

Shamefully, no :(=20
I usually do but this time I was just backwards!

> but more often than not it throws you headlong into=20
> implementation details of someone ele's code without shedding=20
> much light on how to use it.  By entering: perldoc -f use at=20
> the cmmand-line, you should get direct instruction on proper=20
> usage.  I just checked, and the returned text proceeds=20
> immediately into a detailed explanation of the parameters and=20
> their use.
>=20
> Joseph
>=20
>=20
>=20
>=20
0
dmuey
2/5/2004 8:32:08 PM
Dan Muey wrote:

> >
> > But did you perldoc?  Munging source may sometimes be useful,
>
> Shamefully, no :(
> I usually do but this time I was just backwards!

Cool.  I'm afraid I sent the response before I noticed the signature.  I
know that you know better (;-o)

Joseph

0
rjnewton
2/5/2004 11:10:20 PM
> Dan Muey wrote:
>=20
> > >
> > > But did you perldoc?  Munging source may sometimes be useful,
> >
> > Shamefully, no :(
> > I usually do but this time I was just backwards!
>=20
> Cool.  I'm afraid I sent the response before I noticed the=20
> signature.  I know that you know better (;-o)

Thanks for the vote of confidence,=20
sometimes I just can't help but do things SSA!
:)

>=20
> Joseph
>=20
>=20
0
dmuey
2/5/2004 11:43:48 PM
Reply: