Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

sub variables

by Todd Chester (Scribe)
on Jul 22, 2017 at 23:33 UTC ( #1195793=perlquestion: print w/replies, xml ) Need Help??

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

Dear Perl Monks,

In Perl 5, is there a way to name my variables in the () of the sub declaration, as I do in Perl 6?
sub DoSomething( $DataStr ) {;}

Many thanks, -T

Replies are listed 'Best First'.
Re: sub variables
by LanX (Sage) on Jul 23, 2017 at 00:06 UTC
    > In Perl 5, is there a way to name my variables in the () of the sub declaration,

    Yes newer Perl versions have an experimental new feature for signatures

    see https://www.effectiveperlprogramming.com/2015/04/use-v5-20-subroutine-signatures/

    > sub DoSomething( $DataStr ) {;}

    Yes, positional arguments are supported.

    Even with mandatory arguments or optional arguments with default values.

    > as I do in Perl 6?

    Not really.

    Perl6 allows much more, like named variables, types and constraints

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Thank you!
      I am on 5.16.3
      Rats!

        I wrote a simplicistic source filter to bring the subroutine signatures to almost all versions of Perl. The module is Filter::signatures.

Re: sub variables
by kevbot (Priest) on Jul 23, 2017 at 01:06 UTC
    If you want those type of features in perl5 take a look at the Function::Parameters module.

    UPDATE: This module requires perl 5.14 or newer.

Re: sub variables
by golux (Chaplain) on Jul 23, 2017 at 01:36 UTC
    Hi Todd Chester,

    I personally like the module Method::Signatures, but tbh it's the only one I've used. I'll have to check out Function::Parameters for comparison's sake, which, judging by its date(s) looks like it may be more recent.

    say  substr+lc crypt(qw $i3 SI$),4,5
      I have a fairly large code-base that relies on Method::Signatures, and I have found that it works very well. However, it depends on Devel::Declare. The documentation for Devel::Declare states,
      WARNING Warning: Devel::Declare is a giant bag of crack originally implemented + by mst with the goal of upsetting the perl core developers so much b +y its very existence that they implemented proper keyword handling in + the core. As of perl5 version 14, this goal has been achieved, and modules such +as Devel::CallParser, Function::Parameters, and Keyword::Simple provi +de mechanisms to mangle perl syntax that don't require hallucinogenic + drugs to interpret the error messages they produce.
      So, for new code I would recommend Function::Parameters.

      Also, it looks like the author of Method::Signatures (schwern) would like for the two modules to work well together. See these github issues,

      However, these issues are a few years old and I don't know how much progress has been made in addressing them.
        Another module along this like is Toby Inkster's Kavorka, which seems very powerful.
        kevbot,

        Thank you for that info. I have noticed that there is one glitch that occurs in Method::Signatures on occasion. I like to use a func fatal($msg) subroutine in conjunction with caller, to handle fatal errors in some of my programs, and Method::Signatures causes it to sometimes misreport the line from which the error occurred, which schwern alludes to in your link about making it into a wrapper, so it's nice to finally have confirmation of this!

        In lieu of this I will try Function::Parameters with the hopes of using it as a replacement. I already like the idea of typing one less char for functions! (fun instead of func)

        say  substr+lc crypt(qw $i3 SI$),4,5
Re: sub variables
by karlgoethebier (Abbot) on Jul 23, 2017 at 12:51 UTC

    I'm a bit surprised why nobody mentioned signatures:

    #!/usr/bin/env perl # http://perlmonks.org/?node_id=1195793 # $Id: signatures.pl,v 1.3 2017/07/23 12:44:30 karl Exp karl $ use strict; use warnings; use feature qw(say); use signatures; say $^V; say nose(q(cuke)); sub nose ($cuke) { $cuke; } __END__

    But perhaps i missed the point because i don't know Perl6.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      I'm a bit surprised why nobody mentioned signatures:

      LanX did, in the very first reply.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        No I didn't.

        KGB is talking about a module confusingly released under the namespace of pragmas.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

        OK, i didn't follow the link. But please see below above...

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1195793]
Front-paged by stevieb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-12-04 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?