scalars and namespace

Discussion in 'Perl Misc' started by Jeff Thies, Jun 27, 2003.

  1. Jeff Thies

    Jeff Thies Guest

    I have all these "config" files with lists of global scalars:

    config_1.cfg
    ###########

    $scalar1='something';
    $scalar2='something_else';


    #########

    That worked for me in that I could import those scalars like this:

    require 'path_to/config_1.cfg';

    Now I want to import those into a sub and make that list of scalars
    local to that sub. (I want to do something with the list of scalars in
    all these "config" files one file at a time) How do I do that?

    I can see where I would have been better off using hashes!

    Jeff
     
    Jeff Thies, Jun 27, 2003
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Jeff Thies <> wrote in
    news::

    > I have all these "config" files with lists of global scalars:

    ....
    > Now I want to import those into a sub and make that list of scalars
    > local to that sub. (I want to do something with the list of scalars in
    > all these "config" files one file at a time) How do I do that?


    I guess it depends on what you want to do. I'm kinda unclear on what
    you're trying to accomplish. (What does "local to that sub" mean?)

    - --
    Eric
    $_ = reverse sort qw p ekca lre Js reh ts
    p, $/.r, map $_.$", qw e p h tona e; print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBPvz4LmPeouIeTNHoEQIx/QCg/NnMzSp54UFFf1O8f2+/j2GsJBQAoIe8
    MEQJ5TdALv8Af1sQstoHApyS
    =fVfd
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Jun 28, 2003
    #2
    1. Advertising

  3. Jeff Thies

    bd Guest

    On Fri, 27 Jun 2003 21:07:31 -0500, Eric J. Roode wrote:

    > Jeff Thies <> wrote in
    > news::
    >
    >> I have all these "config" files with lists of global scalars:

    > ...
    >> Now I want to import those into a sub and make that list of scalars
    >> local to that sub. (I want to do something with the list of scalars in
    >> all these "config" files one file at a time) How do I do that?

    >
    > I guess it depends on what you want to do. I'm kinda unclear on what
    > you're trying to accomplish. (What does "local to that sub" mean?)


    Perhaps he means a lexical variable? Beyond that, I don't know.

    --
    Freenet distribution not available
    If you don't do it, you'll never know what would have happened if you
    had done it.
     
    bd, Jun 28, 2003
    #3
  4. Jeff Thies

    Jeff Thies Guest

    > > I have all these "config" files with lists of global scalars:
    > ...
    > > Now I want to import those into a sub and make that list of scalars
    > > local to that sub. (I want to do something with the list of scalars in
    > > all these "config" files one file at a time) How do I do that?

    >
    > I guess it depends on what you want to do. I'm kinda unclear on what
    > you're trying to accomplish. (What does "local to that sub" mean?)


    Yes lexical. I'm sorry this is unclear.

    What I'm doing is creating html pages from templates. The scalars (from
    the config files, one config file per page) get plugged into the
    template. The trouble I have is that the number of scalars in each
    config file are different and may have different names.

    That means that when I have all these scalars global to the script that
    for ex: $scalar_that_exists_in_config1_but_not_config2 will still be
    around when I'm processing the page for config2. If I had done this with
    a hash, I could've just emptied the old hash out and reassigned it to
    the hash in the new config file. If I had made these packages, I could
    have just imported the package names. Instead I've stuck myself with
    these global scalars.

    What I think I'll do is like what Gunnar suggested.

    Jeff


    >
    > - --
    > Eric
    > $_ = reverse sort qw p ekca lre Js reh ts
    > p, $/.r, map $_.$", qw e p h tona e; print
    >
    > -----BEGIN PGP SIGNATURE-----
    > Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
    >
    > iQA/AwUBPvz4LmPeouIeTNHoEQIx/QCg/NnMzSp54UFFf1O8f2+/j2GsJBQAoIe8
    > MEQJ5TdALv8Af1sQstoHApyS
    > =fVfd
    > -----END PGP SIGNATURE-----
     
    Jeff Thies, Jun 28, 2003
    #4
  5. Gunnar Hjalmarsson <> wrote:
    > Jeff Thies wrote:
    > > I have all these "config" files with lists of global scalars:
    > >
    > > config_1.cfg
    > > ###########
    > >
    > > $scalar1='something';
    > > $scalar2='something_else';
    > >
    > >
    > > #########
    > >
    > > That worked for me in that I could import those scalars like this:
    > >
    > > require 'path_to/config_1.cfg';
    > >
    > > Now I want to import those into a sub and make that list of scalars
    > > local to that sub. (I want to do something with the list of scalars in
    > > all these "config" files one file at a time) How do I do that?

    >
    > One way:
    >
    > sub mysub {
    > my ($scalar1, $scalar2);
    > open FH, 'config_1.cfg' or die "Can't open... $!";
    > eval while <FH>;
    > close FH;
    > [do something]
    > }
    >
    > / Gunnar


    Shouldn't you do something in there to prevent the overhead of opening,
    interpreting, and closing FH _every_ time mysub is invoked (and thus
    have containers _with state_ for the scaler values), or have I
    (very likely) missed some subtlety in the original request?

    xanthian.
     
    Kent Paul Dolan, Jun 28, 2003
    #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. Ray Gardener

    Inheriting scalars

    Ray Gardener, Jun 9, 2004, in forum: C++
    Replies:
    7
    Views:
    436
    Ray Gardener
    Jun 10, 2004
  2. Replies:
    8
    Views:
    331
    Paul Boddie
    Mar 1, 2005
  3. Roland Reichenberg
    Replies:
    7
    Views:
    172
    Josef Möllers
    Oct 14, 2003
  4. radioactiveman

    arrays and scalars.

    radioactiveman, Jul 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    116
    Brian McCauley
    Jul 27, 2004
  5. AMLiapunov

    Scalars Leaked and Segmentation Fault

    AMLiapunov, Feb 21, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    125
    zentara
    Feb 23, 2006
Loading...

Share This Page