david wrote:

> Fernando wants to send the following thanks to all people who helped him on
> this topic and some explanation of what he really wants to do. He somehow
> sent it to myself only.
>
> #------------- forward message started -------------#
>
> Hello all,
>

OK, then.  One subject at a time.  It helps in focusing your efforts, both in
posting to a list, and in designing your code.

> > I want to have a class I won't instanciate
>
> Here I was badly trying to define a set of functions, procedures and
> attributes which you can use from anywhere in your program, and which
> are collected under a common class, but something you don't have to
> instanciate => which, in Perl, seems to be a namespace or package.

Got it.

> An
> example could be a "Config class" which has methods like WriteConfig
> or ReadConfig -- like for reading .ini files, and it has no sense (for
> me) to instanciate such a class.

Hmmm, I;m not sure about that.  It seems to me that a common handler for such a
purpose would allow it to be used for many programs, or for many users of the
same program.  If you want to make your preferences more granular,
instantiation could be a benefit...but that is not what you are asking for.

There is no reason that you cannot make a package to hold appropriately global
data.  We seldom encourae such approaches, because usually students are just
avoiding certain challenges, such as strict compilation, thinking out scopes,
etc.

Never the less here is one that has not a hint of instances, and writes and
reads an ini file.

RJNConfig.pm:
package RJNConfig;  # less likely to step on existing modules than a
                    # generic name like Config alone

use strict;
use warnings;

use Exporter;

our @ISA = ('Exporter');

our @EXPORT_OK = ();
our @EXPORT = qw(get_specifications write_specifications $specifications);
our $specifications = {};

use constant PROGRAM_NAME => 'test_single_config';

my $program_name = PROGRAM_NAME;

sub get_specifications {
  open IN, "$program_name.ini" or return 0, 'Could not open program initiation
file';
  while (my $spec_line = <IN>) {
    chomp $spec_line;
    my ($specification, $value) = split /\s*=\s*/, $spec_line;
    $specifications->{$specification} = $value;
  }
  close IN;
  return $specifications, 0;
}

sub write_specifications {
  our $specifications = $_[0];

  open OUT, ">$program_name.ini" or return;
  foreach (keys %$specifications) {
    my $spec_line = "$_ = $specifications->{$_}\n";
    print OUT $spec_line;
  }
  close OUT;
}


#!perl

use strict;
use warnings;

use RJNConfig;

my ($specs, $error) = get_specifications();
foreach (keys %$specs) {
  print "the $_ of this program is $specs->{$_}\n";
}

print "Now you set the specs, user-guy, since it IS your machine.
  Cry UNCLE when you've had enough\n";

my %specs;
my $line;
while (defined ($line = <STDIN>) and !($line =~ /UNCLE/i)) {
  my $spec = $line;
  chomp $spec;
  my $value = <STDIN>;
  chomp $value;
  $specs{$spec} = $value;
}

write_specifications(\%specs)

Testing at the command-line:
Greetings! E:\d_drive\perlStuff>test_single_config.pl
the Program name of this program is test_single_config
the Purpose of this program is To mess wit ya, man
Now you set the specs, user-guy, since it IS your machine.
  Cry UNCLE when you've had enough
Favorite Movie
Lassie Come Home
Favorite song
Mr. Rogers' Neighborhood
UNCLE

Greetings! E:\d_drive\perlStuff>test_single_config.pl
the Favorite song of this program is Mr. Rogers' Neighborhood
the Favorite Movie of this program is Lassie Come Home
Now you set the specs, user-guy, since it IS your machine.
  Cry UNCLE when you've had enough
Some silly-ass global spec
A happenstance value sure to get me in trouble
Another spec that came out of nowhere
By serindipity, a value that actually makes sense.
UNCLE

Greetings! E:\d_drive\perlStuff>

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to