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

Subroutine Understanding please?

Expand Messages
  • ericvia2002
    Dear kind Perl friends, I am trying to learn about using Perl subroutines, and have ran into a snag and need someone to please help me understand what I m
    Message 1 of 6 , Dec 5, 2001
    • 0 Attachment
      Dear kind Perl friends,

      I am trying to learn about using Perl subroutines, and have ran
      into a snag and need someone to please help me understand what I'm
      doing wrong.

      I have a little Perl script called "comify.pl" that I used
      frequently - it simply comifies number, for example 123456 will
      become "123,456"

      So rather than CALLING this neat perl script from a shell script -
      I of course want to use it WITHIN a Perl script. So I did:

      #!/usr/bin/perl -w
      sub comify
      {
      $number = $ARGV[0];
      local $_ = shift;
      1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
      return $_;
      }

      Now where I'm lost is - I don't know how to properly CALL this
      subroutine - I mean I have learned to call a subroutine I can use

      &comify

      But I then need to feed it what $ARGV[0] is so it will comify it -
      so I know I need to somehow feed the subroutine a number to be $ARGV
      [0] - but how? If I say in my script: "&comify.pl 123456" the
      script gets mad and complains that it doens't know what what that
      number is!

      How can I easily call upon this subroutine, within a Perl script -
      to have it recognize what I want to do? I can see that it
      wants "$number" - and so I try adding to the script:

      $number=123456
      &comify($number);

      But that returns an uninitialized value from line 6.

      Any help and/or guidance, as usual, will be greatly appreciated!

      EV
    • Godwin Stewart
      On Wed, 05 Dec 2001 19:43:24 -0000, ericvia2002 ... You don t feed a subroutine arguments the way you feed them to a program via the command line. Parameters
      Message 2 of 6 , Dec 5, 2001
      • 0 Attachment
        On Wed, 05 Dec 2001 19:43:24 -0000, "ericvia2002"
        <coolhand.luke@...> wrote:

        > #!/usr/bin/perl -w
        > sub comify
        > {
        > $number = $ARGV[0];
        > local $_ = shift;
        > 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
        > return $_;
        > }

        You don't feed a subroutine arguments the way you feed them to a program via
        the command line.

        Parameters are passed to a sub in an array called @_ which can be shifted to
        extract each individual parameter.

        So, your sub should look like this:

        sub comify {
        $NUMBER = shift; # shorthand for shift(@_)
        1 while $NUMBER =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
        return $NUMBER;
        }

        If you now want to call this sub from your program and print the result:

        #!/usr/bin/perl -w

        print comify($ARGV[0])."\n";

        HTH's

        --
        ____________________________________________
        | G. Stewart -- gstewart@... |
        | Port de Pontille, FR-37500 CHINON, FRANCE. |
        ____________________________________________
      • ericvia2002
        ... program via ... THAT was my problem then! ... shifted to ... THAT explains my question to Denny and his approach in a previous post - very good! ... Great!
        Message 3 of 6 , Dec 5, 2001
        • 0 Attachment
          --- In perl-beginner@y..., Godwin Stewart <gstewart@s...> wrote:

          > You don't feed a subroutine arguments the way you feed them to a
          program via
          > the command line.

          THAT was my problem then!

          > Parameters are passed to a sub in an array called @_ which can be
          shifted to
          > extract each individual parameter.

          THAT explains my question to Denny and his approach in a previous
          post - very good!

          > So, your sub should look like this:
          >
          > sub comify {
          > $NUMBER = shift; # shorthand for shift(@_)
          > 1 while $NUMBER =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
          > return $NUMBER;
          > }

          Great!

          > If you now want to call this sub from your program and print the
          result:

          > print comify($ARGV[0])."\n";

          THIS one is confusing me - I am confused as to what this means - do
          you mean instead of "$ARGV[0]" in the above line replace with a
          number? print comify(123456)."\n";

          That is giving me a uninitialed value error.

          ?? I'm sorry to be a bother - but I want to really understand what
          you're teaching me.

          Eric
        • ericvia2002
          ... program via ... THAT was my problem then! ... shifted to ... THAT explains my question to Denny and his approach in a previous post - very good! ... Great!
          Message 4 of 6 , Dec 5, 2001
          • 0 Attachment
            > You don't feed a subroutine arguments the way you feed them to a
            program via
            > the command line.

            THAT was my problem then!

            > Parameters are passed to a sub in an array called @_ which can be
            shifted to
            > extract each individual parameter.

            THAT explains my question to Denny and his approach in a previous
            post - very good!

            > So, your sub should look like this:
            >
            > sub comify {
            > $NUMBER = shift; # shorthand for shift(@_)
            > 1 while $NUMBER =~ s/^([-+]?\d+)(\d{3})/$1,$2/;
            > return $NUMBER;
            > }

            Great!

            > If you now want to call this sub from your program and print the
            result:

            > print comify($ARGV[0])."\n";

            THIS one is confusing me - I am confused as to what this means - do
            you mean instead of "$ARGV[0]" in the above line replace with a
            number? print comify(123456)."\n";

            That is giving me a uninitialed value error.

            ?? I'm sorry to be a bother - but I want to really understand what
            you're teaching me.

            Eric
          • Godwin Stewart
            On Wed, 05 Dec 2001 20:28:08 -0000, ericvia2002 ... Odd. I just quickly typed this into a file I named comify.pl: #!/usr/bin/perl print comify(22222). n ;
            Message 5 of 6 , Dec 5, 2001
            • 0 Attachment
              On Wed, 05 Dec 2001 20:28:08 -0000, "ericvia2002"
              <coolhand.luke@...> wrote:

              > > print comify($ARGV[0])."\n";
              >
              > THIS one is confusing me - I am confused as to what this means - do
              > you mean instead of "$ARGV[0]" in the above line replace with a
              > number? print comify(123456)."\n";
              >
              > That is giving me a uninitialed value error.

              Odd.

              I just quickly typed this into a file I named comify.pl:

              #!/usr/bin/perl

              print comify(22222)."\n";

              exit(0);


              sub comify {
              $_ = shift;
              1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
              return $_;
              }

              # --8<---8<---8< end of program 8<---8<---8<---8<---


              $ ./comify.pl
              22,222

              Then I modified the line with "print" in it so that it became:

              print comify($ARGV[0])."\n";


              $ ./comify.pl 123456
              123,456

              So, both work for me.

              As for the "($_) = @_;" from Denny, what this does is it assigns the
              elements in the @_ array into the list of scalars comprising just one
              element: $_

              In other words, it puts the first element of @_ in $_

              The effect is exactly the same as "$_ = shift" or "$_ = $_[0]" but perhaps a
              bit clearer.

              "There's more than one way to do it" is the Perl slogan. Although, at the
              very beginning I was more inclined to say "There's more than one way to
              scr*w it" ;o)

              --
              ____________________________________________
              | G. Stewart -- gstewart@... |
              | Port de Pontille, FR-37500 CHINON, FRANCE. |
              ____________________________________________
            • ericvia2002
              ... [snippage] ... That s what I thought - but I ve not gotten back in to play around with it more. ... [snip] ... And there it is! Hmmm! Let me look again
              Message 6 of 6 , Dec 5, 2001
              • 0 Attachment
                --- In perl-beginner@y..., Godwin Stewart <gstewart@s...> wrote:

                [snippage]

                > > That is giving me a uninitialed value error.

                > Odd.

                That's what I thought - but I've not gotten back in to play around
                with it more.

                > I just quickly typed this into a file I named comify.pl:

                [snip]

                > $ ./comify.pl
                > 22,222
                >
                > Then I modified the line with "print" in it so that it became:
                >
                > print comify($ARGV[0])."\n";
                >
                >
                > $ ./comify.pl 123456
                > 123,456

                And there it is! Hmmm! Let me look again at it - could be
                something funky I did wrong.

                > So, both work for me.

                Thank you for testing that! You're very kind.

                > As for the "($_) = @_;" from Denny, what this does is it assigns the
                > elements in the @_ array into the list of scalars comprising just
                one
                > element: $_
                >
                > In other words, it puts the first element of @_ in $_
                >
                > The effect is exactly the same as "$_ = shift" or "$_ = $_[0]" but
                perhaps a
                > bit clearer.

                VERY good info to know - perfect!

                I also found a link out there that's explaining just what we're
                talking about - so I'm reading that:

                http://www.tlc.perlarchive.com/9910/01d.shtml

                > "There's more than one way to do it" is the Perl slogan. Although,
                at the
                > very beginning I was more inclined to say "There's more than one
                way to
                > scr*w it" ;o)

                HAHA! I'm with ya on that one!

                Again thank you for your wonderful assistance and time!

                Eric
              Your message has been successfully submitted and would be delivered to recipients shortly.