(Here's an answer to the updated question.)
why do we need to create variables like data and length when the arguments will be passed from command line.
Those were just an example. Instead, you'd call your variables something like $user, $passwd, etc.
use Getopt::Long ();
sub usage {
my $message = $_[0];
if (defined $message && length $message) {
$message .= "\n"
unless $message =~ /\n$/;
}
my $command = $0;
$command =~ s#^.*/##;
print STDERR (
$message,
"usage: $command -u username [-p password] -s hostname:port\n" .
" ...\n" .
" ...\n" .
" ...\n"
);
die("\n")
}
my $user;
my $passwd;
my $host;
my $port;
Getopt::Long::GetOptions(
'u=s' => \$user,
'p=s' => \$passwd,
's=s' => sub {
local *_ = \$_[1];
/^([^:]+):(\d+)$/
or die("Invalid format for option s.\n");
$host = $1;
$port = $2;
},
)
or usage("Invalid commmand line options.");
usage("The user name must be specified.")
unless defined $user;
usage("The server hostname and port must be specified.")
unless defined $host;
if (!defined $passwd) {
# Prompt for password here...
}