ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
RE: why doesn't this work?
by Peter Guzis other posts by this author
Sep 27 2004 12:53PM messages near this date
view in the new Beta List Site
why doesn't this work? | domain account installed as a local admin
Hello John,

I suspect if you turned on "use warnings" you would see that you are attempting to redefine 
callme() regardless of the current state of $ENV{Oracle}.  To make your current code work, t
ry using typeglobs when setting up your subs.

##

if ($ENV{Oracle}) {

  *callme = sub {print("Version 1\n");}

} else {

  *callme = sub {print("Version 2\n");}

}

##

However, what you should probably be doing is making a base class with a bare minimum of fun
ctionality and subclassing it for each database type.  Small hacks like typeglobs are great 
with quick and dirty scripts, but venturing into the realm of object-oriented programming wi
ll pay off with more flexible, easier to maintain code in the long run.

###################
## BEGIN test.pl ##
###################

use strict;
use warnings;

use TestOracle;
use TestMSSQL;

TestOracle-> callme();
TestMSSQL-> callme();

exit;

#######################
## BEGIN TestBase.pm ##
#######################

package TestBase;

use strict;
use warnings;

# class method (I'll gloss over creating objects this time)

sub callme {

  my $class = shift;  
  
  # do some common task here
  print "Base callme()\n";

}

1;

#########################
## BEGIN TestOracle.pm ##
#########################

package TestOracle;

use strict;
use warnings;

use base 'TestBase';

sub callme {

  my $class = shift;  

  # do common tasks
  $class-> SUPER::callme (@_);

  # do some Oracle-specific task here
  print "Oracle callme()\n";

}

1;

########################
## BEGIN TestMSSQL.pm ##
########################

package TestMSSQL;

use strict;
use warnings;

use base 'TestBase';

sub callme {

  my $class = shift;  

  # do common tasks
  $class-> SUPER::callme (@_);    

  # do some MSSQL-specific task here
  print "MSSQL callme()\n";

}

1;

##

HTH

Peter Guzis
Web Administrator, Sr.
ENCAD, Inc.
- A Kodak Company
email: pguzis@[...].com
www.encad.com


-----Original Message-----
From: John Deighan [mailto:jdeighan@[...].com]
Sent: Monday, September 27, 2004 11:03 AM
To: perl-win32-users@[...].com
Subject: why doesn't this work?


I'm trying to develop a Perl module that will work a bit differently 
depending on which kind of database is installed. The idea is to define an 
environment variable named 'Oracle' that, if set to 1, installs a different 
version of a function. In the following test code, I'm trying to define one 
or the other version of a function named 'callme', but it's not working. 
The environment variable named 'Oracle' is set to 1 (the main script prints 
out its value), but the test library still installs the second version of 
callme when it should be installing the first.

================
test script:
================
use strict;
use testlib;

print("Env Var 'Oracle' is set to '$ENV{Oracle}'\n");
callme();

================
test library (testlib.pm):
================
use strict;

if ($ENV{Oracle}) {
	sub callme {print("Version 1\n");}
	}
else {
	sub callme {print("Version 2\n");}
	}

1;

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved