Quick Question on Including Files

Discussion in 'Perl Misc' started by ExecMan, May 2, 2012.

  1. ExecMan

    ExecMan Guest

    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!
     
    ExecMan, May 2, 2012
    #1
    1. Advertising

  2. ExecMan

    Tim McDaniel Guest

    In article <>,
    Ben Morrow <> wrote:
    > 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.

    --
    Tim McDaniel,
     
    Tim McDaniel, May 2, 2012
    #2
    1. Advertising

  3. ExecMan

    ExecMan Guest

    On May 2, 1:41 am, (Tim McDaniel) wrote:
    > In article <>,
    > Ben Morrow  <> wrote:
    >
    > >    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.
    >
    > --
    > 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:

    my %SERVICES;

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

    }

    Not sure of the difference....
     
    ExecMan, May 2, 2012
    #3
  4. ExecMan

    Tim McDaniel Guest

    In article <>,
    Ben Morrow <> wrote:
    >
    >Quoth :
    >> In article <>,
    >> Ben Morrow <> wrote:
    >> > 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.

    >
    >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.

    ....
    >> my %x = %{$x};

    >
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, May 2, 2012
    #4
  5. ExecMan

    Tim McDaniel Guest

    In article <>,
    ExecMan <> wrote:
    >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.

    --
    Tim McDaniel,
     
    Tim McDaniel, May 2, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eternally
    Replies:
    7
    Views:
    416
  2. JKop
    Replies:
    11
    Views:
    915
  3. Raman
    Replies:
    5
    Views:
    375
    Lew Pitcher
    Jun 7, 2008
  4. KIRAN

    Question about including header files

    KIRAN, Oct 10, 2008, in forum: C Programming
    Replies:
    3
    Views:
    304
    James Kuyper
    Oct 10, 2008
  5. Nathan Beyer
    Replies:
    0
    Views:
    119
    Nathan Beyer
    Nov 15, 2009
Loading...

Share This Page