Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to do this without resorting to intermediate variables?
  • Comment on @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?

Replies are listed 'Best First'.
Re: @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?
by ikegami (Patriarch) on Nov 04, 2010 at 00:14 UTC

    Does it count if you don't declare the variables?

    my @addrs = sub { @_[4..$#_] }->( gethostbyname('www.yahoo.com') );
    my @addrs = sub { splice(@_, 4) }->( gethostbyname('www.yahoo.com') );

    Does it count if you only use anonymous variables?

    my @addrs = splice(@{[ gethostbyname('www.yahoo.com') ]}, 4);

    And one that doesn't use any extra variables.

    my @addrs = gethostbyname('www.yahoo.com'); splice(@addrs, 0, 4);
Re: @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?
by Anonymous Monk on Nov 03, 2010 at 22:44 UTC
    (undef, undef, undef, undef, @addrs) = gethostbyname('www.yahoo.com');
Re: @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?
by Anonymous Monk on Nov 04, 2010 at 02:09 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Net::hostent; use Socket; my @addrs = @{ gethostbyname('www.yahoo.com')->addr_list };
      That still uses an intermediate variable (not that I think it makes sense to request avoiding such).
        I don't understand how. I guess I have no idea what is an "intermediate variable"
Re: @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?
by locked_user sundialsvc4 (Abbot) on Nov 04, 2010 at 12:58 UTC

    Write a nice, three-line sub that performs the desired operation and returns what you want.

    This subroutine is now responsible for doing what you want, regardless of its current implementation.   The subroutine now clearly “does what you mean,” and anyone who looks at the code will see a subroutine-call to a meaningfully-named routine.

    “Always write code to say what, not how.”

Re: @addrs = (gethostbyname('www.yahoo.com'))[4..-1] ?
by kcott (Archbishop) on Nov 04, 2010 at 00:11 UTC

    One way would be to do something like this:

    @addrs = grep {state $i = 0; $i++ > 3 } gethostbyname('www.yahoo.com') +;

    -- Ken

      ow! Don't use that in a sub or a loop!

        And another way, if you needed to do this in a loop, would be:

        my $i = 0; @addrs = grep { $i++ > 3 } gethostbyname('www.yahoo.com');

        -- Ken