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


in reply to Indirect Object Syntax

chromatic nicely summarises the parsing problem here:

new Foo has two barewords, and whenever the Perl parser encounters barewords, it has to guess at what they are. Sometimes it has hints at what those barewords are (if it's encountered declarations of filehandles or subroutines) and it can guess correctly. Sometimes it doesn't have hints, because Perl embraces late binding.

See also:

Related:

Added later:

Replies are listed 'Best First'.
Re^2: Indirect Object Syntax
by Bod (Parson) on Nov 07, 2021 at 12:45 UTC
    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...

      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!

      > 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. :)

      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
Re^2: Indirect Object Syntax
by ikegami (Patriarch) on Nov 08, 2021 at 17:58 UTC

    I don't think there's any guesses. If there is, let me know so I can fix this.