Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: passing arrays to subroutines

by neniro (Priest)
on Mar 16, 2005 at 13:40 UTC ( [id://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; }

Replies are listed 'Best First'.
Re^2: passing arrays to subroutines
by QM (Parson) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://439940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-18 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found