Loading ...
Sorry, an error occurred while loading the content.

no shift EXPR?

Expand Messages
  • Andrew Hazen Schlaikjer
    Hello, I ve been using perl for a while now and thought I new mostly everything about its basic functions, but just today I found something I couldn t explain:
    Message 1 of 4 , Feb 3, 2003
    • 0 Attachment
      Hello,

      I've been using perl for a while now and thought I new mostly everything
      about its basic functions, but just today I found something I couldn't
      explain:

      shift function does not allow for evaluation of a rhs expression, i.e.

      shift ARRAY
      shift

      but no

      shift EXPR

      Why is this??

      Here's a more thorough example:

      <code>

      sub get_list {
      return (1, 2, 3);
      }

      my $zeroth;
      $zeroth = shift get_list(); # does not work
      $zeroth = shift @{ \( get_list() ) }; # works

      </code>

      Is this something to do with efficiency of shift implementation?? Sorry,
      I should just dive into perl source to find answer myself, but a little
      busy and thought someone might be able to give a quick answer..
    • Jenda Krynicky
      From: Andrew Hazen Schlaikjer ... It does. See below. ... The whole point is that shift() works on an ARRAY, not a list :-) Try this: @a =
      Message 2 of 4 , Feb 3, 2003
      • 0 Attachment
        From: Andrew Hazen Schlaikjer <ahs37@...>
        > I've been using perl for a while now and thought I new mostly
        > everything about its basic functions, but just today I found something
        > I couldn't explain:
        >
        > shift function does not allow for evaluation of a rhs expression, i.e.

        It does. See below.

        > Here's a more thorough example:
        >
        > <code>
        >
        > sub get_list {
        > return (1, 2, 3);
        > }
        >
        > my $zeroth;
        > $zeroth = shift get_list(); # does not work
        > $zeroth = shift @{ \( get_list() ) }; # works
        >
        > </code>

        The whole point is that shift() works on an ARRAY, not a list :-)

        Try this:

        @a = (9,8,7,6,5); @b = (10,20,30);

        print shift( 1 < 2 ? @a : @b),"\n";
        print shift( 1 < 2 ? @a : @b),"\n";
        print shift( 1 < 0 ? @a : @b),"\n";
        # tested under Perl 5.8 (ActiveState build 804)
        # and 5.6.1 (ActiveState build 631)

        Jenda
        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      • aschlaikjer <ahs37@columbia.edu>
        Haha! Oh no! you ve blown my mind! You mean that ARRAY and LIST types are different in this context? I ve been operating for so long under the impression that
        Message 3 of 4 , Feb 3, 2003
        • 0 Attachment
          Haha! Oh no! you've blown my mind! You mean that ARRAY and LIST types
          are different in this context? I've been operating for so long under
          the impression that they were indistinguishable as rhs values... So
          why is this not the case?

          Thanks for the example, btw.

          --Andy

          --- In perl-beginner@yahoogroups.com, "Jenda Krynicky" <Jenda@K...> wrote:
          > From: Andrew Hazen Schlaikjer <ahs37@c...>
          > > I've been using perl for a while now and thought I new mostly
          > > everything about its basic functions, but just today I found something
          > > I couldn't explain:
          > >
          > > shift function does not allow for evaluation of a rhs expression, i.e.
          >
          > It does. See below.
          >
          > > Here's a more thorough example:
          > >
          > > <code>
          > >
          > > sub get_list {
          > > return (1, 2, 3);
          > > }
          > >
          > > my $zeroth;
          > > $zeroth = shift get_list(); # does not work
          > > $zeroth = shift @{ \( get_list() ) }; # works
          > >
          > > </code>
          >
          > The whole point is that shift() works on an ARRAY, not a list :-)
          >
          > Try this:
          >
          > @a = (9,8,7,6,5); @b = (10,20,30);
          >
          > print shift( 1 < 2 ? @a : @b),"\n";
          > print shift( 1 < 2 ? @a : @b),"\n";
          > print shift( 1 < 0 ? @a : @b),"\n";
          > # tested under Perl 5.8 (ActiveState build 804)
          > # and 5.6.1 (ActiveState build 631)
          >
          > Jenda
          > ===== Jenda@K... === http://Jenda.Krynicky.cz =====
          > When it comes to wine, women and song, wizards are allowed
          > to get drunk and croon as much as they like.
          > -- Terry Pratchett in Sourcery
        • Jenda Krynicky
          From: aschlaikjer ... I don t think I can explain the exact difference myself. Try if perldoc -q What is the
          Message 4 of 4 , Feb 3, 2003
          • 0 Attachment
            From: "aschlaikjer <ahs37@...>" <ahs37@...>
            > Haha! Oh no! you've blown my mind! You mean that ARRAY and LIST types
            > are different in this context? I've been operating for so long under
            > the impression that they were indistinguishable as rhs values... So
            > why is this not the case?
            >
            > Thanks for the example, btw.
            >
            > --Andy

            I don't think I can explain the exact difference myself. Try if

            perldoc -q "What is the difference between a list and an array?"

            makes sense. :-)

            Jenda
            ===== Jenda@... === http://Jenda.Krynicky.cz =====
            When it comes to wine, women and song, wizards are allowed
            to get drunk and croon as much as they like.
            -- Terry Pratchett in Sourcery
          Your message has been successfully submitted and would be delivered to recipients shortly.