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

Re: help with sub variables

Expand Messages
  • Damien Carbery
    I believe you can easily pass lists and hashes to functions as parameters, but you have to be careful with the parameter order. I think you have to pass your
    Message 1 of 9 , Aug 2, 2001
    • 0 Attachment
      I believe you can easily pass lists and hashes to functions as
      parameters, but you have to be careful with the parameter order.
      I think you have to pass your scalars first, otherwise they'll be
      swallowed into the list or hash.

      This should work:
      sub scalar_then_hash
      {
      my ( $scalar, %hash ) = @_;
      # Do stuff
      }

      scalar_then_hash( $Value, %Hash );

      while this shouldn't:
      sub hash_then_scalar
      {
      my ( %hash, $scalar ) = @_;
      # Do stuff
      }

      hash_then_scalar( %Hash, $Value );
      because the %hash is greedy.

      Of course, I could be wrong.

      --- In perl-beginner@y..., Cam Coble <tech@u...> wrote:
      > Having a problem understanding context here...
      >
      > I have two perl files:
      > A_perl.pl
      > B_perl.pl
      >
      > Script A has environment
      > >use strict;
      > >require './B_perl.pl';
      >
      > and declairs variables by
      > >my ($scalar,@list,%hash);
      >
      > Now, how do I get a variable into a sub in B_perl.pl from A_perl.pl
      without
      > passing as an argument?
      > I need all types of variables... lists and hashes too.
      >
      > Script works by passing as arguments, but only scalars.
      >
      > Thanks for the help,
      > Cam
    • erik.tank@bpxinternet.com
      You can also pass references to hashes and arrays. If you are on UNIX/LINUX type man perlref (without quotes) for more info. If that doesn t work I believe
      Message 2 of 9 , Aug 2, 2001
      • 0 Attachment
        You can also pass references to hashes and arrays. If you are on UNIX/LINUX
        type "man perlref" (without quotes) for more info. If that doesn't work I
        believe the document is available from www.perl.com

        Erik Tank
        602-817-4705
        erik.tank@...


        -----Original Message-----
        From: Damien Carbery [mailto:daymobrew@...]
        Sent: Thursday, August 02, 2001 2:00 AM
        To: perl-beginner@yahoogroups.com
        Subject: [PBML] Re: help with sub variables


        I believe you can easily pass lists and hashes to functions as
        parameters, but you have to be careful with the parameter order.
        I think you have to pass your scalars first, otherwise they'll be
        swallowed into the list or hash.

        This should work:
        sub scalar_then_hash
        {
        my ( $scalar, %hash ) = @_;
        # Do stuff
        }

        scalar_then_hash( $Value, %Hash );

        while this shouldn't:
        sub hash_then_scalar
        {
        my ( %hash, $scalar ) = @_;
        # Do stuff
        }

        hash_then_scalar( %Hash, $Value );
        because the %hash is greedy.

        Of course, I could be wrong.

        --- In perl-beginner@y..., Cam Coble <tech@u...> wrote:
        > Having a problem understanding context here...
        >
        > I have two perl files:
        > A_perl.pl
        > B_perl.pl
        >
        > Script A has environment
        > >use strict;
        > >require './B_perl.pl';
        >
        > and declairs variables by
        > >my ($scalar,@list,%hash);
        >
        > Now, how do I get a variable into a sub in B_perl.pl from A_perl.pl
        without
        > passing as an argument?
        > I need all types of variables... lists and hashes too.
        >
        > Script works by passing as arguments, but only scalars.
        >
        > Thanks for the help,
        > Cam





        Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
      • Jenda Krynicky
        Message 3 of 9 , Aug 7, 2001
        • 0 Attachment
        • Jenda Krynicky
          ... You are probably trying to download the files from a wrong directory. what address do you get for ActiveState Package Repository from c: PPM PPM set I
          Message 4 of 9 , Aug 7, 2001
          • 0 Attachment
            > I am using 5.6.1 of ActiveState perl (unfortunately must be Windoze
            > 2000). Trying to install different pod packages.
            >
            > Get the error:
            >
            > Error installing package 'Net-DNS': Read a PPD for 'Net-DNS', but it
            > is not intended for this build of Perl (MSWin32-x86-multi-thread).

            You are probably trying to download the files from a wrong directory.

            what address do you get for "ActiveState Package Repository" from

            c:\>PPM
            PPM> set

            I have
            "http://ppm.ActiveState.com/cgibin/PPM/ppmserver.pl?urn:/PPMServer"
            and all seems to work OK.

            > Also go to activestate site directly, download and install from hard
            > drive - same error. Tried many other packages... all same error.

            Wrong directory.

            Try http://www.activestate.com/PPMPackages/zips/6xx-builds-only/

            Jenda

            == Jenda@... == http://Jenda.Krynicky.cz ==
            : What do people think?
            What, do people think? :-)
            -- Larry Wall in <199808071736.KAA12738@...>
          • Sidharth Malhotra
            use packages. at the top of A.pl insert: package A; and package B; at the top of B.pl You can then access the variables as: $A::scalar, %A::hash,
            Message 5 of 9 , Aug 7, 2001
            • 0 Attachment
              use packages.
              at the top of A.pl insert:

              package A;

              and

              package B;

              at the top of B.pl You can then access the variables as:
              $A::scalar, %A::hash, $A::hash{'elem'}, @B::Arr, $B::Arr[i]; # you get the
              idea

              hth. Sid.

              -----Original Message-----
              From: Jenda Krynicky [mailto:Jenda@...]
              Sent: Tuesday, August 07, 2001 5:44 PM
              To: perl-beginner@yahoogroups.com
              Subject: Re: [PBML] help with sub variables


              > Having a problem understanding context here...
              >
              > I have two perl files:
              > A_perl.pl
              > B_perl.pl
              >
              > Script A has environment
              > >use strict;
              > >require './B_perl.pl';
              >
              > and declairs variables by
              > >my ($scalar,@list,%hash);
              >
              > Now, how do I get a variable into a sub in B_perl.pl from A_perl.pl
              > without passing as an argument? I need all types of variables... lists
              > and hashes too.
            • Jenda Krynicky
              ... Just two important things : 1) You can t pass several arrays or hashes this way : sub foo{ my (@a, @b) = @_; print Length of @a = ,scalar(@a), n ;
              Message 6 of 9 , Aug 8, 2001
              • 0 Attachment
                > I believe you can easily pass lists and hashes to functions as
                > parameters, but you have to be careful with the parameter order.
                > I think you have to pass your scalars first, otherwise they'll be
                > swallowed into the list or hash.
                >
                > This should work:
                > sub scalar_then_hash
                > {
                > my ( $scalar, %hash ) = @_;
                > # Do stuff
                > }
                >
                > scalar_then_hash( $Value, %Hash );

                Just two important things :
                1) You can't pass several arrays or hashes this way :

                sub foo{
                my (@a, @b) = @_;
                print "Length of \@a = ",scalar(@a),"\n";
                print "Length of \@b = ",scalar(@b),"\n";
                }

                @x = (1,3,5);
                @y = (2,4,6);
                foo(@x,@y);

                2) You are actualy not passing the array or the hash ... you only
                pass one long list. foo() will see NO difference between

                foo(1,2,3,4,5);
                and
                @a = (2,3,4,5);
                foo(1,@a);
                or
                %a = (2 => 3, 4 => 5);
                foo(1,%a);

                This means that you for eample cannot update a hash you passed
                this way. Plus you force perl to flatten your hash into a list and
                then construct a new hash with those keys and values.

                Realy IF YOU WANT TO PASS AN ARRAY OR A HASH PASS A
                REFERENCE.

                Jenda


                == Jenda@... == http://Jenda.Krynicky.cz ==
                : What do people think?
                What, do people think? :-)
                -- Larry Wall in <199808071736.KAA12738@...>
              Your message has been successfully submitted and would be delivered to recipients shortly.