Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by AnomalousMonk (Archbishop)
on May 09, 2009 at 05:13 UTC ( [id://763007]=note: print w/replies, xml ) Need Help??


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

 $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.

Replies are listed 'Best First'.
Re^3: $obj->method v.s. $obj->method()
by ikegami (Patriarch) on May 09, 2009 at 16:24 UTC

    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
Re^3: $obj->method v.s. $obj->method()
by Burak (Chaplain) on May 09, 2009 at 15:08 UTC
    $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.

        You're possibly right :)
      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}
        I meant 5.8+ actually. Haven't used 5.6 much. I usually test 5.5.4 compatibility from time to time for some of my code. But having this in 5.6 is interesting as it kind of enables some perl6 features very early like the foreach loop you wrote :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found