http://qs321.pair.com?node_id=63964

vonman has asked for the wisdom of the Perl Monks concerning the following question:

if( m#^coclli=(.*)# && $coclli eq "" ) { $coclli = $1; }
I have inherited a piece of code that "supposedly" works.

There are no variables or subroutines named m. What hould this code do? I see it as m then a comment for the rest of the line. Any ideas?

Thanks

Edit 2001-03-12 by tye

Replies are listed 'Best First'.
Re: What the heck does this mean?
by japhy (Canon) on Mar 13, 2001 at 01:28 UTC
    Running it through the deparser, I get:
    if (/^coclli=(.*)/ and $coclli eq '') { $coclli = $1; }
    It's using a regex to assign to $coclli if the variable isn't already assigned.

    The deparser is available in Perl 5.005 and up, as:

    japhy% perl -MO=Deparse program_name


    japhy -- Perl and Regex Hacker
      Basically, in English, this code reduces to:

      If the string $_ starts with "coclli=", and then "some characters", and the variable $coclli is equal to the empty string, set the variable $coclli to the (any-amount-of-characters).
Re: What the heck does this mean?
by myocom (Deacon) on Mar 13, 2001 at 01:33 UTC

    perldoc -f m tells you to look at the perlop manpage. In short, it's the match operator.

Re: What the heck does this mean?
by vonman (Acolyte) on Mar 13, 2001 at 01:29 UTC
    There should be a newline after the eq"")and the $1;. Txs
Re: What the heck does this mean?
by vonman (Acolyte) on Mar 13, 2001 at 01:34 UTC
    I think what is happening is that the original code must not be displaying characters correctly on my machine. So what should I replace m and # with?
      What are you talking about? m// is the pattern match operator, and you can use characters other than / for the delimiter. Thus, the code can be:
      /^coclli=(.*)/ # or m!^coclli=(.*)! # or m<^coclli=(.*)>
      Perl can be as eccentric as the programmers who use it.

      japhy -- Perl and Regex Hacker
      perl allows you to specify any character as the regexp seperator. / is just a suggestion and I guess is most widely used? Either way the perldocs say you can use m## just as easy as m// if, for instance you wanted to match a string that had a lot of /'s in it and didn't want to \/ them all - m#///# would work and is a little easier to read than m/\/\/\//.
      Adam