Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: passing arrays to subroutines

by neniro (Priest)
on Mar 16, 2005 at 13:40 UTC ( #439940=note: print w/ replies, xml ) Need Help??


in reply to passing arrays to subroutines

There are two ways of passing data to a subroutine:

- call by value
- call by reference

References are always a good idea - especially if you have large or complex datastructures, but using values is much easier in the beginning. Here's an example that I've posted earlier this day on a german perl-community site. Two array-references gets passed to a function in an anonymous hash:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @numbers = (1..10); my @words = qw(hello world outside); function( {NUMBERS=>\@numbers , WORDS=>\@words} ); exit; sub function { die "no parameter!\n" unless @_; my %opt = %{ shift @_ }; print Dumper \%opt; my @i_words = $opt{WORDS}; print Dumper \@i_words; my @i_numbers = $opt{NUMBERS}; print Dumper \@i_numbers; }


Comment on Re: passing arrays to subroutines
Download Code
Re^2: passing arrays to subroutines
by QM (Vicar) on Mar 16, 2005 at 14:36 UTC
    There are two ways of passing data to a subroutine:

    - call by value
    - call by reference

    Actually, there's also:

    - call by name
    - call by copy-restore
    - call by result
    - call by push value
    - call by need
    - call by macro expansion

    See Parameter (Computer Science) on Wikipedia.

    Most (all?) of these can be done in Perl. In Perl, it really comes down to two things:

    1) Parameters are passed as a list of scalars.
    2) The caller and the callee have to agree on what those scalars mean.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://439940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (246 votes), past polls