Abdullah Shaik Abdullah Shaik - 5 months ago 23
Perl Question

return Boolean variable and pass alphanumeric variable to the function in perl

I wanted to return boolean vairiable in the if condition.. Also I want to pass argument to the subroutine in perl. When I run the script, the error states

Bareword "S1234567D" not allowed while "strict subs" in use at perl_validnric3.pl line 16.
Execution of perl_validnric3.pl aborted due to compilation errors.

my code below

use strict;
use warnings;
sub isValueNRIC
my $nric_no = scalar(@_);
if ($nric_no =~ /^[STFG][0-9]{7}[A-Z]$/)
return 1;
return 0;

I know that there is no such thing as boolean variable in perl. Instead I use true as 1 and false as 0. I found these boolean variable from multiple stack questions. Is it the correct way??Also how to pass alphanumeric as an argument to the function (sub)??????I also want to put multiple alphanumeric to the sub so as to keep checking every id to check whether it is valid.Please help me.. thank you....

Answer Source

You want to pass a string into your subroutine. Strings in Perl (as in pretty much any other language I've come across) need to be in quotes. So when you have:


I think you meant:


Having an unquoted string in your code is often a sign of a bug, so one of the things that use strict does is to throw a fatal error if it finds one. This is a good thing. You should take note of the problem and fix it.

I also notice this line, which will give you problems:

my $nric_no = scalar(@_);

scalar(@_) will give you the number of elements in @_ - which will be 1. What you want is either:

# Get the first element of @_
my $nric_no = shift(@_);


# List assignment
my ($nric_no) = @_;

To answer your actual question, 1 and 0 are perfectly acceptable Boolean values, but you might consider using a bare return without a value for the false option. Perl will see that and return an appropriate false value.

You seem to be asking a lot of very basic questions. Perhaps you should step back a bit and take the time to read a good Perl book before trying to write much more code.

Update: Oh, and one other tip. If you have a Perl error that you don't understand, you could try adding:

use diagnostics;

to your code. That will give you a more detailed explanation of the error. But remember to remove it before putting the code into a production environment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download