in reply to print and return...

Another way...

print "stuff" and return if $foo;

(Works because if is a statement modifier and and creates an expression, thus if has a lower binding (statements can contain expressions but not the other-way-round); more or less.)

Update: Erp. Changed comma operator to and since the comma didn't work. :-/ Same principal, but order of execution was not guaranteed with the comma, so it was returning before printing.... grr.

Update2: Of course, neither mine, nor tachyon's methods will return if the print fails (in can, but almost never does in practice). *shrug*

Update3: Argh! :-) The real reason why the comma operator didn't work was that I was evaluating the return value of return as the last argument to the print statement. The following does work (and is what I intended all along ;-p )...

print("stuff"), return if $foo;

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
Re: (bbfu) (another way) Re: print and return...
by tachyon (Chancellor) on Sep 27, 2001 at 22:00 UTC

    I beg to differ. The do will always work, it's just the print and return that could fail if print fails but as you say how often does that happen? return print "preturn" if $arg does not suffer from this problem and also works. So there are at least 4 idioms to do this.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      I didn't mean to imply that the do wouldn't work, of course. I only meant that you also used the print and return form and that it might fail even inside a do block. Update: Erp. I just re-read your post and saw that the actual code you suggested did not use and so it, of course, wouldn't suffer from that problem. Mea culpa!

      You're right about return print "bar" if $foo, of course. That's probably the "best" way to do it, in that it never has a chance of failing but it's a little less clear what's going on... *shrug*

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.