473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inherited class methods and special variables

Here is some sample code that uses inherited class methods:

sub parseHTML::docomment {
my $comment = shift ;
print $comment, "\n" ;
}

sub parseASP::AUTOLOAD {
use vars qw( $AUTOLOAD ) ;

my $key = $AUTOLOAD ;
my $package = __PACKAGE__ ;
$key =~ s/^${package}::// ;

eval "parseHTML::$key( \@_ )" ;
}

sub parseASP::doParse () {
...
local $/ = undef ;
$content = []
$buf = <HTMLFILE> ;
$buf =~ s|<!--(.*?)-->|'<!-- '.&docomment( $1, $comments ).'-->'|seig ;
...
}

sub parseASP::docomment {
my $comment = shift ;
print $comment, "\n" ;
}

&doParse() ;
The code listed above works fine, outputting each comment of an HTML file.

If I delete the last function, parseASP::docomment(), then the inherited
function parseHTML::docomment() should behave identically. However, this
time, the code outputs a bunch of blank lines.

My best theory is that the first argument, $1, is passed as a reference to a
local special variable that goes out of scope. But common sense tells me that
anything that goes on the stack should still be there when I pull it off.
Having lost my mind trying to figure this out, any insight or explanation
would be greatly appreciated.

-Jim
Jul 19 '05 #1
2 4031
js*******@tqis.com (Jim Schueler) wrote in message news:<ad**************************@posting.google. com>...
Here is some sample code that uses inherited class methods:
No it does not.

What you post is code that crys out to be rewritten using method calls
inheritance but actually uses subroutine call autoloading to fake
inheritance.

I may address that in another follow-up.

But your problem has nothing to do with that.
docomment( $1, $comments ) My best theory is that the first argument, $1, is passed as a reference to a
local special variable that goes out of scope. But common sense tells me that
anything that goes on the stack should still be there when I pull it off.
Having lost my mind trying to figure this out, any insight or explanation
would be greatly appreciated.


Actually you have not lost your mind. You have in fact got it right
except that $1 is not a special local variable, it is a special
_global_ variable.

I have described this problem a number of times in the newsgroup that
replaced this one when this one ceased to exist many years ago.

By passing $1 as a subroutine argument you create an alias to the
global variable $1. The global variable $1 magically always refers to
the last regex evaluated in the current or parent stack frames. This
means the value of $1 changes as you go in and out of scopes but it's
still the same variable. The solution is never to pass $1 et al to
subroutines without quoting.

Regard...

my $r = \$1;

'one' =~ /(.*)/;
print "$1 $$r\n"; # one one
{
{
'two' =~ /(.*)/;
print "$1 $$r\n"; # two two
}
print "$1 $$r\n"; # one one
}

As previously mentioned this newsgroup does not exist (see FAQ).
Please do not start threads here.
Jul 19 '05 #2
js*******@tqis.com (Jim Schueler) wrote in message news:<ad**************************@posting.google. com>...
Here is some sample code that uses inherited class methods:
No it does not.

What you post is code that crys out to be rewritten using method calls
inheritance but actually uses subroutine call autoloading to fake
inheritance.

I may address that in another follow-up.

I addresses your real problem in my previous follow-up.

But now I want to talk about something else
sub parseASP::AUTOLOAD {
use vars qw( $AUTOLOAD ) ;

my $key = $AUTOLOAD ;
my $package = __PACKAGE__ ;
$key =~ s/^${package}::// ;

eval "parseHTML::$key( \@_ )" ;
}


Using symbolic references references is usually a bad idea. Using
eval(STRING) to achieve something that could be achived by symbolic
references is worse.

I very much doubt that you really want to leave the package name in
$key if it is something other than __PACKAGE__. I suspect that either
this will never happen or you would want to strip the package name
regardless.

The code above is confused. Are you expecting __PACKAGE__ to be
'parseASP'? If so then the explicit package qualifier on the
subroutine declaration is redundant. If not then it's unlikely that
the above would work since you'd be looking at the $AUTOLOAD in the
wrong package.

package parseASP;
sub AUTOLOAD {
my ($key) = our($AUTOLOAD) =~ /([^:]*)$/ or die;
goto &{"parseHTML::$key"};
}

Note - this will still have the problem with $1 mentioned in my other
follow-up.

If you can't fix the calling code you can work-around this thus:

sub AUTOLOAD {
my ($key) = do { our($AUTOLOAD) =~ /([^:]*)$/ } or die;
goto &{"parseHTML::$key"};
}

Note: as I said before you'd probably be better off using inheritance
than AUTOLOAD.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
2
by: dsandor | last post by:
I have a class that is inherited by another class. Let say: public class vehicle { public string color { get ... set ... } }
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
9
by: Brian Henry | last post by:
If i inherite a queue class into my class, and do an override of the enqueue member, how would i then go about actually doing an enqueue of an item? I am a little confused on this one... does over...
1
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
3
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
0
by: rwoo_98 | last post by:
All, I have an abstract request object that is inherited by a number of classes. For example: public abstract class BaseRequest { #region Class Variables private string _token;
16
by: gwoodhouse | last post by:
Good Morning, Ive been programming in c# for a few months now, and one of the things i havnt quite figured out is this: I would like to have inherited classes with their own set of variables....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.