in reply to Re: Calling a sub without enclosing its argument inside brackets
in thread Calling a sub without enclosing its argument inside brackets

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

Replies are listed 'Best First'.
Re^3: Calling a sub without enclosing its argument inside brackets
by Bod (Parson) on Mar 18, 2024 at 21:34 UTC
    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?

      "Can pragmas be written or are they part of the Perl release?"

      Many pragmata form part of the core Perl distribution: see "Pragmatic Modules".

      As of Perl v5.10, you can write your own. The core doco perlpragma explains how to do this.

      — Ken

        The core doco perlpragma explains how to do this.

        The main drawback to that approach for my little project is the additional overhead of having to turn perl scalars into Math::Ryu objects and then overloading their stringification.
        It's probably not such a big deal, but then neither is the original issue ;-)
        BTW, in the Math::Ryu github repo, the function is named "pany", not "palt" - neither of which have yet appeared in a cpan release of the module.

        AFAICT, the "integer" pragma doesn't rely on module objects and operator overloading.
        I speculate that it's functionality is built into the perl source, thereby avoiding the need for objectification and operator overloading.

        OTOH, "bignum", "bigrat", "bigint" and "bigfloat" are clearly built on Math::BigInt, Math::BigRat and Math::BigFloat, and use the overloading that those 3 modules provide.

        It's all interesting stuff ...

        Cheers,
        Rob