Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

perl 5 suggestions?

by iaw4 (Monk)
on Aug 17, 2016 at 20:32 UTC ( [id://1169949]=perlquestion: print w/replies, xml ) Need Help??

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

is there a simple way to suggest perl5.28+ changes? https://dev.perl.org/perl5/news/ ends with 5.16.2 . because I know some perl5 maintainers are reading this, can I suggest two things?

  1. ignore the perl6 basic types after my/our, so if I write my `my Str $a;`, it will be treated as `my $a` and not complain. It's not particularly useful, except in building a mental and code bridge to perl6.
  2. allow adding specific variables to no strict 'vars';

this makes sense only if it is easy to do. otherwise, it ain't worth it.

regards, /iaw

Replies are listed 'Best First'.
Re: perl 5 suggestions?
by dcmertens (Scribe) on Aug 18, 2016 at 12:18 UTC
    Perl already supports #1 if you declare the packages ahead of time:
    use strict; use warnings; package Str; package main; my Str $name = 'David'; print "Hello, my name is $name\n";
    I was really surprised to find that this worked. It's actually documented, somewhat, in the explanation of my. Note that my solution was inspired by Ingy's "the" module.
Re: perl 5 suggestions?
by hippo (Bishop) on Aug 17, 2016 at 20:53 UTC
    allow adding specific variables to no strict 'vars';

    I'd be interested to know your use case for this - care to share? None springs to mind.

      I thought the same thing. I mean, it just feels like a way to do something where there's most certainly a better and safer way.

Re: perl 5 suggestions?
by stevieb (Canon) on Aug 17, 2016 at 20:49 UTC

    The first one doesn't really make sense to me, but there is a way you can disable strict 'vars' for specific areas of your code (I've never had a need for no strict 'vars'; before personally, usually no strict 'refs';). Note that all variables outside of the tiny scope (block) that the no strict ... is in is still under the enforcement of strict 'vars', ie. you need to pre-define before using them.

    use warnings; use strict; { no strict 'vars'; $x = 10; # look Ma, no 'my'! print "$x\n"; }
Re: perl 5 suggestions? (hack it yourself)
by LanX (Saint) on Aug 18, 2016 at 13:35 UTC
    Expanding on dcmertens' suggestion:

    This hack shows how to define ONE module (here "Classy" YMMV) which creates as many types and package variables in the callers package as you want to.

    use strict; use warnings; use lib '.'; use Classy ; my Str $a ="Goliath"; # no compilation error $var //= 'Philistine'; # no compilation error print $var;

    module

    package Classy; use strict; use warnings; sub import { my $pkg = (caller())[0]; eval 'require Str'; no strict 'refs'; *{"${pkg}::var"} = \(my $var); } 1;

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-24 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found