Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Subroutine's behavior

by zitic (Initiate)
on Nov 23, 2009 at 01:00 UTC ( #808698=perlquestion: print w/replies, xml ) Need Help??
zitic has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone!

I have some problems with perl subroutines, or being more specific, I cannot figure out why they are not working as they should.

Here is a tiny code snippet as a sample:
sub doubleWord($word) { return $word.$word; } print &doubleWord('hello');

I was hoping that output would be the string 'hellohello', but turns out there is no output at all. I run the script and nothing happens. No errors, no warnings, nothing.

Where is the catch or what am I doing wrong? I'd really like to understand the subroutine's logic.

Replies are listed 'Best First'.
Re: Subroutine's behavior
by keszler (Priest) on Nov 23, 2009 at 01:15 UTC
    use strict; sub doubleWord { my $word = shift; return $word.$word; } print doubleWord('hello');
Re: Subroutine's behavior
by tford (Beadle) on Nov 23, 2009 at 01:38 UTC

    The reply above mine pretty much says it all. However, you might be wondering what the shift is for.

    The shift function takes an array as input, and "shifts" off the first element of the array. Then it returns that element, leaving the array one element shorter.

    For example,

    my @array = (2,3,4); my $value = shift(@array); # now the array contains (3,4) print "value is $value";

    Now, if you don't specify an array to use as input, the shift function works on the "default array" called @_.

    The last important thing to know is that anytime you call a Perl subroutine, you only ever call it with a flat array of arguments. Within the scope of the subroutine, those arguments are automatically assigned to the @_ array.

    Thus shift shifts the first argument off the argument array, and assigns it's value to the variable $word, which is local to the subroutine.

    Hope this helps!


Re: Subroutine's behavior
by toolic (Bishop) on Nov 23, 2009 at 01:31 UTC
    what am I doing wrong?
    You should always use strict and warnings because you would have gotten warnings about your code.
Re: Subroutine's behavior
by doug (Pilgrim) on Nov 23, 2009 at 14:47 UTC

    As has been pointed out, you've got your subroutine arg passing broken. strict and warnings should be your friends.

    If I remember correctly, perl6 will use something like this. Maybe you mean to be running that instead of standard perl5? Or you've been reading perl6 docs and don't know that this is one of the many changes?

    - doug

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://808698]
Approved by keszler
[thezip]: G'day monks!
[thezip]: There's a word I'm looking for, which describes the (lack of) repeatability of an instruction. Specifically, when a command is run the first time, it succeeds, but then every subsequent execution of the same command results in an identical result...
[thezip]: What is the name of this property?

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2018-07-18 20:33 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (395 votes). Check out past polls.