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

Dear monks, Can someone please explain to me why the following two things differ? Many thanks!
push (@array, $value); push @array, "$value ";

2005-01-20 Edited by Arunbear: Changed title from 'push ?', as per Monastery guidelines

Replies are listed 'Best First'.
Re: push with interpolated variable
by borisz (Canon) on Jan 19, 2005 at 19:26 UTC
    The first one push $value into the array. The second push a string containing $value with a trailing space into the array.
    Boris
Re: push with interpolated variable
by holli (Abbot) on Jan 19, 2005 at 19:24 UTC
    push (@array, $value);
    is the same as
    push @array, "$value";
    But when you say
    push @array, "$value ";
    you add a space at the end of $value.

    holli, regexed monk
      Also, by saying "$value" you are making perl treat $value in string context when it might not be at string but a number, or a boolean, or a reference, or an object, etc.
      push (@array, $value);
      is the same as
      push @array, "$value";
      It's not.
      #!/usr/bin/perl use strict; use warnings; my (@array, $value); @array = ("foo"); $value = []; push(@array, $value); $array[-1][0] = "bar"; print $array[-1][0], "\n"; @array = ("foo"); $value = []; push @array, "$value"; $array[-1][0] = "bar"; print $array[-1][0], "\n"; __END__ bar Can't use string ("ARRAY(0x818427c)") as an ARRAY ref while "strict re +fs" in use
        Indeed. I assumed $value contains a simple scalar (a string or a number). If $value contains a reference then "$value" is stringified.

        Thanks for the clarification, i never thought about that.

        holli, regexed monk