develooper Front page | perl.perl5.porters | Postings from August 2001

Re: Interpolating function call in here documents causes out-of-orderexecution

Thread Previous
From:
Jeff 'japhy/Marillion' Pinyan
Date:
August 30, 2001 20:42
Subject:
Re: Interpolating function call in here documents causes out-of-orderexecution
Message ID:
Pine.GSO.4.21.0108302339580.7601-100000@crusoe.crusoe.net
On Aug 31, Yusuf Goolamabbas said:

>#!/usr/bin/perl
>
>sub main() {

Um, the () here is probably not intended.  And Perl programs don't need a
main().

>$key = 5 ;
>print <<"TARGET" ;
>Foobar
>Snafu
>${\(interp($key))}
>TARGET
>}
>
>sub interp {
> my ($arg) = @_ ;
> print "Hello World, $arg \n" ;
> return;
>}
>
>main();
>
>gives the following output
>--
>Hello World 5
>Foobar
>Snafu

Right.  Your function interp() PRINTS something, and then returns.  That
happens before the completion of the print << "TARGET".

If you wrote your code as:

  print << "END";
  a
  b
  @{[ foo() ]}
  END

  sub foo {
    return "c";
  }

Then you would get

  a
  b
  c

as your output.  Because you're printing in the interp() function itself,
you're getting what appears to be out-of-order data.

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About