Brother Sharky has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, This sub (findTheTypeByTheName) returns null(last line, b4 closing curly)I want to use this null in a function to print "Name not found in the code named function below.
#----------------------------------------------------------- sub findTheTypeByTheName{ my $tree = $_[0]; my $name = $_[1];#$name is each line from the config file my $assignment = $tree->{assignment};#$assignment is an array #----------------------------------------------------------- Function: #open Config file open(TXTDOC, $ARGV[1]) || die "Cant open file" . $ARGV[1] . " for comp +aring!\n"; while (<TXTDOC>){ my $name = $_; chomp $name;#remove newlines $name=~s/(\s+)\z//;#rem white space $typeMatched = findTheTypeByTheName($tree,$name); prettyPrintTheType($typeMatched); my $noName = null;#tried to assign to scalar if ($noName){ print "$name . "Is not matched to an ASN name"; } } foreach $theAssignment (@$assignment) { if ($name eq $theAssignment->{ident}{__VALUE__}) { return $theAssignment->{type}; } } return null; }
thx 4 the help

Replies are listed 'Best First'.
Re: using return types
by dash2 (Hermit) on Jan 21, 2002 at 20:27 UTC
    I think you want to use "undef" and then
    unless (defined($result)) {...}
    but to be honest your code is a bit messed up, you seem to have got the main body confused with the subroutine - or your formatting has problems.

    dave hj~

      Thanx to all 4 the hlp.
        dash2 gave an example, check out "perldoc -f defined"
Re: using return types
by derby (Abbot) on Jan 21, 2002 at 20:23 UTC
    Brother Sharky,

    The sub doesn't return null (as in the traditional sense of null) but the bareword null. This function will cause the interpreter to barf if 'use strict' is in place. I would suggest either quoting null (bad idea from an idiomatic viewpoint) or change the function to just fall off (then you can check for definedness).

    but to answer your question, you would need to do a string comparison:

    my $ret = findTheTypeByTheName( "whatever" ); if( $ret eq "null" ) { print "Name not found in the code named below."; }

    but I would suggest dumping the bareword nulls all together - they just feels creepy.

    -derby