Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Optional Subroutine Arguments Like in `print FILEHANDLE LIST`

by mikkoi (Beadle)
on May 27, 2023 at 20:51 UTC ( [id://11152428]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to create my own subroutines which would work the same way as print FILEHANDLE LIST works.

Is there any non-magic way to create a subroutine with an optional first argument? Or would that just create confusion in code?

The sub in question would be used to send a job into a queue. In most circumstances, there is only one queue. But there could be others. And the sub takes only one argument.

example:

queue $job; queue OTHER_QUEUE $job;

Replies are listed 'Best First'.
Re: Optional Subroutine Arguments Like in "print FILEHANDLE LIST"
by LanX (Saint) on May 28, 2023 at 00:12 UTC
    > I would like to create my own subroutines which would work the same way as print FILEHANDLE LIST works.

    That's actually just another syntax for FILEHANDLE->print(LIST) a.k.a. "indirect method call".

    So creating your own methods would do and

    • a bareword OTHER_QUEUE must be a package
    • a var $other_queue hold an object or a class-name (= package).

    But are you sure you wanna go such exotic ways for just a little syntactic sugar?

    IMHO it's better to simply write

    • queue other => $job;

    and checking if queue() had 1 or 2 args.

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      That's actually just another syntax for FILEHANDLE->print(LIST) a.k.a. "indirect method call".

      It's not. print, as an operator, can have whatever syntax it wants. And while print FH ... looks like an indirect method call, it's not. It's simply the print function/op. This means it doesn't call the print of IO::File or whatever package *FH{IO} is blessed into like FH->print( ... ) does.

      But are you sure you wanna go such exotic ways for just a little syntactic sugar?

      Note that indirect method calls are disabled in v5.36 of the language (i.e. when you use use v5.36;). It's not the time start using them!

        I seem to remember that I was able to overide say in this way, but I can't find the thread anymore.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: Optional Subroutine Arguments Like in print FILEHANDLE LIST
by kcott (Archbishop) on May 28, 2023 at 02:59 UTC

    G'day mikkoi,

    "I would like to create my own subroutines which would work the same way as print FILEHANDLE LIST works."

    There's a big difference between "works the same" (as suggested by your title) and "looks the same" (as suggested by your example).

    If you simply want an optional first argument which is a filehandle-like object, you can do something like this (pm_11152428_opt_arg.pl):

    #!/usr/bin/env perl use strict; use warnings; queue("This is: queue(string)\n"); queue('This is: ', "queue(string, string)\n"); queue(\*STDERR, "This is: queue(\\*STDERR, string)\n"); queue(\*STDERR, 'This is: ', "queue(\\*STDERR, string, string)\n"); sub queue { my $Q = ref $_[0] eq 'GLOB' ? shift : \*STDOUT; my @list = @_; $Q->print(@list); return; }

    Output everything:

    $ ./pm_11152428_opt_arg.pl This is: queue(string) This is: queue(string, string) This is: queue(\*STDERR, string) This is: queue(\*STDERR, string, string)

    Output just STDOUT:

    $ ./pm_11152428_opt_arg.pl 2>/dev/null This is: queue(string) This is: queue(string, string)

    Output just STDERR:

    $ ./pm_11152428_opt_arg.pl 1>/dev/null This is: queue(\*STDERR, string) This is: queue(\*STDERR, string, string)

    Of course, if $job were a globref, you'd have to rethink that strategy; however, your question suggests that's not the case.

    If you want "looks the same", then that is called indirect object syntax (as ++LanX has already pointed out) and there are reasons not to use this form.

    perlobj: Invoking Class Methods: Indirect Object Syntax
    The first line of this section starts with the emboldened text: "Outside of the file handle case, use of this syntax is discouraged as it can confuse the Perl interpreter. ..."
    perl5360delta: use v5.36
    This syntax is disabled by default in v5.36.0. You can read the full text but the key elements are: "The 5.36 bundle also disables the features indirect ... will forbid ... the use of "indirect" method calls ... that cause more trouble than they're worth."

    In line with the documentation, I strongly recommend that you do not use this syntax.

    [Aside: As you can no doubt see, <code>...</code> tags do not work in node titles. Please remove them from your post's title. Thankyou.]

    — Ken

Re: Optional Subroutine Arguments Like in <code>print FILEHANDLE LIST</code>
by tybalt89 (Monsignor) on May 27, 2023 at 23:51 UTC

    Something like this ?

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11152428 use warnings; my $job = 'something'; sub queue { print "simple queue: '@_'\n"; } sub OTHER_QUEUE::queue { shift; print "a different queue: '@_'\n"; } queue $job; queue OTHER_QUEUE $job;

    Outputs:

    simple queue: 'something' a different queue: 'something'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-20 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found