Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Calling a sub without enclosing its argument inside brackets

by syphilis (Archbishop)
on Mar 18, 2024 at 04:05 UTC ( [id://11158326]=perlquestion: print w/replies, xml ) Need Help??

syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

The demo:
use strict; use warnings; use Math::BigInt; # The following line of code will die with: # Can't locate object method "palt" via package "Math::BigInt" at try. +pl line 7. # palt Math::BigInt->new(123456); # But this next rendition is fine, and works as expected: palt(Math::BigInt->new(123456)); sub palt { my $arg= shift; print "$arg\n"; } __END__ Outputs: 123456
Is there some technique that will allow me to make that (failing) call work - without having to place that argument in brackets ?

Cheers,
Rob

Replies are listed 'Best First'.
Re: Calling a sub without enclosing its argument inside brackets
by ikegami (Patriarch) on Mar 18, 2024 at 04:53 UTC

    Add the following to get the desired behaviour:

    no feature qw( indirect );

    The following has the same effect:

    use v5.36;
    use v5.38;
      > Add the following to get the desired behaviour:

      > no feature qw( indirect );

      For completeness, he still needs to predeclare the sub palt .

      Either by putting sub palt { ... } earlier or use subs qw(palt); to tell the compiler that it's not a bareword.

      Otherwise he'll get

      Bareword found where operator expected (Do you need to predeclare "palt"?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        For completeness, he still needs to predeclare the sub palt .

        Yeah. That's always the case when calling a sub without parens or &.

        It can also be done using sub palt;.

      no feature qw( indirect );

      Cool ... thanks ikegami++

      Cheers,
      Rob
Re: Calling a sub without enclosing its argument inside brackets
by eyepopslikeamosquito (Archbishop) on Mar 18, 2024 at 04:47 UTC

    I get the same results as you with perl v5.38.2.

    These two work for me:

    palt 'Math::BigInt'->new(123456); palt +Math::BigInt->new(123456);

    Update: so long as I define (or pre-declare) sub palt before making these calls.

    Is there some technique that will allow me to make that (failing) call work - without having to place that argument in brackets ?

    Curious to know why you need to do that - XY Problem?

    👁️🍾👍🦟
      Curious to know why you need to do that

      Let's say I don't like the way that perl's print() function outputs non-zero NVs.
      So I write an "alt"ernative "p"rint function (palt()) that outputs non-zero NVs as I like to see them presented.

      Then, whenever I want to print() a scalar, I need to keep track of whether that scalar is a non-zero NV, or whether it's something else.
      If it's a non-zero NV, I certainly don't want to do  print($sv), and if it's a plain string or a reference to a module object, I certainly don't want to coerce it to an NV by doing palt($sv).
      It would be much better, IMO, if I could just use palt() every time I wanted to print anything - and rely on it to provide the customized output for non-zero NVs, and also to provide the same output as print for all other arguments.
      And that's essentially what I've done.

      Given that print is rarely given bracketed arguments, it would be nice if palt did the same.
      But if that can't be done, then I just document that palt should always be given bracketed argument(s) ... and move on.
      It's not really a big deal ... just something I wanted to check on.

      ikegami's solution looks fine to me - the only downside being that it doesn't port back to pre-5.36 perls, IIUC.

      Cheers,
      Rob
        It would be much better, IMO, if I could just use palt() every time I wanted to print anything

        Perhaps a better solution would be a module/pragma that changed the behaviour of print so that it performed as palt does.

        use strict; use warnings; use palt; # <- new module/pragma print "This is a palt style print\n";

        Is that possible?
        Can pragmas be written or are they part of the Perl release?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11158326]
Approved by ikegami
Front-paged by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2025-06-20 10:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.