Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks

Re^4: What's the deal with apostrophes?

by dsheroh (Priest)
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Jun 10, 2009 at 11:26 UTC ( #770295=note: print w/ replies, xml ) Need Help??

in reply to Re^3: What's the deal with apostrophes?
in thread What's the deal with apostrophes?

It seems a little odd to me to just change characters for something like that and expect perl to "know" that's what they are there for. Does it really work that way?

Yes, it does really work that way... more or less.

The one catch is that, when you use alternate delimiters, you have to explicitly use either "m" or "s" at the beginning of the regex. The "m" and "s" are the actual operators which (in the right context) trigger a regex operation, so when perl sees those followed by punctuation, it recognizes that the punctuation used will be the delimiter for that regex without needing to expicitly set or reset anything.

So, for example, all of these variations are equivalent:

#!/usr/bin/perl use strict; use warnings; my $text = 'This is a text string.'; print $text =~ m/is a/, " - m//\n"; # Default delimiter, so the "m" can be omitted print $text =~ /is a/, " - //\n"; # Custom delimiters, so the "m" is mandatory print $text =~ m#is a#, " - m##\n"; print $text =~ m!is a!, " - m!!\n"; print $text =~ m^is a^, " - m^^\n"; # Open/close punctuation is used in pairs print $text =~ m[is a], " - m[]\n"; print $text =~ m(is a), " - m()\n"; print $text =~ m<is a>, " - m<>\n";
The print on each regex displays the number of matches found along with the delimiters used:
1 - m// 1 - // 1 - m## 1 - m!! 1 - m^^ 1 - m[] 1 - m() 1 - m<>


Comment on Re^4: What's the deal with apostrophes?
Select or Download Code
Re^5: What's the deal with apostrophes?
by tallCoolOne (Initiate) on Jun 10, 2009 at 13:42 UTC
    That is about the coolest thing I've seen in perl since ice cubes. Pretty amazing really.
    Thanks! That's one of the most helpful, useful tips that I have gotten here. This will make for a lot of simplifying changes in my code from now on.
    *bows in respect and wonderment at the awesomeness of perl, and the generosity of the monks*
[reply]
Login:
Password
remember me
What's my password?
Create A New User

Node Status
node history
Node Type: note [id://770295]
help
Community Ads
Chatterbox
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users
Others avoiding work at the Monastery: (6)
GrandFather
wfsp
atcroft
herveus
Eyck
gnosti
As of 2009-11-21 09:33 GMT
Sections
The Monastery Gates
Seekers of Perl Wisdom
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Cool Uses for Perl
Perl News
Information
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth

Future historians will find that the material characteristic of the current era is...

Aluminium
Plastic
Oil
Water
Carbon dioxide
Copper
Iron
Silicon
Salt
Uranium
Hydrogen
Other

Results (729 votes), past polls