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

Re: [PBML] returning arrays and hashes from a subroutine

Expand Messages
  • Charles K. Clarkson
    ... When I started with perl last year, almost everything I managed to get working was an example of poor coding. A subroutine should be able to be pulled out
    Message 1 of 12 , May 2, 2002
    • 0 Attachment
      "markveerman" <mark@...> wrote:

      : Thanks Charles - Are you telling me that even though it
      : works it's not good code? :-).

      When I started with perl last year, almost everything I
      managed to get working was an example of poor coding.
      A subroutine should be able to be pulled out of the
      program it was written in and placed in another and run
      with minimal adjustment.

      When writing a subroutine, think of it as if a manager
      just handed you an assignment. You have no idea how this
      sub will be used in the program. You know what it should
      accept, what it should return, and what it should do.

      You can cut some corners for short programs, but the
      larger and more complex your script gets the more you
      need to follow this or a more refined approach to
      building subroutines.

      : As if it's not totally obvious - I'm a novice.

      A novice what?

      : > So this is what Japhy meant. Shame on you Japhy, I
      : > can't believe you'd let Mark do it this way. :)
      :
      : In defense of Japhy, today was the first day I've ever
      : written a sub routine, so the issues with scoping and
      : local/global variables is entirely new.

      Is was meant as a joke. I knew Japhy didn't mean want
      you thought he did.


      HTH,

      Charles K. Clarkson
      --
      Head Bottle Washer,
      Clarkson Energy Homes, Inc.

      CJ Web Work - Domains for Real Estate Investors.
      E Pluribus Unum -- One from many.
    • markveerman
      Thanks Charles - Are you telling me that even though it works it s not good code? :-). As if it s not totally obvious - I m a novice. /mark ... In defense of
      Message 2 of 12 , May 2, 2002
      • 0 Attachment
        Thanks Charles - Are you telling me that even though it works it's
        not good code? :-). As if it's not totally obvious - I'm a novice.
        /mark

        > So this is what Japhy meant. Shame on you Japhy, I
        > can't believe you'd let Mark do it this way. :)

        In defense of Japhy, today was the first day I've ever written a sub
        routine, so the issues with scoping and local/global variables is
        entirely new.

        Thanks to you both for the assistance.
        /mark
      • Jeff 'japhy' Pinyan
        ... Gaaah! No, Mark misunderstood my code, Charles. Mark, the return() function does not tell Perl that variables you used in your function can be used
        Message 3 of 12 , May 2, 2002
        • 0 Attachment
          On May 2, Charles K. Clarkson said:

          >"markveerman" <mark@...> wrote:
          >
          >: I figured it out! Although, I don't really understand the, but
          >: here's what it was:
          >:
          >: Below within the sub I originally had: my @fieldnames = $db-
          >: >FieldNames(); return @fieldnames; When I removed the word "my",
          >: @fieldnames was returned correctly. Also, I didn't realize you could
          >: call the function like @array = function() - so that was helpful too.
          >:
          >: thanks for your help japhy!
          >
          > So this is what Japhy meant. Shame on you Japhy, I
          >can't believe you'd let Mark do it this way. :)

          Gaaah! No, Mark misunderstood my code, Charles.

          Mark, the return() function does not tell Perl that variables you used in
          your function can be used OUTSIDE the function. It does nothing of the
          sort. All it does is tell Perl that it can put the VALUES of those
          variables in place you can get them from; specifically, from the return
          value of the function:

          sub cube {
          my $n = shift;
          return $n ** 3;
          }

          $big = cube(1234);

          The function cube() RETURNS ONE VALUE. If you don't use the return value
          somehow (like storing it in a variable, or printing it) then it
          disappears.

          Here are some examples to see if it becomes clear to you.

          sub foobar {
          my @bah = (1, 2, 3, 4);
          return @bah;
          }

          my @bah = (5, 6);
          foobar();
          print "@bah\n"; # 5 6

          ###########################

          sub foobar {
          my @bah = (1, 2, 3, 4);
          }

          my @bah = (5, 6);
          foobar();
          print "@bah\n"; # 5 6

          ###########################

          sub foobar {
          my @bah = (1, 2, 3, 4);
          return @bah;
          }

          my @bah = (5, 6);
          @bah = foobar();
          print "@bah\n"; # 1 2 3 4

          ###########################

          sub foobar {
          my @bah = (1, 2, 3, 4);
          }

          my @bah = (5, 6);
          @bah = foobar(); # foobar() returns the last thing evaluated, @bah
          print "@bah\n"; # 1 2 3 4 (but only because you're lucky!)

          ###########################

          sub foobar {
          @bah = (1, 2, 3, 4); # THIS SETS THE OUTSIDE @bah (bad idea)
          return @bah;
          }

          my @bah = (5, 6);
          foobar();
          print "@bah\n"; # 1 2 3 4

          ###########################

          sub foobar {
          my @bah = (1, 2, 3, 4); # DITTO
          }

          my @bah = (5, 6);
          foobar();
          print "@bah\n"; # 5 6

          --
          Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
          RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
          ** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
          <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
          [ I'm looking for programming work. If you like my work, let me know. ]
        • Franki
          well you just taught me something as well.. I hadn t really given it much thought, but now that I have, its kinda obvious and I am ashamed of myself for not
          Message 4 of 12 , May 3, 2002
          • 0 Attachment
            well you just taught me something as well..

            I hadn't really given it much thought, but now that I have, its kinda
            obvious and I am ashamed of myself for not realising it..

            I had been wondering why not all of my "return" code works...

            Thankyou..


            regards

            Frank

            -----Original Message-----
            From: Jeff 'japhy' Pinyan [mailto:japhy@...]
            Sent: Friday, 3 May 2002 10:16 AM
            To: perl-beginner@yahoogroups.com
            Subject: Re: [PBML] returning arrays and hashes from a subroutine


            On May 2, Charles K. Clarkson said:

            >"markveerman" <mark@...> wrote:
            >
            >: I figured it out! Although, I don't really understand the, but
            >: here's what it was:
            >:
            >: Below within the sub I originally had: my @fieldnames = $db-
            >: >FieldNames(); return @fieldnames; When I removed the word "my",
            >: @fieldnames was returned correctly. Also, I didn't realize you could
            >: call the function like @array = function() - so that was helpful too.
            >:
            >: thanks for your help japhy!
            >
            > So this is what Japhy meant. Shame on you Japhy, I
            >can't believe you'd let Mark do it this way. :)

            Gaaah! No, Mark misunderstood my code, Charles.

            Mark, the return() function does not tell Perl that variables you used in
            your function can be used OUTSIDE the function. It does nothing of the
            sort. All it does is tell Perl that it can put the VALUES of those
            variables in place you can get them from; specifically, from the return
            value of the function:

            sub cube {
            my $n = shift;
            return $n ** 3;
            }

            $big = cube(1234);

            The function cube() RETURNS ONE VALUE. If you don't use the return value
            somehow (like storing it in a variable, or printing it) then it
            disappears.

            Here are some examples to see if it becomes clear to you.

            sub foobar {
            my @bah = (1, 2, 3, 4);
            return @bah;
            }

            my @bah = (5, 6);
            foobar();
            print "@bah\n"; # 5 6

            ###########################

            sub foobar {
            my @bah = (1, 2, 3, 4);
            }

            my @bah = (5, 6);
            foobar();
            print "@bah\n"; # 5 6

            ###########################

            sub foobar {
            my @bah = (1, 2, 3, 4);
            return @bah;
            }

            my @bah = (5, 6);
            @bah = foobar();
            print "@bah\n"; # 1 2 3 4

            ###########################

            sub foobar {
            my @bah = (1, 2, 3, 4);
            }

            my @bah = (5, 6);
            @bah = foobar(); # foobar() returns the last thing evaluated, @bah
            print "@bah\n"; # 1 2 3 4 (but only because you're lucky!)

            ###########################

            sub foobar {
            @bah = (1, 2, 3, 4); # THIS SETS THE OUTSIDE @bah (bad idea)
            return @bah;
            }

            my @bah = (5, 6);
            foobar();
            print "@bah\n"; # 1 2 3 4

            ###########################

            sub foobar {
            my @bah = (1, 2, 3, 4); # DITTO
            }

            my @bah = (5, 6);
            foobar();
            print "@bah\n"; # 5 6

            --
            Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
            RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
            ** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
            <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
            [ I'm looking for programming work. If you like my work, let me know. ]



            Unsubscribing info is here:
            http://help.yahoo.com/help/us/groups/groups-32.html

            Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
          Your message has been successfully submitted and would be delivered to recipients shortly.