For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > Adding the use statements to a different file









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 Adding the use statements to a different file
vmalik@engmail.uwaterloo.ca

2005-11-28, 6:57 pm

Hi,

I have to write several perl scripts with a number of modules included.
Therefore, I have to type many "use" statements at the top of each script. Is
there a way to dump all these "use" statements in a different file and then just
include that file everytime I have to include these "use" statements?

I know that writing a module is a great way to do something like that, but
doesn't a module export variables and subroutines? How can I make a module
export "use" statements?

Thanks.

Vishal


----------------------------------------
This mail sent through www.mywaterloo.ca
Shawn Corey

2005-11-28, 6:57 pm

vmalik@engmail.uwaterloo.ca wrote:
> Hi,
>
> I have to write several perl scripts with a number of modules included.
> Therefore, I have to type many "use" statements at the top of each script. Is
> there a way to dump all these "use" statements in a different file and then just
> include that file everytime I have to include these "use" statements?
>
> I know that writing a module is a great way to do something like that, but
> doesn't a module export variables and subroutines? How can I make a module
> export "use" statements?


Perl has three distinct ways of separating content: modules, packages,
and objects. Modules do not export variables and subroutines; packages
do. Or to be specific, packages can export variables and subroutines to
the main package.

You can use a module as a straight-forward include. Simply list the
material you want and it will be including in the package where the
'use' command is.

In the main file:

use MyUseList;

Create a file called MyUseList.pm and add your list.

#!/usr/bin/perl

use strict;
use warnings;

# Your list goes here

1; # Modules must return a non-false value
__END__


--

Just my 0.00000002 million dollars worth,
--- Shawn

"Probability is now one. Any problems that are left are your own."
SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
vmalik@engmail.uwaterloo.ca

2005-11-28, 6:57 pm

That worked Shawn. Thanks

Quoting Shawn Corey <shawn.corey@sympatico.ca>:

> vmalik@engmail.uwaterloo.ca wrote:
> Is
> just
>
> Perl has three distinct ways of separating content: modules, packages,
> and objects. Modules do not export variables and subroutines; packages
> do. Or to be specific, packages can export variables and subroutines to
> the main package.
>
> You can use a module as a straight-forward include. Simply list the
> material you want and it will be including in the package where the
> 'use' command is.
>
> In the main file:
>
> use MyUseList;
>
> Create a file called MyUseList.pm and add your list.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> # Your list goes here
>
> 1; # Modules must return a non-false value
> __END__
>
>
> --
>
> Just my 0.00000002 million dollars worth,
> --- Shawn
>
> "Probability is now one. Any problems that are left are your own."
> SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>





----------------------------------------
This mail sent through www.mywaterloo.ca
Sponsored Links







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

Copyright 2008 codecomments.com