Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Creating string "user@host" elegantly

Reply
Thread Tools

Creating string "user@host" elegantly

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      11-11-2006
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:

http://www.velocityreviews.com/forums/(E-Mail 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?

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-11-2006
(E-Mail Removed) wrote:
>
> 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 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-11-2006
(E-Mail Removed) wrote:
> 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 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;

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      11-11-2006
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

(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
> > 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 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;
>
> --
> -------------------- http://NewsReader.Com/ --------------------
> Usenet Newsgroup Service $9.95/Month 30GB


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-11-2006

Quoth (E-Mail 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 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

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
*(E-Mail Removed) *
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      11-11-2006
(E-Mail 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 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; "@_" }


--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to elegantly get the enum code from its string type thomas C++ 73 04-22-2010 05:13 PM
Chopping the beginning of a string elegantly francisrammeloo@hotmail.com Ruby 12 08-01-2005 01:35 PM
Re: properties + types, implementing meta-class desciptors elegantly? Michele Simionato Python 15 07-25-2003 11:48 PM
Re: properties + types, implementing meta-class desciptors elegantly? Michael Hudson Python 3 07-22-2003 02:06 AM
Active menus: how can I do this elegantly? Ken Fine ASP .Net 0 07-19-2003 04:30 PM



Advertisments