Possible bug?; keywords for sub idenifier?

T

Trent Curry

I just wondering, why does Perl allow you to use supposedly reserved words
for a sub (function) identifier?

An example that illustrates this is as follows:

test.pl
----------
#!/usr/bin/perl

package test;
use strict;

$test::Config = new Test_Config;

print $test::Config->my;
print "\n";
----------

Test_Config.pm
----------
package RMS_Config;
use strict;

sub new {
my $this = shift;
my $obj = {
'TEST1' => 1,
'TEST2' => 2
};

bless $obj, $this;
return $obj;
}

sub my {
my $this = shift;

return "test123";
}

1;

----------

Output
----------
[SR@SRLNX test]$ perl -W test.cgi
test123
[SR@SRLNX test]$
----------

Why on earth does it allow reserved names to be used as identifiers? I also
works if tried like this:

test_2.pl
----------
#!/usr/bin/perl

package test;
use strict;

sub my {
print "f o o\n";
}
 
T

Trent Curry

Trent Curry said:
I just wondering, why does Perl allow you to use supposedly reserved words
for a sub (function) identifier?

An example that illustrates this is as follows:
[Snipped example code]

Just FYI, I'm using Perl 5.6.1 (i386)
 
A

Amir Kadic

Trent said:
I just wondering, why does Perl allow you to use supposedly reserved words
for a sub (function) identifier?

Why not?
Can you imagine any trouble resulting from it
(except for human-unreadable code)
?

The interpreter knows what you mean when you call
a method on a blessed reference, doesn't it?

It also has no trouble with main::my, &my, or &my(),
but it will give up when you say just my(), so everything
is just perfect, IMHO.

Amir
 
T

Trent Curry

Amir Kadic said:
Why not?
Can you imagine any trouble resulting from it
(except for human-unreadable code)
?

The interpreter knows what you mean when you call
a method on a blessed reference, doesn't it?

It also has no trouble with main::my, &my, or &my(),
but it will give up when you say just my(), so everything
is just perfect, IMHO.

Amir

So its actually a feature then? Just seems rather odd when you've been
through so many other langs (c/c++/java/cobol/PL-SQL/and the likes). Its an
interesting feature though. Perl is a lang that never ceases to amaze me :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top