darren chamberlain wrote:
>
> NAME
> Env::Object - An object oriented interface to the current environment
Hello Darren,
Thanks for posting this. While I like and use OO programming methods,
I'm having trouble seeing the value of this additional abstraction. For
example, when using the direct hash method, when I know I want to use
the SCRIPT_NAME, I can I just do this:
"Here's the $ENV{SCRIPT_NAME} you were looking for"
With your method, it takes three lines:
use Env::Object;
my $env = Env::Object->new;
"Here's the ".$env->script_name." you were looking for"
plus, I can't embedded in it an interpolated string anymore because it's
an object, and I may have to do mental lookup to make sure that $env->script_name
really maps to $ENV{SCRIPT_NAME}.
Again, with your subclassing examples, it looks like an extra step to
do something I could do quicker directly.
OTOH, perhaps there are OO purists out there that will better appreciate
having the environment variables available through this framework. :)
-mark
>
> SYNOPSIS
> use Env::Object;
>
> my $env = Env::Object->new;
>
> print $env->home;
> print $env->editor;
>
> my @path = $env->path;
>
> printf "I am %s and my home directory is %s\n",
> $env->username, $env->home;
>
> DESCRIPTION
> I wrote Env::Object because I was sick of accessing the environment via
> the %ENV hash. Env::Object looks cleaner and has the possibility of
> encapsulating more than just %ENV.
>
> METHODS
> new The standard constructor.
>
> my $e = Env::Object->new;
>
> clone
> The clone method returns a copy of the current Env::Object object.
> Note that this is a shallow copy (this is not an issue with the
> default implementation, but subclasses that have multiple levels of
> references should take this into consideration, and subclass the
> clone method).
>
> username
> username attempts to determine the current user's username. This is
> generally the value of $ENV{USER}, but could also be $ENV{LOGNAME}
> or $ENV{LOGNAME} (and possibly others; patches welcome).
>
> path
> path returns the value of $ENV{PATH}. If called in scalar context,
> it is returned as it is stored by the shell (e.g., as a
> colon-separated string), but in list context, it returns a list.
>
> uid, gid, group
> These functions return the current uid, gid, and group name of the
> current user.
>
> my $env = Env::Object->new;
>
> printf "%s's uid/gid is %d/%d.\n",
> $env->username, $env->uid, $env->gid;
>
> All other methods are autoloaded, and have the same name as elements in
> the environment (upper or lowercase). Common ones include home, path (in
> the environment's native format, i.e., as a ':' separated string for
> sh-derived shells), logname, and mail.
>
> SUBCLASSING
> The usefulness of Env::Object becomes obvious when it is subclassed.
>
> Example 1: DBI::Env
>
> package DBI::Env;
>
> use DBI;
> use Env::Object;
> use base qw/Env::Object/;
> my $sql = "SELECT * FROM env WHERE UID = ?";
>
> sub new {
> my $class = shift;
> my $dbh = DBI->connect_cached(...);
> my $env = {};
>
> my $sth = $dbh->prepare_cached($sql);
> $sth->execute($<);
> $env = $sth->fetchrow_hashref;
> $sth->finish;
>
> bless $env => $class;
> }
> __END__
>
> #!/usr/bin/perl
>
> use DBI::Env;
>
> my $env = DBI::Env->new($dbh);
>
> print $env->user;
> # and so on....
>
> Example 2: CGI::Env
>
> package CGI::Env;
>
> use Env::Object;
> use base qw(Env::Object);
>
> sub new {
> my $class = shift;
> my $cgi = shift;
> my $self = {};
>
> for (keys %{$ENV}) {
> $self->{$_} = $ENV{$_};
> }
>
> for ($q->param) {
> $self->{$_} = $q->param($_);
> }
>
> bless $self, $class;
> }
>
> #!/usr/bin/perl
>
> use CGI;
> use CGI::Env;
>
> my $q = CGI->new;
> my $e = CGI::Env->new($q);
>
> AUTHOR
> darren chamberlain <[EMAIL PROTECTED]>
>
> COPYRIGHT
> Copyright 2001 darren chamberlain
>
> This module is free software; you can redistribute it and/or modify it
> under the same terms as Perl itself.
>
> --
> Democracy is a form of government that substitutes election by the
> incompetent many for appointment by the corrupt few.
> -- George Bernard Shaw
--
. . . . . . . . . . . . . . . . . . . . . . . . . .
Mark Stosberg Principal Developer
[EMAIL PROTECTED] Summersault, LLC
v: 765-939-9301 ext 223 website development
. . . . . http://www.summersault.com/ . . . . . . .