in reply to Simple Subroutine Question

I would strongly recommend only accepting one form. If you want to accept both, have 2 different functions. That way if you ever want to add a third argument or fourth argument (such as, oh, a machine to connect to, a port, options for an SSH tunnel, etc) you'll be in good shape.

If you disregard said advice, you could do it as follows:

sub ssh_connect { my ($self, $user, $pass) = @_; if (3 < @_) { (undef, my %args) = @_; $user = $args{user} or confess("No user"); $pass = $args{pass} or confess("No pass"); } print "$user and $pass\n"; }
Alternately you could make the two forms be
$foo->ssh_connect($user, $password); $foo->ssh_connect({user => $user, pass => $password});
and dispatch on whether there are fewer than 3 arguments. This is actually a saner way to go, but it does require {}.

Replies are listed 'Best First'.
Re^2: Simple Subroutine Question
by walkingthecow (Friar) on Aug 01, 2009 at 01:00 UTC
    If choosing between the two, what do the PerlMonks suggest is a better way of doing this? I'd like it to be EASY to write and EASY to read... So, I was thinking the user=> would be good, but there are MANY functions in this bad boy and not sure if I should have them all use that method...

    An example of a lot of arguments is below:
    foo->adduser(id=>$id, uid=>$uid, prgrp=>$pgrp, sgrps=>$sgrps, gcos=>$gcos, expire=>$expire)... I mean, is that ridiculous??