Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Passing variables into a subroutine

by Freezer (Sexton)
on Sep 03, 2012 at 17:13 UTC ( [id://991486]=perlquestion: print w/replies, xml ) Need Help??

Freezer has asked for the wisdom of the Perl Monks concerning the following question:

There appears not to be a logical reason why the following should not work:
sub create_output (@) { my (@array_of_lines, $entry_no_new) = @_; # THIS DOES NOT WORK
However it doesn't. What is the solution please?

Replies are listed 'Best First'.
Re: Passing variables into a subroutine
by CountZero (Bishop) on Sep 03, 2012 at 19:31 UTC
    Your prototype definition is wrong. Your definition of (@) says that your sub takes one array or list as argument, so all arguments are flattened into one list. This is the way any "normal" subroutine without prototypes works.

    However now consider this:

    use Modern::Perl; sub create_output (\@$) { my ($arrayref_of_lines, $entry_no_new) = @_; say "array: @{$arrayref_of_lines}"; say "scalar: $entry_no_new"; } my @array = qw/1 2 3 4/; create_output @array, 5;
    And its output:
    array: 1 2 3 4 scalar: 5

    This is actually one of the few cases of a prototype being applied in a useful way.

    The prototype (\@$) means the first argument to your sub MUST start with a @ (in other words, it must be an array), followed by a scalar. Due to the (\@) in the prototype, Perl actually takes a reference to the array and passes that as the first argument to your sub. So don't you forget to de-reference it in your sub!

    It is this automagical referencing that allows you to pass one or more arrays to a subroutine without them all getting flattened in one big @_.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Perl actually takes a reference to the array and passes that as the first argument to your sub. So don't you forget to de-reference it in your sub!



      Is there a popper way to do the dereferencing, given the context?
        Sadly, there will be no automatic dereferencing done by Perl. But it is very easy to do: to get the original array, just add the @ sigil to the front of the scalar holding the reference.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
      Is something like the following equally cool?
      sub look_through_file ($$) { my ($start_point, $continue_tag) = @_;
        As a general rule don't use prototypes unless there is a good reason for it such as passing in several arrays without them getting flattened into one @_. And even then passing references to the arrays is probably a more clear solution.

        I see no merit in adding a prototype definition of ($$).

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: Passing variables into a subroutine
by choroba (Cardinal) on Sep 03, 2012 at 17:19 UTC
    The problem is in the assignment. You can never make this work:
    my (@array, ...) = ...
    because the array takes all the values.

    Update: Other problems may emerge after solving this one because of the usage of prototypes.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Passing variables into a subroutine
by daxim (Curate) on Sep 03, 2012 at 17:22 UTC
Re: Passing variables into a subroutine
by Corion (Patriarch) on Sep 03, 2012 at 17:18 UTC

    You don't tell us how it fails for you (besides the obvious syntax error).

    Perl flattens passed parameters into @_. perlsub explains some more of that.

Re: Passing variables into a subroutine
by AnomalousMonk (Archbishop) on Sep 03, 2012 at 22:47 UTC

    The other alternative, if you don't want to deal with references and if there is only one array (or hash) to be passed, is to pass any and all scalars (and there can be more than one) first, and then pass the sole array (or hash). The following works the same way with or without the  (@) prototype for the reason explained by CountZero (i.e., in general, don't bother with prototypes):

    perl -wMstrict -le "sub S { my ($scalar, @ra) = @_; ;; print qq{scalar: '$scalar' array:(@ra)}; } ;; my @array = (1, 2, 3); S('foo', @array); " scalar: 'foo' array:(1 2 3)
Re: Passing variables into a subroutine
by evaluator (Monk) on Sep 04, 2012 at 11:53 UTC
    Try this (untested):
    my $entry_no_new = pop @_; my @array_of_lines = @_;
    or
    my @array_of_lines = @_; my $entry_no_new = pop @array_of_lines;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://991486]
Approved by Corion
Front-paged by CountZero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-19 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found