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

Wise Monks,

There's two minor issues that have been bugging me for some time now.

1. How do i get the paramater checking working? i declare a sub with ($$) but it raises no error when i pass it 1 or 3 params.
sub Blah($$) { my $var1 = shift; my $var2 = shift; } &Blah($0);

2. One is supposed to enter 1; at the end of a module. However it still works of i don't. (perl5.6.0 for Solaris 8). Is that normal?

Replies are listed 'Best First'.
Re: ($$) and 1;
by Tomte (Priest) on Aug 01, 2002 at 13:19 UTC
    foo($$); &foo($0);
    From perlsub:

    Not only does the & form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing

    regards,
    tomte
      Wow that helps! Thank you. I do find it a rather odd feature though. I always thought it was better to ise the &, turns out it isn't.
Re: ($$) and 1;
by crenz (Priest) on Aug 01, 2002 at 13:32 UTC
Re: ($$) and 1;
by fruiture (Curate) on Aug 01, 2002 at 13:33 UTC

    1.: get rid of the Perl4-ish '&func(args)', it has special meaning in Perl5. Read perlsub for the full truth.

    2.: One isn't supposed to enter 1;, one is spoosed to return a true value. If your module does not only declare subroutines, it probably returns a true value before:

    package Foo; our $x = 'need_this_on_loading'; sub bar {...}

    No '1;' is neccessary here, because the initialization of $x returns a true value, as every assignation returns the assigned value. see perlmod and perlop

    --
    http://fruiture.de
The magic "one"
by crenz (Priest) on Aug 01, 2002 at 13:43 UTC

    Sorry -- didn't see the second question at first.

    This refers to a mechanism that allows a module to indicate failure. I've never seen it used, though. If you use a package, the module is required to exit with a true value. If a declaration that evaluates to true happens to be at the end of your package, use will work just fine. (That's why it worked for you.) But to be on the safe side, you should add

    1;

    to the end of your modules, or else it might be broken once you decide to change the code. See also http://www.perldoc.com/perl5.6/pod/perlmod.html (search for "1;" in the page).

      i tend to use something more expressive than 1;, like
      $_ ^=~ { module => 'VCS::PVCS65', author => 'particle' };

      ~Particle *accelerates*