2

So, in Perl, I have an array within an object (so, a reference to an array), and I want to find the first value of that array.

I find myself using code like the following quite often:

my $server_ref = $self->{source_env}->{server};
my @servers = @$server_ref;
my $main_server = $servers[0];

That works, but I'm sure I could do this without all the intermediate lines and variables.

Can someone help me with the syntax?

2 Answers 2

6

Try:

my $main_server = $self->{source_env}->{server}->[0];
Sign up to request clarification or add additional context in comments.

1 Comment

All of the arrows after the first are optional. You can write it that way if you want, but you'll be more likely to find $self->{source_env}{server}[0] in code on CPAN.
0

Try $server_ref->[0], it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.