[ABE.pm] WTF is this code doing?

Faber Fedor faber at linuxnj.com
Wed Jun 6 07:13:15 PDT 2007


On 6/5/07, Ricardo SIGNES <rjbs-perl-abe at lists.manxome.org> wrote:
>
> Did you know that when you write:
>
>   if ($string =~ /abcdef/) { ... }
>
> It's the same as:
>
>   if ($string =~ m/abcdef/) { ... }
>
> ?


Yeah.


Well, it is.  The m is optional if you use // -- and I say "if you use //"
> because you can use other delimiters:
>
>   if ($string =~ m|abcdef|) { ... } # some other repeated character
>   if ($string =~ m$abcdef$) { ... } # another repeated character
>   if ($string =~ m{abcdef}) { ... } # an open/close pair of characters


Unix 102: Avoiding the leaning toothpick syndrome.

Some pairs are magic, and have special meaning, like m?foo? -- but you can
> look
> into that on your own.
>
> Anyway, m// isn't the only thing that works this way.  Just like // is
> secretly
> m// in the context above, often "" is secretly qq"" and '' is secretly
> q''.
>
>   m  - matching patern
>   qq - quoted string (interpolates scalars and arrays)
>   q  - quoted string (no interpolation)
>
> So!
>
> > $s = q@($t = $s) =~ s/\045/\100/g;
> > print "#!/util/bin/perl\n\$s = q%$s%;$t";
> > @;($t = $s) =~ s/\045/\100/g;
> > print "#!/util/bin/perl\n\$s = q@$s@;$t";
>
> The first @ begins a non-interpolated string, which ends at the next
> @.  That
> program says:
>
  $s = '($t = $s) =~ s/\045/\100/g;'
>      . "\n"
>      . 'print "#!/util/bin/perl\n\$s = ...
>
> and so on.  Does that help?



So I could use q'', q@@, q||. q!! etc instead of q() just like I can use
s''', s@@@, s||| and s!!! instead of s/// ?

--

Faber
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/abe-pm/attachments/20070606/73926bd9/attachment.html 


More information about the ABE-pm mailing list