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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

gam3 demonstrates a great method. ++. Perl Best Practices advocates avoiding using $1, $2, etc. whenever you possibly can, and that is a great way to do it. IMO it's sort of a "poor man's" named capture (one of the few regex features that Perl "lacks"), and you never have to refer to the numbered variables themselves except in some special cases like in the replacement clause of the substitution operator.

I am also particularly fond of a one-step syntax to get the results of a substitution on a variable without affecting the original variable. It's hard to explain but I'm sure you've either done it or wanted to do it at some point. Here's an example (which I apologize is less than ideal; I wasn't sure if any spaces would turn up in the match from the split):

use strict; use warnings; my @staff = `whoare -g somegroup`; #chomping an array chomps all the elements chomp @staff; foreach my $emp (@staff) { my @fields = split /\s{2,}/, $emp; die "error! number of fields is " . scalar @fields . "! " unless ( scalar @fields == 3 ); #dispense with the opening paren to get the group ( my $def_grp = $fields[1] ) =~ s/\s*\(\s*//; #etc. }
I think of both of those tricks as falling in the same category, though I use the first more often. It came in handy in the very first lines of a CGI script I wrote:
use strict; use warnings; # First things first. If I can't figure out who you are, you're outta + here. my $editor; if ( exists( $ENV{REMOTE_USER} ) ) { ($editor) = $ENV{REMOTE_USER} =~ m/(some regex)/i; } (defined $editor) || die "I can't determine who you are, so you can't +access this area."; #use editor later throughout the program
These examples are probably not the best but it's from code I could put my hands on in short order.

While we're on the topic, I should note that it's a very good idea to use non-capturing parentheses(?:) whenever you want or need to use parentheses but don't need to remember the stuff that matched in there.

I like computer programming because it's like Legos for the mind.

In reply to Re: $1, etc. are not just strings by OfficeLinebacker
in thread $1, etc. are not just strings by ysth

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (2)
    As of 2018-05-26 16:15 GMT
    Find Nodes?
      Voting Booth?