Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Perl OOO Function Entrance

by shmem (Chancellor)
on Aug 28, 2017 at 17:00 UTC ( [id://1198195]=note: print w/replies, xml ) Need Help??


in reply to Perl OOP Function Entrance

There are in fact only 2 cases, if $self is not being made use of in the subroutine:

  1. a method call
  2. a function call

For a function call, no class resolving is done, so the function must be either fully qualified or its symbol imported into the current namespace.

A method call is resolved via $class_name or $class_instance (object), and the sub gets the resolving thing as first argument.

Conflating these two into the same thing just tells that the package is not meant to be object oriented. Losing the identifier of the call makes it impossible to dispatch to another class on which this one is based.

use 5.10.0; package Animal; sub speak { my $self = shift; ref $self and $self = ref $self; say "$self says: ", $self->word; } package Cow; @Cow::ISA = qw(Animal); sub word { "moo" } package Dog; @Cow::ISA = qw(Animal); sub word { "wuff" } package main; Cow->speak; Dog->speak; __END__ Cow says: moo Dog says: wuff

If you throw away the Animal, there's no way to make it speak.

So, this is not about Perl OOO Function Entrance but adding syntactic sugar to plain function calls. For that, the following does the job:

shift if $_[0] eq __PACKAGE__ || ref $_[0] eq __PACKAGE__;

This can be added via simple filtering as the first line to the body of a subroutine.

But I concur with LanX: I consider it bad practice to do such conflation on a regular basis, since it blurs the semantic differences of the various ways to call a subroutine. If that equality of dispatching is condoned within your team, it will bite you the first time when you use really object oriented modules from a third party.

And you haven't considered other ways to call a subroutine. The following are all equivalent:

use Animal; # contains Cow and Dog as above Cow->speak; speak Cow; my $animal = 'Cow'; $animal->speak; speak $animal; Animal::speak('Cow'); $animal = bless do{\my $x}, 'Cow'; Animal::speak($animal); $animal->speak;

Of all computer languages I know, perl is the most language thing. And you can make most use of a language knowing and musing about its subtleties, and expressing yourself acccordingly without blurring them.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Perl OOO Function Entrance
by Mano_Man (Acolyte) on Aug 28, 2017 at 17:18 UTC
    I suppose you have a point. The base ideas was to create a class 'Object' which all classes inherit from. I did not like this idea one bit, seeing the methods utilized are Utility methods, and not 'Object' methods. Maybe it is a better idea to simple 'require' a regular script file with subroutines, or simply import all functions with something like this:
    sub import { no strict 'refs'; my $caller = caller; while (my ($name, $symbol) = each %{__PACKAGE__ . '::'}) { next if $name eq 'BEGIN'; # don't export BEGIN blocks next if $name eq 'import'; # don't export this sub next unless *{$symbol}{CODE}; # export subs only my $imported = $caller . '::' . $name; *{ $imported } = \*{ $symbol }; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1198195]
help
Chatterbox?
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-04-25 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found