Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'll offer another linguistic interpretation for you and call this heuristically ambiguous object syntax (in addition to Perldoc's own references to indirect as well.)

The reason for this is spelled out in the perlobj documentation, which recommends the so-called indirect object syntax be avoided.

Consider the code below, which provides both a new and connect constructor.

The instantiation of $obj4 breaks because connect is also a built-in command, and Perl's heuristics prefer it over the connect constructor of the Example class. The same is true for the popular DBI->connect() constructor, as well as many other real-world examples you find in the wild.

It's best not to let something as muddy as heuristics determine what your code does, so I personally avoid such constructs with a passion. Leave that stuff to Java where it belongs ;).

use strict; use warnings; my $obj1 = Example->new(); my $obj2 = new Example; my $obj3 = Example->connect(); my $obj4 = connect Example; package Example; # Constructors # (be subclass friendly, even in example code ;) sub new { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; } sub connect { my $class = shift; $class = ref($class) || $class; # subclass boilerplate. return bless { }, $class; }

In reply to Re: "Indirect" object syntax? by Apero
in thread "Indirect" object syntax? by muba

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-04-19 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found