0

I've tried two similar bits of syntax. This first one works:

   my $obj = PI::something::ObjectManipulator->new();
   $obj->setValue('HELLO');
   my $objList = [$object];

This, however, doesn't:

  my $objList= [];
  foreach my $value (@values) {
    my $obj = PI::something::ObjectManipulator->new();
    $obj->setValue($value);
    push @$objList, $obj;
  };

What is the difference between these two way of doing things? Why doesn't the second work? (By not work, it seems to be that $objList is still empty at the end of the code.)

1
  • 5
    Looks good from here. Are you sure that @values is not an empty list? Commented Jun 16, 2016 at 18:55

1 Answer 1

4

Assuming my $objList = [$object]; is a typo for my $objList = [$obj];, and assuming, my @values = 'HELLO';, there are no differences.

Assuming you meant "@$objList is still empty" when you said "$objList is still empty", that can only happen if the loop isn't entered (i.e. @values is empty).

Sign up to request clarification or add additional context in comments.

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.