3 ms·
> I don't miss perls halfarsed function args. You mean you don't like writing things like... sub foo { my ($a, $b, $c) = shift; ?
by jstanley 1y ago
> I don't miss perls halfarsed function args.
You mean you don't like writing things like...
sub foo {
my ($a, $b, $c) = shift;
?
- oneshtein 1y agoYes, I hate to write sub foo($$$) { my ($a, $b, $c) = @_; } Where @_ is array of arguments, and ($$$) is function prototype (3 scalars are expected).
- AndrewDavis 1y agoPerl has subroutine signatures now. You can write sub foo ($x, $y, $x) { ...} It's just syntactic sugar, so you still can't pass in multiple lists, and the list must be the final parameter.
- deleted 1y ago[deleted]
- AndrewDavis 1y agoNot sure if you've deliberately put in two bugs there haha 1. shift only shifts off the first element. 2. (if classify this as a bug) using $a and $b are frowned upon because they're the default variables when using sort.
- jstanley 1y agoHa, (1.) was deliberate but I'd forgotten (2.)
- sshine 1y agoYeah, you probably want my ($x, $y, $z) = @shift; :-D
- rurban 1y agoFor reference: Valid is either sub () { my ($x, $y, $z) = @_; or sub ($x, $y, $z) {
- kqr 1y agoWhy the empty prototype in the first alternative?
- sshine 1y agoI should have said my ($x, $y, $z) = @$_[0]; but I was making a joke. If $_[0] is an array reference, @$_[0] becomes a list. The joke was a reference to most wrong Perl programs being valid for something. Hence the meme: "93% of Paint Splatters are Valid Perl Programs" https://www.mcmillen.dev/sigbovik/ https://www.mcmillen.dev/sigbovik/
- 7952 1y agoOh wow I had forgotten about default variables. Such a lovely and inscrutable idea.
- KaiserPro 1y agoIndeed! It just grinds my gears that _I_ need to check to see if the caller has given me all the required bits. That seems like something the language should do. I understand that it does give you a lot of flexibility, but Hnnnnnnnnn (from what I recall object oriented perl doesn't give you this flexibility, but I'm not sure, as I never really did it. )
- jstanley 1y agoObject oriented perl gives you exactly the same flexibility. If you have a module called "My::Module", and you call "My::Module->some_method", then you'd implement that like: sub some_method { my ($pkg, @args) = @_; i.e. the module gets passed (as a string) as the first argument. You can then call $pkg->some_other_method; And similarly, if you have an object "$foo" you would call it like "$foo->some_other_method" and implement it as: sub some_method { my ($self, @args) = @_; i.e. the object gets passed as the first argument. And you can call "$self->some_other_method" with it. A minimal class is just: package My::Module; sub new { my ($pkg, %opts) = @_; my $self = bless \%opts, $pkg; return $self; } 1; Don't let them tell you you need Moo or Moose or Mouse or fields.pm. Hand-rolled objects is the way :)