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

Re: [PBML] Passing Arrays Between Subs

Expand Messages
  • Jeff Soules
    ... No, he got it. You just didn t listen. ... You have three local variables defined in a subroutine with no explicit return. A perl sub with no explicit
    Message 1 of 7 , May 4, 2009
    • 0 Attachment
      > You missed the point entirely.

      No, he got it. You just didn't listen.

      > In a nutshell, I need to pass a number of parameters between subs.
      >
      > Getting back on track - Given the extremely simplified example below. How
      > does one get Larry, Moe and Curly out of sub_a and into sub_b?

      You have three local variables defined in a subroutine with no
      explicit return. A perl sub with no explicit return will by default
      return the last evaluated value. If you want that sub to return an
      array of values, you'd need to explicitly return the multiple values.


      Best,
      Jeff


      On Mon, May 4, 2009 at 3:54 AM, essential quint
      <quintessential1@...> wrote:
      >
      >
      >
      > Hello Randall:
      >
      > You missed the point entirely.
      >
      > In a nutshell, I need to pass a number of parameters between subs.
      >
      > Getting back on track - Given the extremely simplified example below. How
      > does one get Larry, Moe and Curly out of sub_a and into sub_b?
      >
      > #!/usr/bin/perl -Tw
      >
      > use strict;
      > use CGI;
      > use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
      >
      > ### SETUP MAIN DRIVER ###
      >
      > sub_a ();
      > my @values = sub_a ();
      > sub_b (@values);
      >
      > ### RUN SUBS ###
      >
      > sub sub_a {
      > my $x = "Larry";
      > my $y = "Moe";
      > my $z = "Curly";
      > }
      >
      > sub sub_b {
      >
      > my @values = shift;
      > my $i;
      >
      > print "Content-Type: text/html\n\n";
      >
      > foreach $i (@values) {
      > print "$i\n\n";
      > }
      > }
      >
      > Thanks,
      >
      > quint
    • Jeff Pinyan
      On Mon, May 4, 2009 at 3:54 AM, essential quint
      Message 2 of 7 , May 4, 2009
      • 0 Attachment
        On Mon, May 4, 2009 at 3:54 AM, essential quint <quintessential1@...
        > wrote:

        >
        > Hello Randall:
        >

        His name is Randal. (Why does everyone add an 'l' to the end of his name?)


        > You missed the point entirely. In a nutshell, I need to pass a number of
        > parameters between subs.
        >

        No, he didn't. In order to do what you want, you need to *return* the
        values from sub_a(), which you don't do.


        > sub sub_a {
        > my $x = "Larry";
        > my $y = "Moe";
        > my $z = "Curly";
        > }
        >

        There is NO reason for this function to return three values. As it is
        written, it will only return the last one, 'Curly'.


        > sub sub_b {
        >
        > my @values = shift;
        >

        And even if sub_a() was written correctly, the @values array would only have
        a single element, because the shift() function only returns ONE value.

        Consider re-reading the *perlsub* (perl subroutines) documentation. Pay
        closer attention to the return() function and the built-in @_ variable.

        --
        The Cross Reference - http://thecrossreference.blogspot.com/
        Critical Mass (The Science of the Liturgy) -
        http://romanliturgy.blogspot.com/

        [Mary said,] "Do whatever he tells you." ~ John 2:5


        [Non-text portions of this message have been removed]
      • merlyn@stonehenge.com
        ... Jeff His name is Randal. (Why does everyone add an l to the end of his Jeff name?) Sadly, it s the more common spelling. I blame my parents. :) Mom
        Message 3 of 7 , May 4, 2009
        • 0 Attachment
          >>>>> "Jeff" == Jeff Pinyan <japhy.734@...> writes:

          Jeff> His name is Randal. (Why does everyone add an 'l' to the end of his
          Jeff> name?)

          Sadly, it's the more common spelling. I blame my parents. :)

          Mom explains the single L as "but that's how it was spelled in the book of
          baby names we consulted". Therefore, I argue, it was a typo. :)

          --
          Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
          See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
        • yehezkielbs
          You need to change sub_a to : sub sub_a { my $x = Larry ; my $y = Moe ; my $z = Curly ; return ($x, $y, $z); } and sub_b to : sub sub_b { my @values = @_;
          Message 4 of 7 , May 4, 2009
          • 0 Attachment
            You need to change sub_a to :

            sub sub_a {
            my $x = "Larry";
            my $y = "Moe";
            my $z = "Curly";

            return ($x, $y, $z);
            }

            and sub_b to :

            sub sub_b {
            my @values = @_;

            ...
            }


            Regards,
            Yehezkiel

            --- In perl-beginner@yahoogroups.com, essential quint <quintessential1@...> wrote:
            >
            >
            > Hello Everyone:
            >
            > Can someone explain/correct what I have done wrong here? I want it to output all values (Larry, Moe and Curly), but it's only giving me the last one (Curly).
            >
            > #!/usr/bin/perl -Tw
            >
            > use strict;
            > use CGI;
            > use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
            >
            > ### SETUP MAIN DRIVER ###
            >
            > sub_a ();
            > my @values = sub_a ();
            > sub_b (@values);
            >
            > ### RUN SUBS ###
            >
            > sub sub_a {
            > my $x = "Larry";
            > my $y = "Moe";
            > my $z = "Curly";
            > }
            >
            >
            > sub sub_b {
            >
            > my @values = shift;
            > my $i;
            >
            > print "Content-Type: text/html\n\n";
            >
            > foreach $i (@values) {
            > print "$i\n\n";
            > }
            > }
            >
            > Thanks,
            >
            > quint
            >
            > _________________________________________________________________
            > Insert movie times and more without leaving HotmailĀ®.
            > http://windowslive.com/Tutorial/Hotmail/QuickAdd?ocid=TXT_TAGLM_WL_HM_Tutorial_QuickAdd1_052009
            >
            > [Non-text portions of this message have been removed]
            >
          Your message has been successfully submitted and would be delivered to recipients shortly.