$obj->method will work on 5.004 at least, and I think on all 5.xxx versions.
Unlike a 'non-method' function invocation, which can take arguments without using parentheses if the function is predeclared or pre-defined, the $obj->method form cannot take arguments; if arguments are passed, a parenthesized list must be used, e.g., $obj->method('a', 'b'). (But see update below.)
>perl -v
This is perl, version 5.004_04 built for Intel
Copyright 1987-1997, Larry Wall
Win32 port Copyright 1996-1997 by Mortice Kern Systems Inc.
MKS version 7.5 build 1183
Perl may be copied only under the terms of either the
Artistic License or the GNU General Public License,
which may be found in the Perl 5.0 source kit.
>perl -wMstrict -le
"package Foo;
sub new {
my $class = shift;
return bless { @_ } => $class;
}
sub method {
my $self = shift;
return @{$self}{ @_ ? @_ : 'c' }
}
package main;
my $foobj = Foo->new( qw(a 1 b 2 c 3) );
print $foobj->method;
print $foobj->method( qw(b a) );
"
3
21
>perl -wMstrict -le
"package Foo;
sub new {
my $class = shift;
return bless { @_ } => $class;
}
sub method {
my $self = shift;
return @{$self}{ @_ ? @_ : 'c' }
}
package main;
my $foobj = Foo->new( qw(a 1 b 2 c 3) );
print $foobj->method;
print $foobj->method( qw(b a) );
print $foobj->method qw(b a);
"
syntax error at -e line 1, near "->method qw(b a)"
Execution of -e aborted due to compilation errors.
>perl -wMstrict -le
"sub foo { print 'foo' . $_[0] }
sub bar;
foo 'zot';
bar 'zot';
sub bar { print 'bar' . $_[0] }
"
foozot
barzot
>perl -wMstrict -le
"sub foo { print 'foo' . $_[0] }
sub bar;
foo 'zot';
bar 'zot';
baz 'zot';
sub bar { print 'bar' . $_[0] }
sub baz { print 'baz' . $_[0] }
"
Bareword "baz" not allowed while "strict subs" in use at ...
Unquoted string "baz" may clash with future reserved word at ...
String found where operator expected at -e line 1, at end of line
(Missing operator before ?)
syntax error at -e line 1, near "baz 'zot'"
Execution of -e aborted due to compilation errors.
Update: It turns out that when run under ActiveState version 5.8.2,
$foobj->method( qw(b a) );
$foobj->method qw(b a) ;
$foobj->method( 'b', 'a' );
all compile and all return the same values, but
$foobj->method 'b', 'a' ;
fails to compile.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.