Quick Question on Including Files

E

ExecMan

Hi,

I have created a file with a data structure in it. I want to include
that file into a number of scripts.

I have code like this:

my %SERVICES;

if (-e "hash_config") {
require "hash_config";
}

But it is not working?? The structure is just empty. The structure
is like the one I created with everyone's help:

%SERVICES = (
'tactical' => {
'service' => "tactical,
'url' => "tacticaltrader",
'from' => "Tactical Trader <tacticaltrader\@mail.com>",
'mailer' => "Tactical Trader Mailer",
'subject' => "Tactical Trader Summary",
'alert' => {
'GENERATE' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=0", 'command' => "eod-trader.pl
-t -M",},
'GENERATEBUY' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=1", 'command' => "trader.pl -t -
B -M",},
'GENERATESELL' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=2", 'command' => "trader.pl -t -
S -M",},
'SEND' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=0", 'command' => "eod-
trader.pl",},
'SENDBUY' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=1", 'command' => "trader.pl -
B",},
'SENDSELL' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=2", 'command' => "trader.pl -
S",},
'GENERATEINTRA' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=3", 'command' => "trader.pl -t -
I -M",},
'SENDINTRA' => { 'html' =>
"sid=SCt78s8Uf4Hml80&group_id=29&ctype=3", 'command' => "trader.pl",},
},
},
);

Help anyone? How do I make this an include so I can reference it?

Thanks!
 
T

Tim McDaniel

if (-e $config) {
%SERVICES = do $config;
}

Then you want your config file to *just* contain the data structure,
without any variable assignment, like this

tactical => {
service => "tactical",
url => "tacticaltrader",
...
},
...

If you use this syntax, I think the parentheses around the list have
to be supplied somewhere, though I've not tested it. That is, I think
it has to be either

%SERVICES = (do $config);

or the config file has to be

(
tactical => {
service => "tactical",
url => "tacticaltrader",
...
},
...
)

I looked at perlfunc do and came up with this test program.
It has an error-checking scheme that nearly requires a scalar return
value (otherwise, an error return in
my %x = (do "./103a.pl");
would cause warnings
Odd number of elements in hash assignment
Use of uninitialized value in list assignment
and result in a hash of
'' => undef
).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $x = do "./103a.pl";
warn "couldn't parse file: $@" if $@;
warn "couldn't do file: $!" unless defined $x;
warn "couldn't run file" unless $x;
my %x = %{$x};
print Data::Dumper->Dump([\%x], [qw[x]]), "\n";
exit 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[this is 103a.pl]

use strict;
use warnings;
{
a => 'b',
c => 'd',
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This does error checking: see what happens if you put a syntax error
in the included file (here, 103a.pl).

It does not require "our" variables.
 
E

ExecMan

Ben Morrow   said:
   if (-e $config) {
       %SERVICES = do $config;
   }
Then you want your config file to *just* contain the data structure,
without any variable assignment, like this
   tactical  => {
       service => "tactical",
       url     => "tacticaltrader",
       ...
   },
   ...

If you use this syntax, I think the parentheses around the list have
to be supplied somewhere, though I've not tested it.  That is, I think
it has to be either

        %SERVICES = (do $config);

or the config file has to be

    (
        tactical  => {
            service => "tactical",
            url     => "tacticaltrader",
            ...
        },
        ...
    )

I looked at perlfunc do and came up with this test program.
It has an error-checking scheme that nearly requires a scalar return
value (otherwise, an error return in
    my %x = (do "./103a.pl");
would cause warnings
    Odd number of elements in hash assignment
    Use of uninitialized value in list assignment
and result in a hash of
    '' => undef
).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $x = do "./103a.pl";
warn "couldn't parse file: $@" if $@;
warn "couldn't do file: $!"    unless defined $x;
warn "couldn't run file"       unless $x;
my %x = %{$x};
print Data::Dumper->Dump([\%x], [qw[x]]), "\n";
exit 0;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[this is 103a.pl]

use strict;
use warnings;
{
 a => 'b',
 c => 'd',

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This does error checking: see what happens if you put a syntax error
in the included file (here, 103a.pl).

It does not require "our" variables.


Perl is just so strange. And several attempts I got it to work like
this:

my %SERVICES = do "$CONFIG_DIR/config";

Interesting how this did NOT work:

my %SERVICES;

if (-e "hash_config") {
do "hash_config";

}

Not sure of the difference....
 
T

Tim McDaniel

Quoth (e-mail address removed):

I, on the other hand, *have* tested it, and they do not.

The parens are required on the RHS of a list assignment because
assignment binds tighter than comma. (This is because comma is sometimes
pretending to be the C comma operator, and is not helpful IMHO.) The RHS
of that assignment is a single function call ('do' is a builtin, but it
parses like a function call), so no parens are required. The expression
inside the file is a single statement returning a list, so again no
parens are required.

AH! Thank you. It works the same basic way as

#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub foo { 19, 20, 21 }
my @a = foo();
print Dumper(\@a), "\n";
my $a = foo();
print Dumper($a), "\n";
exit 0;

printing, first, [ 19, 20, 21 ], and second, just 21.
I was not assuming the OP would know what to do with a
$SERVICES hashref. ....

That's a bad plan, if you can avoid it. Copying a hash like that will
not just copy but also rehash all the elements.

Yes, but I was not assuming the OP would know what to do with a
$SERVICES hashref. Unless it's large or being done a lot, if you're
not familiar with it it's simpler to just stick it into the
appropriate aggregate.
or use Data::Alias.

It should be noted that that does not appear to be built in to Perl.
 
T

Tim McDaniel

Perl is just so strange. And several attempts I got it to work like
this:

my %SERVICES = do "$CONFIG_DIR/config";

Interesting how this did NOT work:


Not sure of the difference....

Leaving aside the obvious difference in file names:

In the example config file, there was just a list of data like
thumper => 'rabbit',
or whatever. Nowhere was it being assigned to a variable.
my %SERVICES;

if (-e "hash_config") {
do "hash_config";

}
would just evaluate it and throw it away. If you changed the "do" to
%SERVICES = do "hash_config";
it should have worked.

Though I really think it better that your code not ignore error
messages.
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top