Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Trudging along the learning perl path. -- wrong path? :=)

by Discipulus (Canon)
on Apr 15, 2017 at 20:33 UTC ( [id://1188019]=note: print w/replies, xml ) Need Help??


in reply to Trudging along the learning perl path.

welcome AnonymousMonk,

First of all it is a good thing you want to learn experiencing but, BUT! trying to do something without using the simpler, acclaimed way (an hash for uniqueness) maybe is not so good idea, worst if you are beginning. The satandard way is a FAQ infact (infaq?)

Anyway it is possible for example using the smart match operator (it is sperimental) which syntax is ~~ see Smartmatch-Operator and act, in the form $scalar ~~ @array like if array contains scalar

my @arr = (0,1,2,3,1,2); my @uniq; foreach (sort{$a<=>$b} @arr){ unless($_ ~~ @uniq){push @uniq, $_} } print join ' ', @uniq;

This is very similar to another existing builtin tool any from List::Util (update: fixed from Utils to Util) core module:

use List::Util qw(any); my @arr = (0,1,2,3,1,2); my @uniq; foreach my $it (sort{$a<=>$b} @arr){ unless(any{$_ == $it}@uniq){push @uniq, $it} } print join ' ', @uniq;

I say this because it is very important and crucial to know which Perl tool to use for a task: this can save hours of your precious time: get the habit to read across perldoc to know everyday more: functions tutorials, core modules.. perldoc save your day!

Then, instead of start spending several hours trying to get uniqness without using an hash it will be by far better to read some good manual like ModernPerl.pdf the free book, or to get a copy if the even old PerlCookbook (a must have to get a wide panorama of solutions and fields to play with).

I say this because me too i'm somehow a Perl-just-for-fun (I use at work too but when conceiled and i just show perl result and benefits when i've done).

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Trudging along the learning perl path. -- wrong path? :=)
by Anonymous Monk on Apr 15, 2017 at 20:53 UTC

    Hi Disciplus

    So can I safely assume that the good Perl developers use these modules and these are not training wheels for new learners? Meaning professional Perl guys too use modules and they rarely roll their own stuff (unless needed)?

      So can I safely assume that the good Perl developers use these modules and these are not training wheels for new learners? Meaning professional Perl guys too use modules and they rarely roll their own stuff (unless needed)?

      Yes, of course, if they remember that they exist, while solving a problem. For a good programmer, pulling unique values out of an array is an easy thing, and it is written quickly. If it is a small bit in the overall task, they just write it inline, and done. But if it is to be done all over the place, and/or the program deals extensively with lists, they will pull in List::Util for sure, instead of writing subroutines already written.

      Good programmers know when to use a module; and good programmers also understand the modules they are using. They use modules, because they are lazy and won't solve things already solved; because they are impatient and want to see their job done, so they just include things already done; and because doing that, their hubris can unfold making just their own code bullet proof - because the modules they include are tested elswhere, and they avoid the technical debt of maintaining, testing and improving stuff written by somebody else: it is somebody elses burden.

      Of course, paraphrasing Fred Allen, "CPAN is a medium because anything well done is rare" - there surely are lousy modules on CPAN, but many gems. To tell them apart, you need expertise, and you get that by experience, which besides trial and error and reading documentation means learning from others. So, yes, modules are also training wheels for new learners, and I recommend reading their source code. If after reading you frown upon them, or feel enlightened, that's a good sign; but beware: you might frown for the wrong reason, or have been enlightened by false lights.

      update: s/List::Utils/List::Util/ - thanks Discipulus

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      Hello again,

      While i'm not a professional perl developer, but i bet yes; i saw them.

      Then, have you seen my signature? ;=)

      But look from another perspective: having or well using a module is a little price you pay, generally. If you use core modules, which every perl incarnation has ( http://perldoc.perl.org/index-modules-A.html ), the way is super plain: they are, for sure, better implemented than my or your code, they are tested a lot, they will remains stable during ages..

      Normally a well desinged module push in just the code you want like in use List::Util qw(any); so you pay quite nothing.

      CPAN is full, leterally, of useful modules too; here is preferable to choose carefully a well developped module, a famous and widely tested, or be able to understand what it is really doing: the risk to indroduce something unxepcted is present; with core modules is rare (by experience if i find something wrong with perl itself and it's core module i start looking for what i misunderstood about..).

      Experimental features are by other hand to avoid unless playing with the language to learn.

      I smiled the last time i was called Disci+ anyway; Discipulus is ancient latin word meaning pupil or student ;=)

      PS: another good way to learn is creating an account on perlmonks.org asking and observing questions from others and their replies

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      "Meaning professional Perl guys too use modules and they rarely roll their own stuff (unless needed)?"

      I'm not officially a professional Perl guy, but I promise you, the best of the best use what is already available, as long as it has a good test suite, an extremely high test pass rate, along with very good documentation and even better if the distribution's author is reachable for questions and feedback.

      Regarding the "rarely roll their own stuff", that's not just "unless needed". I have written distributions on the CPAN not because I needed it, and not because there are already duplicates, but because I needed/wanted to expand my knowledge on a specific area, and at the time, I had a need for a module that would allow me to gain that experience while writing something that was already written.

      Not all of my duplicate efforts have made it to the CPAN, but many have. There's nothing wrong with that, it's just a different way to achieve the same goal. I typically note the other modules that do the same thing in my documentation, and in some cases I've had authors of the similar distributions contact me to discuss my own approach. Re-writing something isn't always a waste of time is what I'm saying here.

      I'm not necessarily for re-creating the wheel, so when it comes to the basics and re-learning, knowing completely how to integrate other modules into your code and use other APIs are just as important as being able to do a print "hello, world!\n"; imho.

      Just as important however, is understanding and fluently using the common idioms, as some things just aren't worth trying to re-work/reproduce and there are better things to re-invent for learning purposes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-19 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found