Passing Variable from .pl to .pl

Passing Variable from .pl to .pl

am 22.01.2007 14:55:59 von Ben Eagle

This is a multi-part message in MIME format.

--===============2109755688==
Content-Class: urn:content-classes:message
Content-Type: multipart/alternative;
boundary="----_=_NextPart_001_01C73E2D.0B7E4AE8"

This is a multi-part message in MIME format.

------_=_NextPart_001_01C73E2D.0B7E4AE8
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I am trying to break up a script for easy configuration,=20

=20

So I want to put all my global variables in a file call config.pl

=20

And on BuildReport.pl I add

Require "config.pl";

=20

In strict mode this doesn't work I get error saying I never declared my
variables.

=20

I use our($this, $that);

=20

And still nothing

=20

Any suggestions??

=20

Ben Eagle


------_=_NextPart_001_01C73E2D.0B7E4AE8
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable




charset=3Dus-ascii">












style=3D'font-size:10.0pt;
font-family:Arial'>I am trying to break up a script for easy =
configuration,



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>So I want to put all my global variables in a file =
call
config.pl



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>And on BuildReport.pl I add



style=3D'font-size:10.0pt;
font-family:Arial'>Require “config.pl”;



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>In strict mode this doesn’t work I get error =
saying I
never declared my variables.



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>I use our($this, $that);



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>And still nothing



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>Any suggestions??



style=3D'font-size:10.0pt;
font-family:Arial'> 



style=3D'font-size:10.0pt;
font-family:Arial'>Ben Eagle









------_=_NextPart_001_01C73E2D.0B7E4AE8--

--===============2109755688==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============2109755688==--

RE: Passing Variable from .pl to .pl

am 22.01.2007 16:12:23 von Brian Raven

From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Ben
Eagle
Sent: 22 January 2007 13:56
To: activeperl@listserv.ActiveState.com
Subject: Passing Variable from .pl to .pl

> I am trying to break up a script for easy configuration,
>
> So I want to put all my global variables in a file call config.pl

my %advice = ("global variables?" => "Avoid as much as possible.");

>
> And on BuildReport.pl I add
> Require "config.pl";
>
> In strict mode this doesn't work I get error saying I never declared
my variables.
>
> I use our($this, $that);
>
> And still nothing

First, require is executed at run time, i.e. after the variables that
are presumably declared in config.pl have been used. Second, lexical
variables (i.e. 'my') are only visible in the scope in which they are
declared.

>
> Any suggestions??

If you want to do it that way you will need to predeclare your global
variables with 'our' in BuildReport.pl before the require. Then,
assuming no package statements in BuildReport.pl, you could initialise
variables in config.pl like "$::var = ..." instead of "$var = ...".

However this is a pretty fragile solution. There are any number of
config modules on CPAN (e.g.
http://search.cpan.org/search?query=config&mode=all), many of which will
also be available via ppm. Picking one that matches your requirements
would probably be a better solution.

HTH

--
Brian Raven

_________________________________________

The information contained in this e-mail is confidential and solely
for the intended addressee(s). Unauthorised reproduction, disclosure,
modification, and/or distribution of this email may be unlawful. If you
have received this email in error, please notify the sender immediately
and delete it from your system. The views expressed in this message
do not necessarily reflect those of Atos Euronext Market Solutions.
_________________________________________



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Passing Variable from .pl to .pl

am 23.01.2007 05:52:22 von Foo JH

As Brian mentioned, global variables are generally highly discouraged, =

as it usually leads to sloppy coding that is hard to refactor.

If you want to break up your script for easy maintenance, you should =

consider using:
1. libraries, or in perl teminology 'packages'
2. OOP, to abstract the logic into various specialised objects


Ben Eagle wrote:
>
> I am trying to break up a script for easy configuration,
>
> So I want to put all my global variables in a file call config.pl
>
> And on BuildReport.pl I add
>
> Require =93config.pl=94;
>
> In strict mode this doesn=92t work I get error saying I never declared =

> my variables.
>
> I use our($this, $that);
>
> And still nothing
>
> Any suggestions??
>
> Ben Eagle
>
> ------------------------------------------------------------ ------------
>
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Passing Variable from .pl to .pl

am 23.01.2007 15:01:45 von Ben Eagle

Thanks, I think I will go the route of making a module "Config.pm"

Although it seems there are many ways to do that, which one is the best
or "proper way" of making one, I don't want to start coding any more
sloppy then I already do.

I have tried to make a config.pm module but still have troubles getting
it to work, I am not sure why it won't find the module, use lib "lib/";
use Dell::Config doesn't get me to my lib directory where I have the
folder "Dell" and Config.pm inside of that.

Not exactly sure what I am doing wrong but strict seems to not like it

Config.pm
___________
use strict;
package Dell::Config;

our($Template, $ChartImageName, $bubbleName, $imageType);


$Template = qq(Dell_Deployment_Optimization_Report.pdf);
$ChartImageName = "DellDOChart";
$bubbleName = "Bubble";
$imageType =".png";

1; #Return True


BuildReport.pl
______________

use strict;
use warnings;
use lib "lib/";
use Dell::Config


code here....


any ideas what I am doing wrong?



-----Original Message-----
From: Bill Luebkert [mailto:dbecoll@roadrunner.com]
Sent: Monday, January 22, 2007 5:39 PM
To: Ben Eagle
Subject: Re: Passing Variable from .pl to .pl

Ben Eagle wrote:
> I am trying to break up a script for easy configuration,
>
>
>
> So I want to put all my global variables in a file call config.pl
>
>
>
> And on BuildReport.pl I add
>
> Require "config.pl";
>
>
>
> In strict mode this doesn't work I get error saying I never declared
my
> variables.
>
>
>
> I use our($this, $that);

You could make it a .pm file and do something like:

use strict;
use warnings;
use myconfig;

print "x=$x\n";
print "y=$y\n";
print "z=$z\n";

__END__

myconfig.pm:

use strict;
use warnings;
use vars qw($x $y $z);

$x = 1;
$y = 2;
$z = 3;

__END__

Or you could use a do in a begin block:

use strict;
use warnings;

BEGIN { do 'config.pl'; }

print "x=$x\n";
print "y=$y\n";
print "z=$z\n";

__END__

Where config.pl is the same as config.pm above.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Passing Variable from .pl to .pl

am 23.01.2007 16:26:17 von Bill Luebkert

Ben Eagle wrote:
> Thanks, I think I will go the route of making a module "Config.pm"
>
> Although it seems there are many ways to do that, which one is the best
> or "proper way" of making one, I don't want to start coding any more
> sloppy then I already do.
>
> I have tried to make a config.pm module but still have troubles getting
> it to work, I am not sure why it won't find the module, use lib "lib/";
> use Dell::Config doesn't get me to my lib directory where I have the
> folder "Dell" and Config.pm inside of that.

What are the full paths to these directories ?
Unless your Dell dir is under the installed .../perl/site/lib or '.'
dir, you'll need a full path to it.

> Not exactly sure what I am doing wrong but strict seems to not like it
>
> Config.pm
> ___________
> use strict;
> package Dell::Config;
>
> our($Template, $ChartImageName, $bubbleName, $imageType);
>
>
> $Template = qq(Dell_Deployment_Optimization_Report.pdf);
> $ChartImageName = "DellDOChart";
> $bubbleName = "Bubble";
> $imageType =".png";
>
> 1; #Return True
>
>
> BuildReport.pl
> ______________
>
> use strict;
> use warnings;
> use lib "lib/";

The above line is wanted/needed.

> use Dell::Config
>
>
> code here....
>
>
> any ideas what I am doing wrong?

Are you getting an error of some sort ? What indication do you
have that something is wrong ?

Assuming you have a dir Dell under the current dir or under the
site/lib dir, this should work :

use strict;
use warnings;
use Dell::Config;
print "template=$Dell::Config::Template\n";

One of the problems with doing a config file like this is
handling the scoping issues. You could also do something like:

Dell/Config.pm:

use strict;
package main; # put the vrbls under main

use vars qw($Template $ChartImageName $bubbleName $imageType);

$Template = qq(Dell_Deployment_Optimization_Report.pdf);
$ChartImageName = "DellDOChart";
$bubbleName = "Bubble";
$imageType =".png";

1;

__END__

use strict;
use warnings;
use Dell::Config;
print "template=$Template\n";

__END__
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Passing Variable from .pl to .pl

am 24.01.2007 06:05:28 von Foo JH

What exactly is the error you are getting?

To make your life simple, create the Dell folder in the same directory
as BuildReport.pl, and Config.pm in the Dell folder. Then you won't need
the 'use lib' bit.

To use the Config package variable, you need to address them like this:
print $Dell::Config::Template;


Ben Eagle wrote:
> Thanks, I think I will go the route of making a module "Config.pm"
>
> Although it seems there are many ways to do that, which one is the best
> or "proper way" of making one, I don't want to start coding any more
> sloppy then I already do.
>
> I have tried to make a config.pm module but still have troubles getting
> it to work, I am not sure why it won't find the module, use lib "lib/";
> use Dell::Config doesn't get me to my lib directory where I have the
> folder "Dell" and Config.pm inside of that.
>
> Not exactly sure what I am doing wrong but strict seems to not like it
>
> Config.pm
> ___________
> use strict;
> package Dell::Config;
>
> our($Template, $ChartImageName, $bubbleName, $imageType);
>
>
> $Template = qq(Dell_Deployment_Optimization_Report.pdf);
> $ChartImageName = "DellDOChart";
> $bubbleName = "Bubble";
> $imageType =".png";
>
> 1; #Return True
>
>
> BuildReport.pl
> ______________
>
> use strict;
> use warnings;
> use lib "lib/";
> use Dell::Config
>
>
> code here....
>
>
> any ideas what I am doing wrong?
>
>
>
> -----Original Message-----
> From: Bill Luebkert [mailto:dbecoll@roadrunner.com]
> Sent: Monday, January 22, 2007 5:39 PM
> To: Ben Eagle
> Subject: Re: Passing Variable from .pl to .pl
>
> Ben Eagle wrote:
>
>> I am trying to break up a script for easy configuration,
>>
>>
>>
>> So I want to put all my global variables in a file call config.pl
>>
>>
>>
>> And on BuildReport.pl I add
>>
>> Require "config.pl";
>>
>>
>>
>> In strict mode this doesn't work I get error saying I never declared
>>
> my
>
>> variables.
>>
>>
>>
>> I use our($this, $that);
>>
>
> You could make it a .pm file and do something like:
>
> use strict;
> use warnings;
> use myconfig;
>
> print "x=$x\n";
> print "y=$y\n";
> print "z=$z\n";
>
> __END__
>
> myconfig.pm:
>
> use strict;
> use warnings;
> use vars qw($x $y $z);
>
> $x = 1;
> $y = 2;
> $z = 3;
>
> __END__
>
> Or you could use a do in a begin block:
>
> use strict;
> use warnings;
>
> BEGIN { do 'config.pl'; }
>
> print "x=$x\n";
> print "y=$y\n";
> print "z=$z\n";
>
> __END__
>
> Where config.pl is the same as config.pm above.
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Passing Variable from .pl to .pl

am 24.01.2007 11:11:01 von Brian Raven

Ben Eagle <> wrote:
> Thanks, I think I will go the route of making a module "Config.pm"
>
> Although it seems there are many ways to do that, which one is the
> best or "proper way" of making one, I don't want to start coding any
> more sloppy then I already do.
>
> I have tried to make a config.pm module but still have troubles
> getting it to work, I am not sure why it won't find the module, use
> lib "lib/"; use Dell::Config doesn't get me to my lib directory where
> I have the folder "Dell" and Config.pm inside of that.
>
> Not exactly sure what I am doing wrong but strict seems to not like it
>
> Config.pm
> ___________
> use strict;
> package Dell::Config;
>
> our($Template, $ChartImageName, $bubbleName, $imageType);
>
>
> $Template = qq(Dell_Deployment_Optimization_Report.pdf);
> $ChartImageName = "DellDOChart";
> $bubbleName = "Bubble";
> $imageType =".png";
>
> 1; #Return True
>
>
> BuildReport.pl
> ______________
>
> use strict;
> use warnings;
> use lib "lib/";
> use Dell::Config
>
>
> code here....
>
>
> any ideas what I am doing wrong?

The first thing you are doing wrong is not giving us enough information.
A small, self contained example (SSCE), that we can cut and paste and
run, along with some idea of what is happening and how that differs from
what you expected/wanted, is invaluable in helping us to help you.

I can see a couple of potential problems with what you have posted. The
issue of naming and locating your module has been addressed by others.
In addition, the variables in your module will not be visible to the
code that "uses" it, unless you fully qualify them, e.g.
$Dell::Config::Template. Another way to make them visible is to export
them, see 'perldoc Exporter'. For example, Dell/Config.pm could look
like this:

use strict;
use warnings;

package Dell::Config;

use base "Exporter";
our @EXPORT = qw{$Template $ChartImageName $bubbleName $imageType};

our $Template = qq(Dell_Deployment_Optimization_Report.pdf);
our $ChartImageName = "DellDOChart";
our $bubbleName = "Bubble";
our $imageType =".png";

1;

HTH

--
Brian Raven

_________________________________________

The information contained in this e-mail is confidential and solely
for the intended addressee(s). Unauthorised reproduction, disclosure,
modification, and/or distribution of this email may be unlawful. If you
have received this email in error, please notify the sender immediately
and delete it from your system. The views expressed in this message
do not necessarily reflect those of Atos Euronext Market Solutions.
_________________________________________



=================================
Atos Euronext Market Solutions Disclaimer
=================================
The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions.

L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions.

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Passing Variable from .pl to .pl

am 24.01.2007 12:16:51 von Foo JH

Brian Raven wrote:
> In addition, the variables in your module will not be visible to the
> code that "uses" it, unless you fully qualify them, e.g.
> $Dell::Config::Template. Another way to make them visible is to export
> them, see 'perldoc Exporter'. For example, Dell/Config.pm could look
> like this:
Just want to add that exporting package variables is generally
discouraged, unless there is a good reason for it. Even then, try to
throw in some concept of scope, like the APACHE_OK

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs