tags:

views:

278

answers:

4

What is the best way to achieve sscanf-like functionality in perl?

I am looking now looking at the sscanf module,

Which is better,

Option-1: Going sscanf way?

Option-2: Regex way? [I am a beginner when it comes to Regex]

A: 

You can use the regular expression.

If you want to use the sscanf module, you need to download that module. So it will occupies the space.

Instead of this, you use the regular expression only.

According to me regular expression is the better way

muruga
+1  A: 

There is more than one way to do itTM.

However, regular expressions are quite more versatile than sscanf. They are also not difficult to learn.

In Perl, an attempt to mimic the functionality of sscanf would most likely make heavy use of regular expressions.

Svante
+1  A: 

Guess what? CPAN have a sscanf module for Perl!

Regex is better, but TIMTOWTDI.

J-16 SDiZ
+6  A: 

The Perl documentation includes this tidbit:

  • scanf

    scanf() is C-specific, use <> and regular expressions instead, see perlre.

I would say that learning regexes is well worth it; they are part of the core strength of Perl and are a useful tool elsewhere too.

ephemient
Note the output of `perl -MPOSIX -e 'scanf'`
Greg Bacon