Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 exploiting the Monastery: (3)
As of 2026-01-15 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (118 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.