Reputation: 87
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');
}
Upvotes: 4
Views: 380
Reputation: 87
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.
Upvotes: 1
Reputation: 126732
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;
}
Upvotes: 3