Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
There are tools in Perl that allow you to find things like that out by yourself.

The first and easiest (but perhaps not always the most precise) is B::Deparse, which lets perl parse the program, and then reconstructs it from its internal representation. Let's try that:

$ perl -MO=Deparse -ce '$a->method' $a->method; -e syntax OK $ perl -MO=Deparse -ce '$a->method()' $a->method; -e syntax OK

So B::Deparse thinks the two are equivalent.

If you don't trust that, you can get an output of the internal representation of Perl, which is called its op tree with B::Concise:

$ perl -MO=Concise -ce '$a->method()' 7 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 6 <1> entersub[t2] vKS/TARG ->7 3 <0> pushmark s ->4 - <1> ex-rv2sv sKM/1 ->5 4 <#> gvsv[*a] s ->5 5 <$> method_named[PV "method"] ->6 -e syntax OK $ perl -MO=Concise -ce '$a->method' 7 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v:{ ->3 6 <1> entersub[t2] vKS/TARG ->7 3 <0> pushmark s ->4 - <1> ex-rv2sv sKM/1 ->5 4 <#> gvsv[*a] s ->5 5 <$> method_named[PV "method"] ->6 -e syntax OK

You don't have to understand its output (neither do I, although I see some sense in some of it), but you can simply compare the two outputs:

$ perl -MO=Concise -ce '$a->method' > a -e syntax OK $ perl -MO=Concise -ce '$a->method()' > b -e syntax OK $ diff a b | wc -l 0

(Don't compare them without a tool, it's too easy to miss a small difference in 2x 8 lines of weird symbols).

So you see that both forms construct the same op tree.

(You don't have to understand the rest of the post, if it confuses you just ignore it).

That said, it's still no 100% proof, because the presence of the parenthesis might affect the parsing of the next term. To check that, you could try the same with

$a->method / 1 # /; $a->method() / 1 # /;

If the parser expects a term, the /.../ part will be parsed as a regexp, if it expects an operator it will parse the slash as the division operator, and the # takes care that the last slash isn't a syntax error. (Yes, I checked it; no, the parens don't make a difference).

In reply to Re: $obj->method v.s. $obj->method() by moritz
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 drinking their drinks and smoking their pipes about the Monastery: (2)
    As of 2024-07-14 06:45 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.