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

Ok, we use FastCGI, so when calls are made the output neds to go to stardard out. The way most of our stuff works is we have a take_call subroutine which sorts out the call and then calls the appropriate subroutine, which prints out it's own data. I have come to want a way to print something and return in the same call, but am not sure if this is possible. for example
if($foo) { print "1\nError was $foo\n"; return; } #would become preturn "1\nError was $foo\n" if $foo;
and that would print the message to standard out and return.

I have thought about having take_call print the return value if any... but that would be... difficult at this point.

I am guessing there probably is no way to do this, or if there is, it is a nasty hack, but you never know unless you ask :)

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: print and return...
by tachyon (Chancellor) on Sep 27, 2001 at 21:16 UTC

    Just use a do{print and return} block vis:

    sub test { $arg = shift; do{ print "Did preturn!\n"; return } if $arg; print "Did not print and return!\n"; } test(0); test(1);

    You can even do this but you need to watch precedence.

    sub test { $arg = shift; print "Did preturn!\n" and return if $arg; print "Did not print and return!\n"; } test(0); test(1);

    cheers

    tachyon

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

(bbfu) (another way) Re: print and return...
by bbfu (Curate) on Sep 27, 2001 at 21:26 UTC

    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.

      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.

Re: print and return...
by dlc (Acolyte) on Sep 27, 2001 at 21:28 UTC

    Why doesn't

    return print "1\nError was $foo\n" if $foo;

    work for you?

    (darren)

      Well, it's not an equivalent statement--I don't know how precise Ant's requirements are, but assuming your print succeeds (which is probable, though not guaranteed), your code will return 1, and his will return undef.

      As I say, I don't know what the requirements of the function are, but if the code to which this sub is returning makes assumptions about the return value, this is not a good choice.

      That said, I like it. :-)



      If God had meant us to fly, he would *never* have given us the railroads.
          --Michael Flanders

Re: print and return...
by Sidhekin (Priest) on Sep 27, 2001 at 22:34 UTC

    I would do this, if I should need it once:

    do{print, return $_ for "1\nError was $foo\n"} if $foo;

    If I would need it often, I would make a function:

    sub teeprint { print, return $_ for @_ }

    ... and call it like this:

    return teeprint "1\nError was $foo\n" if $foo;

    Update:Um, reading Chemboy's response, I note I may have misunderstood the question.
    ("Good answer, Sidhekin -- too bad it was not an answer to suaveant's question.")
    If you want to just return false, my choice of idiom would be
    print("1\nError was $foo\n"), return if $foo;

    The Sidhekin