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 1:26 PM
    • 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
      ... global ... you ... your ... you ... helps keep ... Okay, I think I understand what you re saying, however, If I don t return @fieldnames, then I can t do
      Message 2 of 12 , May 2 5:16 PM
      • 0 Attachment
        > No! You're not returning anything then! You're just setting a
        global
        > array. The concept of returning something from a function is that
        you
        > ACTUALLY send content FROM the function BACK to the program. Also,
        your
        > approach of removing 'my' makes your code possibly less safe, since
        you
        > might end up ruining some OTHER @fieldnames array. Using my()
        helps keep
        > your variables scoped properly.

        Okay, I think I understand what you're saying, however, If I don't
        return @fieldnames, then I can't do anything with them ... like print
        them out. I'm going to be using this function to query a db and
        return the results. I can't do anything with the results within the
        function, because I want it to remain generic enough so I can use it
        for all my db queries. So, in order for me to USE the info I get
        from the queries, I've got to return the data BACK to the program.

        > There's no point in returning @fieldnames. You're not returning it
        TO anything.

        I thought I was returning it to the program so I could then loop
        through it and print out the field names. Bear with me, I'm really
        trying to understand :-).
        /mark

        >
        > --
        > Jeff "japhy" Pinyan japhy@p...
        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. ]
      • 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 3 of 12 , May 2 5:41 PM
        • 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 4 of 12 , May 2 7:16 PM
          • 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 5 of 12 , May 3 2:58 AM
            • 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.