Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Okay, so Page 9 [perl.com] says that // is no longer a m//, but instead a short form of rx//.

    It also talks about delayed evaluation of the pattern.

    So, given that almost all the example patterns use /foo/ and not m/foo/, what is happening, and when is it happening?

    • by Damian (784) on 2002.06.06 3:04 (#9244)
      So, given that almost all the example patterns use /foo/ and not m/foo/, what is
      happening, and when is it happening?


      /.../ now *always* returns a regex object.
      But a regex object in a void, boolean, string, numeric, or =~ context
      immediately matches against the current topic.

      So you get:
          /pattern/;                 # void context    -> automatch
          if /pattern/ {...}         # boolean context -> automatch
          %hash{/pattern/} = 1;      # string context  -> automatch
          if /pattern/ < 1 {...}     # numeric context -> automatch
          $str =~ /pattern/;         # =~ context      -> automatch
          when /pattern/ {...}       # =~ context      -> automatch
      and:
          @pats = (/pat1/, /pat2/);  # list context    -> no automatch
          %hash = ( pat => /pat/ );  # scalar context  -> no automatch
          $pat = /pat/;              # scalar context  -> no automatch
          given /pat/ {...}          # scalar context  -> no automatch
      And if you're ever unsure, just be explicit:
          $pat = rx/pat/;            # definitely assigning a regex object
          $pat = m /pat/;            # definitely assigning the match result