Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is this code correct

by davido (Cardinal)
on Mar 22, 2021 at 19:30 UTC ( [id://11130152]=note: print w/replies, xml ) Need Help??


in reply to Is this code correct

It's incorrect because $sixth has never been initialized, so will always be undefined at the point you're attempting to assign its value to $a.

Did you mean this instead?

my $sixth; if (defined $a) { $sixth = $a; } else { $sixth = 3; }

That would make more sense. And if it's what you actually intended to type into your question, yes it is correct. The choice of $a as a variable name is terrible though, because it is the same name used in Perl's sort routine, and is exempt from strictures. Choose any name that isn't $a or $b. Even $c would be ok.

So while my rewrite of your code is correct (assuming it was your original intent and you just wrote it wrong for your question), it's not the most Perlish. In Perl we would do something like this:

my $sixth = defined $a ? $a : 3; # or this my $sixth = $a // 3;

Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130152]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 20:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found