Creating string "user@host" elegantly

U

usenet

Greetings.

I have two variable, $host and $user (and $user could be undef).

I wish to construct a string such as would be used in an FTP command,
such as:

(e-mail address removed)

However, if $user is undefined, the string should simply say

hostname.example.com

I could do this:
my $foo = ($user) ? "$user\@$host" : $host;
but that seems a bit redundant (I had to type "$user" twice and "$host"
twice).

I could avoid typing "$host" twice with something like:
my $foo = "$user\@" if $user;
$foo .= $host;
but I'm not sure I like that any better...

Does anybody have a more elegant suggestion?
 
J

John W. Krahn

I have two variable, $host and $user (and $user could be undef).

I wish to construct a string such as would be used in an FTP command,
such as:

(e-mail address removed)

However, if $user is undefined, the string should simply say

hostname.example.com

I could do this:
my $foo = ($user) ? "$user\@$host" : $host;
but that seems a bit redundant (I had to type "$user" twice and "$host"
twice).

I could avoid typing "$host" twice with something like:
my $foo = "$user\@" if $user;
$foo .= $host;
but I'm not sure I like that any better...

Does anybody have a more elegant suggestion?

( my $foo = "$user\@$host" ) =~ s/^\@//;




John
 
X

xhoster

Greetings.

I have two variable, $host and $user (and $user could be undef).

I wish to construct a string such as would be used in an FTP command,
such as:

(e-mail address removed)

However, if $user is undefined, the string should simply say

hostname.example.com

I could do this:
my $foo = ($user) ? "$user\@$host" : $host;
but that seems a bit redundant (I had to type "$user" twice and "$host"
twice).

I'd probably just do that, but stuff it in a sub if you don't like
the redundancy sitting out on the kitchen table.
I could avoid typing "$host" twice with something like:
my $foo = "$user\@" if $user;
$foo .= $host;
but I'm not sure I like that any better...

Does anybody have a more elegant suggestion?

Weird, but not redundant:

my $foo = join "@", grep defined, $user, $host;
 
P

paul

You may able to do that with this
my $user_login ;

if($user && $host) {
$user_login = $user.'@'.$host;
}
elsif ($host && !$user) {
$user_login = $host;
}
else {
undefine something
}

that may be more secured then...

PAUL
 
B

Ben Morrow

Quoth (e-mail address removed):
Greetings.

I have two variable, $host and $user (and $user could be undef).

I wish to construct a string such as would be used in an FTP command,
such as:

(e-mail address removed)

However, if $user is undefined, the string should simply say

hostname.example.com

I could do this:
my $foo = ($user) ? "$user\@$host" : $host;
but that seems a bit redundant (I had to type "$user" twice and "$host"
twice).

I could avoid typing "$host" twice with something like:
my $foo = "$user\@" if $user;
$foo .= $host;
but I'm not sure I like that any better...

Well, the obvious is

my $foo = ($user ? "$user\@" : '') . $host;

which can be simplified to

my $foo = ($user && "$user\@") . $host;

if you don't mind undefined warnings and losing a user called '0'.

I would probably go with

(my $foo = "$user\@$host") =~ s/^\@//;

, though, or perhaps

my $uri = URI->new('ftp://');
$uri->userinfo($user);
$uri->host($host);
my $foo = $uri->authority;

Ben
 
D

Dr.Ruud

(e-mail address removed) schreef:
I have two variable, $host and $user (and $user could be undef).

I wish to construct a string such as would be used in an FTP command,
such as:

(e-mail address removed)

However, if $user is undefined, the string should simply say

hostname.example.com

I could do this:
my $foo = ($user) ? "$user\@$host" : $host;
but that seems a bit redundant (I had to type "$user" twice and
"$host" twice).

I could avoid typing "$host" twice with something like:
my $foo = "$user\@" if $user;
$foo .= $host;
but I'm not sure I like that any better...

Does anybody have a more elegant suggestion?

my $foo = (defined($user) ? "$user\@" : '') . $host ;

do {local ($", @_) = ("@"); defined($_) and push @_, $_ for $user,
$host; "@_" }
;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top