anacondy_wly:

The s#xxx#yyy# is just an alternate form of s/xxx/yyyx. With the s and </c>m</c> operators, you can specify an alternate set of delimiters for your regex. It's often used to make an expression easier to read--for example, if you're altering strings with slashes in them (think directory paths), then you'd need to prefix each '/' with a '\' inside your regular expression if you used the typical '/' delimiter. If you choose a different delimiter, though, it can be easier to read. Note: You can also used curly braces, too. These three expressions are identical:

$t =~ s/\/home\/roboticus\/tmp/\/home\/postgresql\/tmp/; $t =~ s{/home/roboticus/tmp}{/home/postgresql/tmp}; $t =~ s#/home/roboticus/tmp#/home/postgresql/tmp#;

So since the grep statement gets a list of aliases, the statement is first altering the value, then checking whether it should be included in the output list. A trivial example:

$ cat t.pl #!/usr/bin/perl my @a = qw(apple banana cherry durian); my @b = grep { s#(a.)+#x#g; m/x/ } @a; print join(", ", @b), "\n"; $ perl t.pl xple, bxa, durix

Finally, the statement doesn't look like it's syntactically correct, but as I understand it the grep command is doing another transformation: It seems to be setting the MEMBERS item to the number of an array ref of a list of the SUBKEYS. Then, since grep evaluates each element in scalar context, the grep statement keeps the item if and only if there are any SUBKEYS. However, $ isn't a variable, so the first bit of the code block looks incorrect. Secondly, since nothing changes $RegObj, I don't see what good is going to happen: it seems to me that either all elements would be retained in the list, or none of them would.

If no objects are supposed to be retained, then I'd simply use a for loop to process the items. If all items should be retained, then I'd replace the statement with a map statement.

Just my $0.02.

Update: Jiminy, I'm slow. Three answers before I replied, and they show that I goofed. I also obviously got it a bit wrong. (That's what I get for not testing.) I was expecting a variable expansion in the match portion of the regex. (I don't know *why* I expected it, as '$' is the 'end of line' match character. Perhaps I was thrown off by the use of something that looked like a variable name on the right side.) So anyway, I've stricken the bit that's patently incorrect, and underlined a bit of text I inserted to partially correct it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: can understand one line of code by roboticus
in thread can understand one line of code by anaconda_wly

Title:
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, details, 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, summary, 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.