Creating string "user@host" elegantly

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Nov 11, 2006.

  1. Guest

    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:



    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)
    , Nov 11, 2006
    #1
    1. Advertising

  2. 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:
    >
    >
    >
    > 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
    John W. Krahn, Nov 11, 2006
    #2
    1. Advertising

  3. Guest

    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:
    >
    >
    >
    > 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
    , Nov 11, 2006
    #3
  4. paul Guest

    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

    wrote:
    > 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:
    > >
    > >
    > >
    > > 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
    paul, Nov 11, 2006
    #4
  5. Ben Morrow Guest

    Quoth :
    > 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:
    >
    >
    >
    > 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)
    * *
    Ben Morrow, Nov 11, 2006
    #5
  6. Dr.Ruud Guest

    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:
    >
    >
    >
    > 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."
    Dr.Ruud, Nov 11, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Fine
    Replies:
    0
    Views:
    326
    Ken Fine
    Jul 19, 2003
  2. Michael Hudson
    Replies:
    3
    Views:
    362
    Bengt Richter
    Jul 22, 2003
  3. Michele Simionato
    Replies:
    15
    Views:
    546
  4. thomas
    Replies:
    73
    Views:
    1,716
    Keith H Duggar
    Apr 22, 2010
  5. Replies:
    12
    Views:
    162
Loading...

Share This Page