Re: auto-initializing values

2000-09-11 Thread Tom Christiansen

I like this idea, although not necessarily for the "spurious warnings"
reason.

--tom



Re: auto-initializing values

2000-09-11 Thread John Porter

Dave Storrs wrote:
 
   { 
   # handle pensioners here
my ($num_pensioners, $base_pension, $years_service) = (0,0,0); 
   }
 
   {
my $init_vars = 'ERROR::NAME_NOT_FOUND';
my ($name_1, $spouse_name_1) = ($init_vars)x2; 
   }
 
   {
my @init_vars = (1,7,9);
my (@blarg) = @init_vars;
   }
 
   {
my $rh_data_record = {};
   }

Four (slightly) different scenarios, all easily handled with existing
perl syntax.  Choose the one which fits best.

-- 
John Porter




RE: auto-initializing values

2000-09-11 Thread Dave Storrs



On Mon, 11 Sep 2000, Myers, Dirk wrote:

 Suppose you could specify the value with which all variables
 in the enclosing scope should be initialized; for example:
 
 I haven't seen this either, but I suggest that it should be a set of
 pragmas:
 use init_scalar 0 ;
 use init_array () ;
 
 ... especially because I'm not sure what effect :
 
   init_vars = \{} ;
 
   my @foo ;
 
 should have.  (Probably none, but how do you specify default values if you
 want to init multiple kinds of data in the same scope?)


My suggestion would be that it simply have no effect.  On the
other hand, if a "initial size" attribute were added to arrays, then this
could be very useful...say that you have 50 employees, each of whose data
is stored in a hash.  Here's an easy way to get a list of references to
all the hashes, with error handling built in:

init_vars \{name = 'NONE'};
my @employees : size 50;  # 50 entries, each a ref to 1 elem. hash
@employees = get_from_db('*');
for (@employees) {
if ( $_{name} eq 'NONE' ) {
die "Oops!  DB error\n";
}
}


Dave





Re: auto-initializing values

2000-09-11 Thread John Porter

Dave Storrs wrote:
 
 if a "initial size" attribute were added to arrays, then this
 could be very useful...say that you have 50 employees, each of whose data
 is stored in a hash.  Here's an easy way to get a list of references to
 all the hashes, with error handling built in:
 
   init_vars \{name = 'NONE'};
   my @employees : size 50;  # 50 entries, each a ref to 1 elem. hash
   @employees = get_from_db('*');
   for (@employees) {
   if ( $_{name} eq 'NONE' ) {
   die "Oops!  DB error\n";
   }
   }

I still don't see any compelling benefit for new syntax here.
Old syntax works great.

my @employees = map { { name = 'NONE' } } 1..50;

By the way, regardless of how @employees gets initialized above,
that initialization gets blown away by the next thing you do:

   @employees = get_from_db('*');  # only got 2 records???

Unfortunately, your approach in this particular case is flawed.

You could do something like this, which is ripe for optimizations:

for ( @employees ) {
get_from_db( \$_, '*' );
# may or may not have assigned some fields...

-- 
John Porter

We're building the house of the future together.




Re: auto-initializing values

2000-09-11 Thread Dave Storrs



On Mon, 11 Sep 2000, John Porter wrote:

 Dave Storrs wrote:
  
  init_vars \{name = 'NONE'};
  my @employees : size 50;  # 50 entries, each a ref to 1 elem. hash
  @employees = get_from_db('*');
  for (@employees) {
  if ( $_{name} eq 'NONE' ) {
  die "Oops!  DB error\n";
  }
  }
 
 I still don't see any compelling benefit for new syntax here.
 Old syntax works great.
  
   my @employees = map { { name = 'NONE' } } 1..50;


Ok, this is a fine complaint.  Which is why I started this thread
with the question "Would it be useful if"


 
 By the way, regardless of how @employees gets initialized above,
 that initialization gets blown away by the next thing you do:
 
  @employees = get_from_db('*');  # only got 2 records???
 
 Unfortunately, your approach in this particular case is flawed.


*Sigh*  This, unfortunately, shows that you completely missed my
point. The point of the exercise was to set up some variables (the entries
in the array), initialize them with sentienl values (i.e., values that are
known to be invalid), fill them with information from a DB, and then check
to make sure that you actually got valid info into each variable.  As
regards the imaginary function "get_from_db('*')"...I expected it to be
understood that that meant "get all records from the DB."


Dave