Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
 $obj->method will work on 5.004 at least, and I think on all 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.

In reply to Re^2: $obj->method v.s. $obj->method() by AnomalousMonk
in thread $obj->method v.s. $obj->method() by perl5ever

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    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: (2)
    As of 2024-07-14 07:37 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.