scalars and namespace

J

Jeff Thies

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
 
E

Eric J. Roode

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

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

bd

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

Jeff Thies

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

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
 
K

Kent Paul Dolan

Gunnar Hjalmarsson said:
Jeff said:
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.
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top