Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

What does this statement do?

by Fian7 (Initiate)
on Jun 10, 2010 at 22:23 UTC ( #844137=perlquestion: print w/ replies, xml ) Need Help??
Fian7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to read a PERL program, and the following line has me confused - what is it trying to do? unless ($nth =~ /^+-?\d+$/ ) { ##Removed this code to make things simpler## }

Comment on What does this statement do?
Replies are listed 'Best First'.
Re: What does this statement do?
by choroba (Canon) on Jun 10, 2010 at 22:52 UTC
    Welcome to the Monastery, Fian7. Please, read and try to follow the basic rules: Markup in the Monastery. Also note there is nothing called PERL here, the language is "Perl" and the interpreter is "perl".
    Concerning the code, it should have probably been:
    unless ($nth =~ /^[+-]?\d+$/ ) { ##Removed this code to make things si +mpler## }
    unless means if not (see perlsyn).
    The =~ operator matches (see perlop) the variable $nth against a regular expression, enclosed in slashes.
    To match, a string can (hence ?) start with a plus or minus sign, then it should have one or more (hence +) digits (\d) and nothing more ($).
Re: What does this statement do?
by linuxer (Deacon) on Jun 10, 2010 at 22:56 UTC
    The regular expression can be explained, e.g.:
    perl -MYAPE::Regex::Explain -wle ' my $re = qr/^[+-]?\d+$/; print YAPE::Regex::Explain->new($re)->explain();' The regular expression: (?-imsx:^[+-]?\d+$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- [+-]? any character of: '+', '-' (optional (matching the most amount possible)) ---------------------------------------------------------------------- \d+ digits (0-9) (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
    So, all in all the code checks if the content of $nth contains an integer with or without a prefix (+ or -); if it does not, then the removed code is executed.

    Documentation: perlretut perlre

    edit: hints about s/PERL/Perl/ and code tags removed, as choroba already mentioned it

Re: What does this statement do?
by aquarium (Curate) on Jun 10, 2010 at 23:45 UTC
    I'd (wildly) guess that this is part of a routine that handles numbers with the code provided executing the block of code only when there is not a valid exponent. An exponent is (typically) such a simple number as that being matched, a signed or unsigned (+/-) integer.
    the hardest line to type correctly is: stty erase ^H

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://844137]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2015-07-31 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls