Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-05-25 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found