Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

My first module

by tamaguchi (Pilgrim)
on Apr 26, 2006 at 11:25 UTC ( #545765=perlquestion: print w/replies, xml ) Need Help??

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

I have written a program..
test.pl ------ #!/usr/bin/perl -w use strict; use test_module; my $var=10; my $new_var=testsub($var); print "$new_var\n";
..and my first module..
test_module.pm ----------------------- sub testsub { my $in_arg=@_; my $new_arg=$in_arg+10; return($new_arg); } 1;
Could someone explain why this construct prints out "11" instead of 20?? Thank you for your help.

Replies are listed 'Best First'.
Re: My first module
by GrandFather (Saint) on Apr 26, 2006 at 11:34 UTC

    Because $in_arg is scalar my $in_arg=@_; assigns the number of elements in @_ (1) to $in_arg.

    Instead either:

    my ($in_arg)=@_;

    or:

    my $in_arg=shift;

    DWIM is Perl's answer to Gödel
      Because $in_arg is scalar my $in_arg=@_;

      I would say, if I really wanted to say that,

      my $in_arg=scalar @_;

      but then I would just say that @array evaluated in scalar context returns the number of elements in @array and that assignment to a scalar imposes scalar context. Or something like that...

      Update: since this was downvoted, why don't you actually try?

      $ perl -e 'scalar my $in_arg=@_' Can't modify scalar in scalar assignment at -e line 1, at EOF Execution of -e aborted due to compilation errors.

      Update2: I read Re: My first module once again and now I realize I read it uncorrectly. I use a theme in which occasionally inline code is not visually distinctive enough, and GrandFather didn't put a suitable interpunction sign, so this added to my confusion and misreading. Sorry, my apologies...

Re: My first module
by gellyfish (Monsignor) on Apr 26, 2006 at 11:36 UTC

    It is printing 11 because you have the:

    my $in_arg=@_;
    Which is giving scalar context to the assignment from @_ which puts the length of the array in $in_arg you want to do either:
    my ( $in_arg ) = @_;
    or
    my $in_arg = shift;
    There is a difference between the two related to the side-effects but you won't notice it in your code.

    /J\

Re: My first module
by blazar (Canon) on Apr 26, 2006 at 13:27 UTC

    Aside from the answers coping with your actual problem, you don't have really a problem having to do with your "first module" or a module at all, but incidentally while we're here I'll remind you that you probably shouldn't pollute the main namespace in modules, but put an explicit package in them, exporting only what you really need, or better giving the user choose what he really needs to export.

    Also incidentally, even if this was just a minimal example, all lowercase modules are reserved for pragmata.

Re: My first module
by TedPride (Priest) on Apr 26, 2006 at 17:00 UTC
    Or... my $in_arg = $_[0];
Re: My first module
by osunderdog (Deacon) on Apr 26, 2006 at 13:34 UTC

    Just clear up the potentially obvious stuff. Your module should be:

    ########## test_module.pm ########## package test_module; use strict; sub testsub { my $in_arg= shift; my $new_arg=$in_arg+10; return($new_arg); } 1;
    Also, you can start out with everything in one file first:
    #!/usr/bin/perl -w use strict; package test_module; use strict; sub testsub { my $in_arg= shift; my $new_arg=$in_arg+10; return($new_arg); } my $var=10; my $new_var=test_module::testsub($var); print "$new_var\n";

    Hazah! I'm Employed!

Re: My first module
by perladdict (Chaplain) on Apr 27, 2006 at 06:32 UTC
    hi,monk

    here is my code

    #!/usr/bin/perl -w use strict; my $var=10; my $new_var=testsub($var); print "$new_var\n"; sub testsub { my $in_arg=shift; my $new_arg=$in_arg+10; return($new_arg); }

    Edit: g0n - corrected code tags

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-01-18 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?