Effective Perl Programming: Ways to Write Better, More Idiomatic Perl (2nd Edition) (Effective Software Development Series)

Category: Programming
Author: Joseph N. Hall, Joshua McAdams, Brian D. Foy
All Stack Overflow 12
This Year Stack Overflow 1
This Month Stack Overflow 1


by anonymous   2019-01-13

People often try to tie sigils to variable types, but they are only loosely related. It's a topic we hit very hard in Learning Perl and Effective Perl Programming because it's much easier to understand Perl when you understand sigils.

Many people forget that variables and data are actually separate things. Variables can store data, but you don't need variables to use data.

The $ denotes a single scalar value (not necessarily a scalar variable):


The @ denotes multiple values. That could be the array as a whole, a slice, or a dereference:

@hash{qw(key1 key2)}
@{ func_returning_array_ref };

The % denotes pairs (keys and values), which might be a hash variable or a dereference:


Under Perl v5.20, the % can now denote a key/value slice or either a hash or array:

%array[ @indices ];  # returns pairs of indices and elements
%hash{ @keys };      # returns pairs of key-values for those keys
by anonymous   2018-03-19

As hobbs points out, if you're sure that you'll be on 5.10 or later, you can use the built-in check:

 use 5.010;
 use re qw(is_regexp);
 if (is_regexp($pattern)) {
     say "It's a regex";
 } else {
     say "Not a regex";

However, I don't always have that option. In general, I do this by checking against a prototype value with ref:

 if( ref $scalar eq ref qr// ) { ... }

One of the reasons I started doing it this way was that I could never remember the type name for a regex reference. I can't even remember it now. It's not uppercase like the rest of them, either, because it's really one of the packages implemented in the perl source code (in regcomp.c if you care to see it).

If you have to do that a lot, you can make that prototype value a constant using your favorite constant creator:

 use constant REGEX_TYPE => ref qr//;

I talk about this at length in Effective Perl Programming as "Item 59: Compare values to prototypes".

If you want to try it both ways, you can use a version check on perl:

 if( $] < 5.010 ) { warn "upgrade now!\n"; ... do it my way ... }
 else             { ... use is_regex ... }
by anonymous   2017-08-20

Learning Perl, 5th Edition is current up to 5.10. There's even a chapter for the smart match operator, just like you want, in addition to the other new features that a beginner will want. If you want to start learning Perl, that's the book to use. Once you get through it, move onto whatever else you want to do. I'll have Effective Perl Programming, 2nd Edition out around the start of spring too. That will be current up to 5.10.1 (and maybe a little farther since we use at least one 5.12 feature in some code).

You really don't want a book on Catalyst or Moose. Anything you read will be out of date because those projects have a high velocity and the practices change pretty quickly. You're going to need to learn the basics before you dive into the advanced topics, anyway.

To continue as a programmer, you're going to have to get used to not depending on books. I can't write them as fast as stuff changes, so you have to learn from the documentation too. That's just how life is.

by anonymous   2017-08-20

The Encode module has a way that you can try to do this. You decode the raw octets with what you think the encoding is. If the octets don't represent a valid encoding, it blows up and you catch it with an eval. Otherwise, you get back a properly encoded string. For example:

 use Encode;

 my $a_with_ring =
   eval { decode( 'UTF-8', "\x6b\xc5", Encode::FB_CROAK ) }
     or die "Could not decode string: $@";

This has the drawback that the same octet sequence can be valid in multiple encodings

I have more to say about this in the upcoming Effective Perl Programming, 2nd Edition, which has an entire chapter on dealing with Unicode. I think my publisher would get mad if I posted the whole thing though. :)

You might also want to see Juerd's Unicode Advice, as well as some of the Unicode docs that come with Perl.

by anonymous   2017-08-20

I work on Melody which is written primarily in Perl. It's a rather large code base, and I've found the process of learning the Melody code base is identical to any Java system I've worked on.

It really comes down to just working with it, googling when you see behavior you've never seen before and experimentation.

This book is a great reference for picking up Perl in a serious way. It's not very dense and it will teach you a lot about proper Perl development.