Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: $obj->method v.s. $obj->method()

by Burak (Chaplain)
on May 09, 2009 at 04:02 UTC ( [id://762999]=note: print w/replies, xml ) Need Help??


in reply to $obj->method v.s. $obj->method()

$obj->method will not work on some archaic perls (5.005 or 5.004 I guess) apart from that no.

Replies are listed 'Best First'.
Re^2: $obj->method v.s. $obj->method()
by AnomalousMonk (Archbishop) on May 09, 2009 at 05:13 UTC
     $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.

      qw(b a) is (currently) equivalent to ('b', 'a') at a very low level. The parser only sees the latter.

      It also fools the x operator.

      >perl -le"$,=', '; print qw( a b c ) x 3" a, b, c, a, b, c, a, b, c >perl -le"$,=', '; print sub { qw( a b c ) }->() x 3" ccc

      Update: Even high level stuff like for.

      >perl -le"for my $x qw( a b c ) { print $x }" a b c
      $obj->method will work on 5.004 at least, and I think on all 5.xxx versions.

      As I recall, this feature was broken in a specific version of perl, caused by a wrong commit I think. But I couldn't locate something related to that now with a little Googling.

      $foobj->method qw(b a) ;
      Ah, that's interesting :) Recent perl (5.10 & 5.8.8) parses that as:
      C:\Users\burak>perl -MO=Deparse -e "$foobj->method qw(b a) ;" $foobj->method('b', 'a'); -e syntax OK [schultz]$ perl -MO=Deparse -e '$foobj->method qw(b a) ;' $foobj->method('b', 'a'); -e syntax OK [schultz]$
      it looks like qw() has some magic in it in recent versions

        IIRC it's $obj->$method that wouldn't work in 5.4 (and 5.6?) while $obj->$method() would.

        it looks like qw() has some magic in it in recent versions
        If you mean with "recent" any formal releases in years starting with '2', then yes.

        This has worked ever since 5.6.0 - which was the version that started doing qw at compile time, turning qw[foo bar] into ('foo', 'bar'). With parens. Probably most often seen in:

        foreach my $qw qw (qw qw) {print $qw}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://762999]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2025-06-24 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.