-1

I have seen usage of $ in subroutine definition somewhere. To learn more about it, I created various cases with a simple subroutine, and came to know that it's used for defining subroutines with exact signatures.

Can anyone please confirm:

  1. Whether I'm right? I mean whether it's used for this purpose?
  2. Whether it has any other uses?
  3. Is there any other way to access those parameters inside subroutine, other than using my $param1 = shift; or my (@params) = @_?

use strict;
use warnings;

# just a testing function
sub show($$){
    print "Inside show";
}

show(1, 1);   # works fine
show(1);     # gives compilation error
# Not enough arguments for main::show at test.pl line 8, near "1)"
# Execution of test.pl aborted due to compilation errors.

show(1, 1, 1);   # gives compilation error
# Too many arguments for main::show at test.pl line 8, near "1)"
# Execution of test.pl aborted due to compilation errors.       
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Kamal Nayan
  • 1,890
  • 21
  • 34
  • 1
    Larry would have the most intellectual comment of us all. ;) - I think the by now two downvoters don't like that you did not simply read the documentation. – simbabque May 19 '17 at 11:57
  • 2
    @KamalNayan: *"If everyone starts reading documentation fully"* That is a disgraceful attitude. I have been programming Perl professionally for nearly twenty years now, and I still read every page of the documentation at least once a year. If you're not up to the task then don't play at programming. – Borodin May 19 '17 at 12:04
  • @Borodin Apologies, I didn't mean that. Most often (generally newbies) prefer to read a little part of documentation. – Kamal Nayan May 19 '17 at 12:06
  • 2
    Type `perldoc perltoc` in your terminal and read *everything* listed there at least once a year. – Sinan Ünür May 19 '17 at 12:07
  • @KamalNayan: *"Most often (generally newbies) prefer to read a little part of documentation"* yes, and that pervasive attitude is what leads to the many "I need code to do this" questions here. If you don't know the language then please don't get other people to do your work for free. – Borodin May 19 '17 at 12:08
  • @SinanÜnür: Thanks, I was not knowing about this command. And again sorry if my comments hurted anyone! – Kamal Nayan May 19 '17 at 12:08
  • @KamalNayan: *"I was not knowing about this command"* And it's clear *why* you didn't know about it. – Borodin May 19 '17 at 12:11
  • Yes I admit, I dont know and browse stackoverflow to learn. – Kamal Nayan May 19 '17 at 12:13
  • @KamalNayan: There are many advantages to learning a language from the documentation. It is free; it is fast; you don't waste anyone's time asking them to recite it to you; it is usually accurate; it is more comprehensive; and it is immediately accessible at any time of day or night. – Borodin May 19 '17 at 12:18
  • 2
    @KamalNayan: You would learn a *lot* more by *answering* questions on Stack Overflow. – Borodin May 19 '17 at 12:19

1 Answers1

2

You are using subroutine prototypes. Don't. For more detail, see Why are Perl 5's function prototypes bad?

A new, experimental feature introduced in 5.20 is subroutine signatures. Those do everything you wished subroutine prototypes could.

For example:

use strict;
use warnings;

use feature 'signatures';
no warnings 'experimental::signatures';

sub show ( $canvas, $actor ) {
    $actor->draw( $canvas, $COLOR{default});
}

etc

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • I have seen this code somehere, I'm not using it in production, just wanted to know in a small script. :) – Kamal Nayan May 19 '17 at 12:00
  • 1
    @KamalNayan yes, people coming to Perl from other languages look for signatures and find prototypes and use them. But they really serve a different purpose and should almost never be used. And the newer signatures feature is experimental and subject to change or removal, so don't use that either unless you are comfortable with that. – ysth May 19 '17 at 20:05