Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Idiomatic Perl?

by mxb (Pilgrim)
on Mar 20, 2018 at 17:20 UTC ( [id://1211356]=note: print w/replies, xml ) Need Help??


in reply to Idiomatic Perl?

As haukex has already pointed out, it's probably best to avoid eval in this case as it could be manipulated to run arbitary Perl code in your example.

hippo has also made a valid suggestion about using the ternary if conditional to make the code easier to read.

Personally, I'd use both of these suggestions, add in the use of say rather than print when appropriate and move the display prompt and return input into a separate function, but that might be over-engineering this learning exercise.

Putting it all together gives the following:

use strict; use warnings; use 5.016; sub prompt_read { print shift; chomp( $_ = <STDIN> ); return $_; } say "RegEx Engine 1.0\n________________"; my $str = prompt_read("Gimme a string: "); my $pattern = prompt_read("Gimme a RegEx: "); say $str =~ /$pattern/ ? "Yes!" : "No!"; say "kthxbye";

Replies are listed 'Best First'.
Re^2: Idiomatic Perl?
by Marshall (Canon) on Mar 21, 2018 at 01:45 UTC
    I don't want to start any "style wars". But my natural inclination would have been to code prompt_read() something like this:
    sub prompt_read { my $prompt = shift; print $prompt; my $response = <STDIN>; $response =~ s/^\s*|\s*$//g; return $response; } Note: these "standard idioms" can be used, but with recent Perl's the more complex regex above is slightly faster. $response =~ s/^\s*//; # these 2 statements do the same $response =~ s/\s*$//; # but slightly slower
    Some Comments:
    • I prefer the bracketing style shown for Perl. For Java I like the more compact form because of all of the little "getter and setter" subs which can take up a lot of space.
    • In Perl, I look for the first line of the sub to understand the input args. Perl doesn't have prototypes like C but this appears to work just fine.
    • Giving a name to a variable is "cheap", "very cheap" in terms of execution speed. I assigned $prompt as the sub's input value. There is nothing "un-Perl" about that at all. Likewise making a new variable $response for the input is "very cheap". I don't assign values to $_ which I consider to "belong to Perl". In for() or foreach() loops I assign my own name for the loop variable rather than $_. This prevents problems in nested foreach() loops as well as providing some more documentation, provided of course that the loop variable name makes sense!
    • idiomatic Perl shows up in the next line. In Python, you have to explicitly import stuff to use regex and then explicitly compile the regex, then use it in another statement. In Perl, regex is "built-into the language". Normal CLI input conventions would be to strip all leading and trailing spaces as well as the line ending. That one statement does both. In a loop, Perl will take care of compiling the regex and not doing that step more than necessary (in most cases).
    It would take a more complex example for the power of Perl vs Python to be demonstrable.

    One point that I have is that well-written idiomatic Perl does not have to be cryptic.

    I don't claim that my style above is better than other styles. This is just one example.

    From a coding standpoint, I did like the idea of splitting out the function of sending a prompt and getting a response. I wrote a more sophisticated version of this awhile back. My expanded routine took a prompt,regex,err_message as input. This handled blank lines, did retries and such things.

Re^2: Idiomatic Perl?
by Anonymous Monk on Mar 20, 2018 at 19:08 UTC
    If you're gonna use globals you're supposed to localize
      Can you properly localize $_? I vaguely recall Rafael Garcia-Suarez mentioning something about "having 'local $_' not working as intended", but never investigated what that means.

      Huh, I didn’t know that! I’ve only ever assigned to $_ in throw away scripts and one liners. Thanks for that, I learned something new.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found