in reply to Re: $obj->method v.s. $obj->method()
in thread $obj->method v.s. $obj->method()
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.)
Update: It turns out that when run under ActiveState version 5.8.2,>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.
all compile and all return the same values, but$foobj->method( qw(b a) ); $foobj->method qw(b a) ; $foobj->method( 'b', 'a' );
fails to compile.$foobj->method 'b', 'a' ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: $obj->method v.s. $obj->method()
by ikegami (Patriarch) on May 09, 2009 at 16:24 UTC | |
|
Re^3: $obj->method v.s. $obj->method()
by Burak (Chaplain) on May 09, 2009 at 15:08 UTC | |
by Your Mother (Archbishop) on May 09, 2009 at 16:41 UTC | |
by Burak (Chaplain) on May 09, 2009 at 17:03 UTC | |
by AnomalousMonk (Archbishop) on May 09, 2009 at 18:40 UTC | |
by Burak (Chaplain) on May 09, 2009 at 18:53 UTC | |
| |
by JavaFan (Canon) on May 09, 2009 at 18:38 UTC | |
by Burak (Chaplain) on May 09, 2009 at 18:45 UTC |