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


In reply to Re: Optional Subroutine Arguments Like in print FILEHANDLE LIST by kcott
in thread Optional Subroutine Arguments Like in `print FILEHANDLE LIST` by mikkoi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.