Well, you could reduce it to one line, and hide that line in another helper sub, I suppose. This method relies on the fact that if a sub is called with an '&' and without an explicit parameter list, it inherits @_ from its caller (see perlsub).

use strict; use warnings; package TestClass; sub _has_params { return @_ > 1 ? scalar(@_) - 1 : 0; } sub mytest { die "@_\n" if &_has_params; print "We're ok.\n"; } 1; package main; print "Testing with no parameters.\n\t"; TestClass->mytest( ); print "Testing with one parameter.\n\t"; TestClass->mytest( "Hello" );

There is one trick though, and I'm sure you're already dealing with it somehow: When a sub is called as a method (be it a class method or an object method) it will have one parameter passed into @_; either the class name or the self object ref. That's why in my "has_params()" subroutine, I only trigger a positive if there is more than one parameter.

When you run the snippet above you'll see that it passes the first test, and dies on the second one.


Dave


In reply to Re: Controlling the start and endings of method calls by davido
in thread Controlling the start and endings of method calls by Anonymous Monk

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.