Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can i make this more efficient with a special variable?

by kirbyk (Friar)
on Jan 10, 2006 at 21:09 UTC ( [id://522322]=note: print w/replies, xml ) Need Help??


in reply to Can i make this more efficient with a special variable?

I don't know of any special variable that means 'the value from the last if test expression'. You could do:
if ( $name = get_name() ) { print "\n My name is $name"; }
And that wouldn't even be hard for others to read and maintain.

Also, is there a reason you don't use return in sub get_name? I know you don't have to, but it makes it _much_ easier to detect bugs if you insert new lines at the end of the sub (or, someone else who isn't paying enough attention to your code does.) I'd generally consider always saying 'return' when you want to return a value as one of those habits of highly successful perl programmers.

-- Kirby, WhitePages.com

Replies are listed 'Best First'.
Re^2: Can i make this more efficient with a special variable?
by polettix (Vicar) on Jan 11, 2006 at 02:41 UTC
Re^2: Can i make this more efficient with a special variable?
by nmerriweather (Friar) on Jan 10, 2006 at 21:23 UTC
    ok. i thought the value from the last if expression would be some sort of special variable in the local block. nice to know there's not.

    the solution you suggested looks to work well for my needs.

    as for the formatting of the function... it's simple--

    typically more people here yell at me (and others) for writing " return $var " than " $var "
      ok. i thought the value from the last if expression would be some sort of special variable in the local block. nice to know there's not.

      Well, not that nice, but I've never really felt the lack of such a beast. OTOH you can do an aliasing in a syntactically sweet enough way as hinted at the very end of my other reply.

      typically more people here yell at me (and others) for writing " return $var " than " $var "

      Right! it's a very common and unambiguous perlism. I'm all with you, as you can see in my comment to his reply.

Re^2: Can i make this more efficient with a special variable?
by blazar (Canon) on Jan 11, 2006 at 09:21 UTC
    Also, is there a reason you don't use return in sub get_name? I know you don't have to, but it makes it _much_ easier to detect bugs if you insert new lines at the end of the sub (or, someone else who isn't paying enough attention to your code does.) I'd generally consider always saying 'return' when you want to return a value as one of those habits of highly successful perl programmers.

    This is by large a matter of personal taste/preference. I'm not striving for "extreme" conciseness, as that would be "golf" and doesn't make for clarity, but the "Right(TM)" degree of conciseness, which includes avoiding redundant or unnecessary syntax, does make for it. In particular the last line of a sub is already visually distinct enough and I generally consider never saying return unless I want to explicitly return prematurely.

    It's not exactly the same thing but the advice of always returning reminds me of the cargo-culted habit of always including an unnecessary

    exit 0;

    line at the end of one's scripts.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-18 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found