get variables from another file

R

Rocky

Hi. I wrote a script to monitor a tomcat error log.
http://www.bobotheclown.org/scripts/jakartalog

My boss wants to put all the recipients and hostnames in another file so
that we can change it per site.
here is what I tried
#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;
require ('hello.pl');
my $hostname = $ENV{HOSTNAME};
my $jakartahome = $ENV{CATALINA_HOME};
my $logdir = "$jakartahome/logs";

hello.pl looks like this:

my $recipient1 = 'EMAIL1 HERE';
my $recipient2 = 'EMAIL2 HERE';
my $recipient3 = 'EMAIL3 HERE';
my $recipient4 = 'EMAIL4 HERE';
my $recipient5 = 'EMAIL5 HERE';
my $mailfrom = 'MAILFROM HERE';
my $fromaddress = "ISEETHEFROM HERE";

The problem is that the main script does not see the recipients from the
file. I am sure this is because of the way I have it scoped, but I know
no other way. Any pointers will be appreciated.
Rocky
 
G

Gunnar Hjalmarsson

Rocky said:
my $recipient1 = 'EMAIL1 HERE';
my $recipient2 = 'EMAIL2 HERE';
my $recipient3 = 'EMAIL3 HERE';
my $recipient4 = 'EMAIL4 HERE';
my $recipient5 = 'EMAIL5 HERE';
my $mailfrom = 'MAILFROM HERE';
my $fromaddress = "ISEETHEFROM HERE";

The problem is that the main script does not see the recipients
from the file. I am sure this is because of the way I have it
scoped, but I know no other way. Any pointers will be appreciated.

http://perl.plover.com/FAQs/Namespaces.html
 
P

Paul Lalli

Rocky said:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;
require ('hello.pl');
my $hostname = $ENV{HOSTNAME};
my $jakartahome = $ENV{CATALINA_HOME};
my $logdir = "$jakartahome/logs";

hello.pl looks like this:

my $recipient1 = 'EMAIL1 HERE';
my $recipient2 = 'EMAIL2 HERE';
my $recipient3 = 'EMAIL3 HERE';
my $recipient4 = 'EMAIL4 HERE';
my $recipient5 = 'EMAIL5 HERE';
my $mailfrom = 'MAILFROM HERE';
my $fromaddress = "ISEETHEFROM HERE";

The problem is that the main script does not see the recipients from the
file. I am sure this is because of the way I have it scoped, but I know
no other way. Any pointers will be appreciated.

By definition, 'my' variables are block-scoped. If there is no explicit block
surrounding their declaration, the implicit block is the file itself.. They are
not visable outside of the file in which they are defined. You want to be using
package variables instead:

hello.pl:
our @recipients = qw/email1 email2 email3 email4 email5/;
our $mailfrom = 'MAILFROM';
our $fromaddress = 'FROMADDR';

main script:

#!/usr/bin/perl
use strict;
use warnings;
require 'hello.pl';
{
our (@recipients, $mailfrom, $fromaddress);
#do whatever you need with the varialbes
}

There are other variations on this solution too. Rather than the our
declaration in the main script, you could fully qualify each variable name (such
as @main::recipients ). If you don't want to mung the main:: namespace at all,
you could declare hello.pl to be in a new package, and then also declare that
package to be in effect in the block in which you use the variables. (As use
strict is still in effect, the our declaration is still necessary, however).
Finally, you could declare hello.pl to be in a new package, and skip the our
declaration in the main script by fully qualifying each variable with the new
package name whenever you use it.

Paul Lalli
 
R

Rocky

By definition, 'my' variables are block-scoped. If there is no explicit block
surrounding their declaration, the implicit block is the file itself.. They are
not visable outside of the file in which they are defined. You want to be using
package variables instead:

hello.pl:
our @recipients = qw/email1 email2 email3 email4 email5/;
our $mailfrom = 'MAILFROM';
our $fromaddress = 'FROMADDR';

main script:

#!/usr/bin/perl
use strict;
use warnings;
require 'hello.pl';
{
our (@recipients, $mailfrom, $fromaddress);
#do whatever you need with the varialbes
}

There are other variations on this solution too. Rather than the our
declaration in the main script, you could fully qualify each variable name (such
as @main::recipients ). If you don't want to mung the main:: namespace at all,
you could declare hello.pl to be in a new package, and then also declare that
package to be in effect in the block in which you use the variables. (As use
strict is still in effect, the our declaration is still necessary, however).
Finally, you could declare hello.pl to be in a new package, and skip the our
declaration in the main script by fully qualifying each variable with the new
package name whenever you use it.

Paul Lalli

Thank you Mr. Lalli
 
B

Brian McCauley

Paul said:
There are other variations on this solution too. Rather than the our
declaration in the main script, you could fully qualify each variable name (such
as @main::recipients ). If you don't want to mung the main:: namespace at all,
you could declare hello.pl to be in a new package, and then also declare that
package to be in effect in the block in which you use the variables. (As use
strict is still in effect, the our declaration is still necessary, however).
Finally, you could declare hello.pl to be in a new package, and skip the our
declaration in the main script by fully qualifying each variable with the new
package name whenever you use it.

Another variation - convert your original script into a module and make
the config file into a script that uses that module.
 
J

J. Romano

Rocky said:
My boss wants to put all the recipients and hostnames in another file so
that we can change it per site.
here is what I tried

#!/usr/bin/perl
use strict;
use warnings;
use Net::SMTP;
require ('hello.pl'); [SOME CODE SNIPPED]

hello.pl looks like this:

my $recipient1 = 'EMAIL1 HERE';
my $recipient2 = 'EMAIL2 HERE';
my $recipient3 = 'EMAIL3 HERE';
my $recipient4 = 'EMAIL4 HERE';
my $recipient5 = 'EMAIL5 HERE';
my $mailfrom = 'MAILFROM HERE';
my $fromaddress = "ISEETHEFROM HERE";

The problem is that the main script does not see the recipients from the
file. I am sure this is because of the way I have it scoped, but I know
no other way. Any pointers will be appreciated.

Dear Rocky,

You are right when you think that it is the way you have it scoped.
The "my" keyword makes a variable visible only to one file. To let
it have visibility outside the file, change "my" to "our".

Also, I recommend that you end your "hello.pl" script with the
following line:

1;

This will prevent "require" from failing if the last variable in
"hello.pl" is set to zero (or an empty string).

You might even want to consider changing the name of "hello.pl" to
"hello.pm" since "hello.pl" is not meant to be executed directly.
That way, you can read in the "hello.pm" file just by typing:

use hello;

I hope this helps, Rocky.

-- Jean-Luc
 
D

David K. Wall

my $recipient1 = 'EMAIL1 HERE';
my $recipient2 = 'EMAIL2 HERE';
my $recipient3 = 'EMAIL3 HERE';
my $recipient4 = 'EMAIL4 HERE';
my $recipient5 = 'EMAIL5 HERE';

The original problem has been answered by other posters, so I'll ignore
it.

The quoted code is a red flag: a sequence of numbered scalars like that
suggests you should be using an array instead. Consider how your code
might change if you wanted to add a 6th recipient, and how using an
array could make that easier.

Mark-Jason Dominus wrote a series of "Red Flag" articles that you may
find interesting. Links to them are at http://perl.plover.com/.
 
R

Rocky

The original problem has been answered by other posters, so I'll ignore
it.

The quoted code is a red flag: a sequence of numbered scalars like that
suggests you should be using an array instead. Consider how your code
might change if you wanted to add a 6th recipient, and how using an
array could make that easier.

Mark-Jason Dominus wrote a series of "Red Flag" articles that you may
find interesting. Links to them are at http://perl.plover.com/.

Thank you sir. That site is pretty cool.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,884
Members
47,419
Latest member
ArturoBres

Latest Threads

Top