SPUG: use strict and do files.

Borow, Scott W Scott.Borow at pss.boeing.com
Fri Jul 30 12:55:22 CDT 1999


I know this doesn't answer the original use strict question, but you may 
want to look at the Safe module as it gives you more control over what 
namespaces are used, what variables are shared with the config file, 
and what kinds of opcodes are allowed in your configuration file when it 
gets evaled.


# build a safe compartment
my $cpt = new Safe;

# define variables to share with main::
my @shared = qw(
        %seed_file_sites
        @devices_cf_hosts
);
$cpt->share( @shared );

# get the shared variables from our config file...
my $return;
unless ( $return = $cpt->rdo($config_file)) {
        die "Couldn't parse $config_file: $@" if $@;
        die "Couldn't do $config_file: $!"    unless defined $return;
}

--Scott

> ----------
> From: 	Stuart Poulin[SMTP:stuart_poulin at yahoo.com]
> Sent: 	Friday, July 30, 1999 10:23 AM
> To: 	Doug Beaver; charles.e.derykus at boeing.com
> Cc: 	spug-list at pm.org; stuart_poulin at yahoo.com
> Subject: 	Re: SPUG: use strict and do files.
> 
> 
> 
> --- Doug Beaver <dougb at scalar.org> wrote:
> > On Thu, Jul 29, 1999 at 07:34:33PM -0700, charles.e.derykus at boeing.com wrote:
> > > > Stu Poulin wrote:
> > > > I was looking for a cheap rc type file behavior.  I thought I could
> > > > just use a "do" file with variables in it - but I wanted "use
> > > > strict" to be enforced. The perlfunc man page says:
> > > >    do 'stat.pl';
> > > >      is just like
> > > >    scalar eval `cat stat.pl`;
> > > 
> > > > This doesn't appear to be true.  This is on win98 so s/cat/type/ but
> > > > "type" is expensive as far as run time.  Is there a way to force a
> > > > "use strict" on do files?
> > > 
> ...
> >  
> > The funny thing is, if you add C<use strict> to doit.pl, then you have
> > to declare the variables as either globals (with C<use vars> or by
> > stating the package name explicitly) or make them lexical.
> > 
> Yes, a use vars is in the original message.
> >
> ... 
> > You can still use do() to implement a quick and useful rc system though:
> > 
> > -------
> > doit.pl
> > -------
> > $LOGGING_DAEMON_CONFIG = {
> >     logdir => '/var/log',
> >     proclimit => 32,
> >     valid_domains => [qw/ibm.com sun.com hp.com/],
> > };
> > --snip--
> > 
> 
> My idea of cheap was to declare some variables with "use vars" and let perl
> toss an error if a name was mispelled or a unknown variable was set.
> Using a hash I'd have to parse for unknown keys myself.  (Of course that'd be
> easier than writing all this email ;-)
> 
> But you did give me an idea.  I though "What if I use the new psuedo hash? 
> That'd be easy!"
> Alas, it doesn't work with "do" because it's a compile time verses runtime
> issue.  But it does work with "require".   So maybe that's what I'll use.
> Thanks,
>   Stu
> 
> #!perl -w
> 
> use strict;
> 
> package doit;
> use vars qw($parms);
> $parms = [ { one=>1, two=>2 }, "main_one", "main_two" ];
> print "$doit::parms->{one}  $doit::parms->{two}\n";
> 
> # $parms->{whoops} = "whoops"; # Does toss an error with do :-) Compile time.
> 
> # do "doit.pl";
> require "doit.pl";
> 
> package main;
> print "$doit::parms->{one}  $doit::parms->{two}\n";
> 
> __END__
> 
> 
> 
> # doit.pl
> #use strict;
> 
> $parms->{one} = "doit_one";
> $parms->{two} = "doit_two";
> $parms->{whoops} = "error";  # doesn't toss an error with "do" -  Runtime
> 
> 
> _____________________________________________________________
> Do You Yahoo!?
> Free instant messaging and more at http://messenger.yahoo.com
> 
> 
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>     POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
>  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
>  SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
>         Email to majordomo at pm.org: ACTION spug-list your_address
> 
> 

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list