4

For my homework, I was told to make a subroutine within Perl, and when called with arguments in it, it would print the arguments. I did this and the second part of my homework was to call that first subroutine within the second one. So I did all this, but then the question was: try running the second subroutine with "&" while calling the first one, and without it and to write what is the difference in these two different calls. So subroutine seems to works perfectly in both cases, with and without, but I just don't know what it really changed, therefore i don't know what to write. Off topic, does my code look fine? Kinda first time coding in this Perl.

This is the code:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

show_args('fred','barney','betty');

show_args_again();

sub show_args{
    my ($broj1, $broj2, $broj3) = @_;
    print "The arguments are: " . "$broj1, " . "$broj2, " . "$broj3.\n";
}
sub show_args_again{
    &show_args('fred','barney','betty');
}
Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
AsapCancel
  • 87
  • 1
  • 10
  • 1
    Welcome to Stack Overflow! This is a fair question. Regarding your code, it looks pretty good. You have `strict` and `warnings`. The names are clear. I suspect `$broj1` refers to _argument_ in your native tounge. You should decide if you want to stick with English or not, and stay with that decision. The `print` line could be better written as `print "foo: ", join(',', $broj1, $broj2, $broj3), "\n";`. Note that `say` was not in Perl 5.10. – simbabque Jul 27 '15 at 13:16
  • the code does look fine, but I would advise sticking to one thing at a time http://stackoverflow.com/questions/8912049/difference-between-function-and-function-in-perl – beasy Jul 27 '15 at 13:17
  • @simbabque: `say` was introduced in Perl v5.10 – Borodin Jul 27 '15 at 13:25
  • @Borodin: http://perldoc.perl.org/5.10.0/search.html?q=say doesn't find it. But http://perldoc.perl.org/5.10.0/functions/say.html exists. Strange. I stand corrected. :) – simbabque Jul 27 '15 at 13:25
  • @simbabque: It's in [`perl5100delta`](http://search.cpan.org/dist/perl-5.10.0/pod/perl5100delta.pod#say%28%29) – Borodin Jul 27 '15 at 13:27
  • @beasy: I think handing the answer to a homework question on a plate isn't very constructive – Borodin Jul 27 '15 at 13:29
  • @simbabque thank you for the welcome. also thank you all for the VERY fast answers... Borodin nah, it was only said for first subroutine to be called with parameters, the second one is just supposed to call the first one with "&" or without, but looking to the other questions & answers: "& tells Perl to ignore the sub's prototype" and few more, i checked it myself now and seem to start understanding it better – AsapCancel Jul 27 '15 at 13:49

2 Answers2

3

Does the assignment say whether show_args_again should be called with parameters?

The only thing I can think of that you are meant to discover is demonstrated by this code. Give it a try

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

show_args('fred', 'barney', 'betty');
print "---\n";
show_args_again('fred', 'barney', 'betty');

sub show_args{
    my ($broj1, $broj2, $broj3) = @_;
    no warnings 'uninitialized';
    print "The arguments are: $broj1, $broj2, $broj3.\n";
}

sub show_args_again {
    show_args(@_);
    &show_args(@_);
    show_args();
    &show_args();
    show_args;
    &show_args;
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • Nah it was only said for the first subroutine, and was told that second one should just call the first one, but thanks for that code i am currently trying it out now – AsapCancel Jul 27 '15 at 13:55
  • @AsapCancel: Well your `show_args` doesn't (and shouldn't) have a prototype, so the only difference calling with an ampersand can make is when you call it without parameters at all, like `&show_args;`. Perhaps the point is that there isn't any difference. Either way, you should *never* call subroutines in modern production code unless you need the esoteric features that it provides. Even then you probably want `goto &show_args` – Borodin Jul 27 '15 at 15:10
1

I think that I found the way it should have been done, and finally realized what they actually wanted me to show with this homework. So this is my code:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;


show_args("fred","barney","betty",);

show_args_again("fred","barney","betty");

sub show_args{ 
    print "The arguments are: ", join(', ', @_), ".\n"; 
}

sub show_args_again{
     &show_args; 
}

So when I don't put "&" symbol, it doesn't show me arguments, and as I saw somewhere before, when you use "&" without trailing parentheses you ignore the prototype and you use the current value of @_ argument list. So calling it without that symbol is just the counter effect of the whole thing I suppose. And yeah, the second subroutine should do the same thing as first one, and they didn't really say if it should or shouldn't have arguments, but just said that it must call it.

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
AsapCancel
  • 87
  • 1
  • 10