http://qs321.pair.com?node_id=11138549


in reply to Re: Indirect Object Syntax
in thread Indirect Object Syntax

see also Indirect Object Syntax by Bod from (working class) Coventry, UK.

Around here they call me posh as I hail from Warwick. Only 14 miles distance geographically but a world apart if the British class system still existed!

new Foo has two barewords

Is new not a keyword that Perl understands without any context other than a module name will follow?

So, to ensure I do understand...
Are these the accepted way to do it? Can they be imporved?

my $gd = GD::Image->new(); @bounds = $gd->stringFT(...);
Is it OK to condense that down to this and avoid the intermediate variable definition?
@bounds = GD::Image->new()->stringFT(...);
Are the brackets necessary as the -> operator explicitly tells Perl this is a method call?
@bounds = GD::Image->new->stringFT(...);


Update: I've been reading this book and realised that new is not a keyword in Perl! It is just the name of a commonly used module method. Another little chunk of understanding falls into place...

Replies are listed 'Best First'.
Re^3: Indirect Object Syntax
by haukex (Bishop) on Nov 07, 2021 at 13:21 UTC
    I've been reading this and realised that new is not a keyword in Perl!

    Correct! Though it's generally considered polite to pay for those books than to read the pirated versions online (you may want to remove the link and replace it with a reference to the actual book).

    Are these the accepted way to do it? Can they be imporved?

    Yes, those are (syntactically) correct and best practice. They can be improved in the ways you showed:

    Is it OK to condense that down to this and avoid the intermediate variable definition?

    Yes, if you don't need the object - though some OO classes make a habit of returning the original object from some methods in order to explicitly allow method chaining, e.g. my $json = JSON::PP->new->ascii->pretty->allow_nonref;

    Are the brackets necessary

    No, they're not; IMHO it's fine to leave them off when there are no arguments (others might feel differently).

      Thanks haukex for your help

      you may want to remove the link and replace it with a reference to the actual book

      Done!

Re^3: Indirect Object Syntax
by eyepopslikeamosquito (Bishop) on Nov 07, 2021 at 20:49 UTC

    > Around here they call me posh as I hail from Warwick. Only 14 miles distance geographically but a world apart if the British class system still existed!

    Good to know you share a nickname with Posh Spice. :) We have something in common because my mother also hails from Warwick - only 10,286 miles distance geographically from Warwick UK, but a world apart! Unlike its UK namesake, Warwick Queensland is definitely not posh. :)

Re^3: Indirect Object Syntax
by bliako (Monsignor) on Nov 09, 2021 at 09:26 UTC
    Is new not a keyword that Perl understands without any context other than a module name will follow?

    Technically, one can use any valid sub name instead of new for the role of "create object" (i.e. blessing a hash with the module name), when writing an OOP module -- and can use sub new for some other function, confusing yes but it shows that name new is nothing special:

    { package MyObj; sub retro { bless { name => $_[1] }, $_[0] } sub string { join ', ', values %{ $_[0] } } } my $obj = MyObj->retro("aghag"); my $obj2 = retro MyObj("aaa"); # with the usual caveats