Professional Web Applications Themes

passing values to a sub - PERL Miscellaneous

Also sprach Sam:   You have to be aware that any arguments you pass to Perl-subroutines are flattened to one large indistinguishable list. So a = (1, 2, 3), $s = "foobar"; func(a, $s); # roughly becomes func(1, 2, 3, "foobar"); That means you have no real chance to separate the arguments again. You have two choices. You can choose a clever calling-convention where scalars are passed before list-alike data-structures. However, you can only pass one list that way (it has to be at the end of the argument list for obvious reasons): ma_calc( $Mn, MyArr ); sub ma_calc { ...

  1. #1

    Default Re: passing values to a sub

    Also sprach Sam:
     

    You have to be aware that any arguments you pass to Perl-subroutines are
    flattened to one large indistinguishable list. So

    a = (1, 2, 3),
    $s = "foobar";
    func(a, $s);

    # roughly becomes

    func(1, 2, 3, "foobar");

    That means you have no real chance to separate the arguments again. You
    have two choices. You can choose a clever calling-convention where
    scalars are passed before list-alike data-structures. However, you can
    only pass one list that way (it has to be at the end of the argument
    list for obvious reasons):

    ma_calc( $Mn, MyArr );
    sub ma_calc {
    my ($value, list) = _;
    ...
    }

    Or you always pass scalar values. An array therefore would have to be
    passed as a reference:

    ma_calc ( \MyArr, $Mn );
    sub ma_calc {
    my list = { shift }; # dereference
    my $value = shift;
    ...
    }

    It works just the same for hashes since they are flattened to a list as
    well:

    ma_calc (\MyArr, \%MyHash );
    sub ma_calc {
    my list = { shift };
    my hash = %{ shift };
    ...
    }

    All the above is explained in detail in 'perldoc perlsub'.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+ii ixesixeseg;y~\n~~dddd;eval
    Tassilo Guest

  2. #2

    Default Re: passing values to a sub

    On Sun, 21 Sep 2003 09:22:48 GMT, Sam <com.au> wrote: 

    Maybe instead of asking abunch of questions which are all answered
    in the doentation that comes with perl, you could try reading
    the doentation.

    --
    Sam Holden

    Sam Guest

  3. #3

    Default Re: passing values to a sub

    Sam Holden wrote: 
    >
    >
    > Maybe instead of asking abunch of questions which are all answered
    > in the doentation that comes with perl, you could try reading
    > the doentation.
    >[/ref]

    I wish one day it would be easy to ready those doents, every time I
    open one to read it I get lost between terms, refrences to terms,
    location ...etc. I some times which that it would contain hyperlinks for
    abstract terms as well so it would be easy to understand for a slow to
    understand person like me.


    Sam Guest

  4. #4

    Default Re: passing values to a sub

    Tassilo v. Pval wrote: 
    >
    >
    > You have to be aware that any arguments you pass to Perl-subroutines are
    > flattened to one large indistinguishable list. So
    >
    > a = (1, 2, 3),
    > $s = "foobar";
    > func(a, $s);
    >
    > # roughly becomes
    >
    > func(1, 2, 3, "foobar");
    >
    > That means you have no real chance to separate the arguments again. You
    > have two choices. You can choose a clever calling-convention where
    > scalars are passed before list-alike data-structures. However, you can
    > only pass one list that way (it has to be at the end of the argument
    > list for obvious reasons):
    >
    > ma_calc( $Mn, MyArr );
    > sub ma_calc {
    > my ($value, list) = _;
    > ...
    > }
    >
    > Or you always pass scalar values. An array therefore would have to be
    > passed as a reference:
    >
    > ma_calc ( \MyArr, $Mn );
    > sub ma_calc {
    > my list = { shift }; # dereference
    > my $value = shift;
    > ...
    > }
    >
    > It works just the same for hashes since they are flattened to a list as
    > well:
    >
    > ma_calc (\MyArr, \%MyHash );
    > sub ma_calc {
    > my list = { shift };
    > my hash = %{ shift };
    > ...
    > }
    >
    > All the above is explained in detail in 'perldoc perlsub'.
    >
    > Tassilo[/ref]

    Mm = ma_calc ( \MyArr, $Mm );

    sub ma_calc {
    my list = { shift };
    my $value = shift;

    Ambiguous use of {shift} resolved to shift at
    /home/usrname/perl-programs/test.pl line 56.
    Global symbol "shift" requires explicit package name at
    /home/usrname/perl-programs/test.pl line 56.
    Execution of /home/usrname/perl-programs/test.pl aborted due to
    compilation errors.

    Compilation exited abnormally with code 255 at Sun Sep 21 20:46:29

    Sam Guest

  5. #5

    Default Re: passing values to a sub

    Sam wrote:
     

    Imho its very hard to learn a new programming-language without
    purchasing/borrowing/stealing a book that helps you along with the first
    steps. Later the mentioned docs will be extremely useful, but by now a book
    that guides you through the most basic tasks might help you to avoid lot of
    lost time and frustration.

    best,
    peter


    --
    peter pilsl
    at
    http://www.goldfisch.at

    peter Guest

  6. #6

    Default Re: passing values to a sub

    Sam <com.au> wrote: [/ref]

     
    >>
    >>
    >> Maybe instead of asking abunch of questions which are all answered
    >> in the doentation that comes with perl, you could try reading
    >> the doentation.
    >>[/ref]
    >
    > I wish one day it would be easy to ready those doents, every time I
    > open one to read it I get lost between terms, refrences to terms,
    > location ...etc.[/ref]


    If you don't understand the docs, post a question about the docs
    here, and we will help explain it for you. Note that this is
    different from not reading the docs... :-)

     


    The second paragraph in the docs for subroutines answers your question:

    perldoc perlsub

    ...
    The Perl model for function call and return values is simple: all
    functions are passed as parameters one single flat list of scalars, and
    all functions likewise return to their caller one single flat list of
    scalars. Any arrays or hashes in these call and return lists will
    collapse, losing their identities


    --
    Tad McClellan SGML consulting
    com Perl programming
    Fort Worth, Texas
    Tad Guest

  7. #7

    Default Re: passing values to a sub

    peter pilsl wrote: 
    >
    >
    > Imho its very hard to learn a new programming-language without
    > purchasing/borrowing/stealing a book that helps you along with the first
    > steps. Later the mentioned docs will be extremely useful, but by now a book
    > that guides you through the most basic tasks might help you to avoid lot of
    > lost time and frustration.
    >
    > best,
    > peter
    >
    >[/ref]

    thanks Peter
    I had Element of programming with Perl, also have Programming Perl, as
    well as a copy of Perl in a nutshell, I just need some practical
    examples and doing it myself to make it more of a reality instead of
    theory and some examples. it will come be time. as new as I am to linux
    3 month only with life wasted to M$ it is a steep curve but not
    impossible. thanks for all the help I get from all of you.

    Sam Guest

  8. #8

    Default Re: passing values to a sub

    * Sam <com.au>:

    [...] 

    When you do get around to understanding the docs, remember
    what you just wrote and contribute back to the perl project
    by revising the doentation to make it easier for people
    to understand.



    cheers,
    --
    Iain. <http://eh.org/~koschei/>
    Iain Guest

  9. #9

    Default Re: passing values to a sub

    On Sun, 21 Sep 2003 10:42:02 GMT
    Sam <com.au> wrote:

    # Mm = ma_calc ( \MyArr, $Mm );
    #
    # sub ma_calc {
    # my list = { shift };
    # my $value = shift;
    #
    # Ambiguous use of {shift} resolved to shift at
    # /home/usrname/perl-programs/test.pl line 56.
    # Global symbol "shift" requires explicit package name at
    # /home/usrname/perl-programs/test.pl line 56.

    Here the perl interpreter is confused as to whether it is a variable
    name or a code to be executed. You can help it by adding a + in front.

    my list = { +shift };

    Regards,
    Thens.
    Thens Guest

  10. #10

    Default Re: passing values to a sub

    On Sun, 21 Sep 2003 10:42:02 GMT
    Sam <com.au> wrote:
     

    Try:

    sub ma_calc {
    my list = { shift() };
    my $value = shift();
    ...
    }

    Roy Guest

Similar Threads

  1. Passing Values in Flash
    By RainGauge in forum Macromedia Flash Data Integration
    Replies: 0
    Last Post: September 19th, 06:21 PM
  2. Passing List Values
    By hecsan in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: July 7th, 04:23 PM
  3. Datagrid/SQL & passing values
    By VFR in forum ASP.NET Data Grid Control
    Replies: 3
    Last Post: February 6th, 01:47 PM
  4. Combo Box and passing values
    By syktek webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: January 21st, 07:24 PM
  5. passing through values
    By Jeroen van Vliet in forum Macromedia Flash
    Replies: 1
    Last Post: January 12th, 06:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139