in reply to Re^6: Recomendations For Learning perl?
in thread Recomendations For Learning perl?

the second argument that needs to be passed. Is this just the length of the name?

No, there's only one argument expected. That line of code is the program's error checking. It does two things, but acts on the same variable both times.

( my $name = $ARGV[0] ) && length $ARGV[0] > 2 or die "Usage: `$0 <nam +e>` (min. 3 letters)";
What's happening here is we are evaluating the truth of an expression without using an explicit if statement.
my $name = $ARGV[0]
will evaluate to false if the first argument passed in is empty (i.e. non-existent) because then the value of $name will be false (so `0` would fail also)[1]. If that happens, the or die ... kicks in. But if it evaluates to true, then
&& length $ARGV[0] > 2
is evaluated for truth, so at this point we know we have some kind of value in the arg passed in, and now we check that it's at least three characters long. If the second part of the evaluation fails, the whole thing fails (because of the &&), and again we go to or die ....

Finally, in our error message emitted upon dying, we use $0, the global variable that holds the name of the currently executing script, and explain what the problem was.

Amazing how much useful stuff you can do with one line of Perl, isn't it?

[1] So in Perl you can evaluate truth very concisely:

while ( 1 ) { # runs forever ... } if ( 0 ) { # won't run ... } sub foo { return 'something'; # or maybe not } if ( foo() ) { # only runs if foo() returns something ... }

Hope this helps.

edit: added missing parens


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^8: Recomendations For Learning perl?
by adamZ88 (Beadle) on Dec 08, 2016 at 20:18 UTC

    Wow! I totally missed that. Yes, it is amazing how one line can achieve so much. Even as a beginner, I feel that I pressure myself to keep my code as concise as possible, thus, I like to achieve a lot with little code, though it does get confusing at times. Your breakdown of the program is very detailed and helpful! I was eager to try the program myself, so I installed the recommended module, copied the code and ran it. I did not have words installed, so I took care of that as well. The output is not as expected. I tried Steve's one-liner below and that works correctly for what it is supposed to do. But the output for your program is below:

      Even as a beginner, I feel that I pressure myself to keep my code as concise as possible

      this can be a sign of the dark size of the source..

      Concise for humans does not immediately means faster for compilers; speed is one of last priorities and must be measured.

      When you code your goal is quality.

      Concise and clever musto go after readable and maintenable see this post thread for an example.

      Once some monks said: Dont code at your best. Being to debug twice difficult than write code, you'll not be able to debug, by definition

      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.

        This post was quite helpful. I appreciate your recommendations. I do tend to assume that my "efficiency" will result in time saving. I will rectify this.

      Please show the first few lines of the word file. And the command you ran on your command line. It looks like it's searching for the number `1` :-/ What OS are you using?

      And note that you don;t have to use the <spoiler> tags for only a few lines, say up to about 50 :-)

      The way forward always starts with a minimal test.