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

Hi,

Idiocy on my part, no doubt ... the intention is that the following simplistic demo script should output "Success" when run on perl 5.8.x, and output "Ok" when run on perl 5.00505:
use strict; my $var = ''; if($] > 5.007) { if(open (my $mem, '>', \$var)) {print "Success\n"} } else {print "Ok\n"}
It does as desired on perl 5.8.x, but fails to compile on 5.00505 (because there are too many arguments being supplied to open).

I gather the best solution has something to do with string eval ... which I've never used (to any great effect, anyway). And the 'eval' documentation didn't produce any "Aaaaah, I see" moments for me. (Changing the open call to a 2 argument form is not a viable solution :-)

Cheers,
Rob

Replies are listed 'Best First'.
Re: Compile time problem
by FunkyMonk (Bishop) on Oct 06, 2007 at 15:13 UTC
    String eval compiles at runtime and will avoid the error in older perls (you'll have to move the declaration of $mem to avoid a scoping issue).
    my $var = ''; my $mem; if($] > 5.007) { if( eval q{open ($mem, '>', \$var) } ) {print "Success\n"} } else {print "Ok\n"}

      Thanks, FunkyMonk ... that looks like the "Aaaah, I see" moment I was after.

      Does that "q{}" stuff work ok on 5.00505 ? (I don't actually have a 5.00505 to test with ... though I may yet build one.)

      Cheers,
      Rob
        Good point. I don't know when q{} (etc) made their first appearance in perl.

        Just use appropriate quoting and escaping if it doesn't work, but I'm sure some Monk will appear with the answer before too long.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Compile time problem
by BrowserUk (Patriarch) on Oct 06, 2007 at 16:56 UTC
Re: Compile time problem
by perlfan (Parson) on Oct 06, 2007 at 15:13 UTC
    Doesn't the first argument of open have to be a file handle, i.e, bare word (no sigil or my)?

    Or does your problem have to do with evaluating $]?
      See open. The 3 argument open first appeared in perl 5.6 (first appearance in a production perl, anyway)

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Compile time problem
by ikegami (Patriarch) on Oct 08, 2007 at 07:39 UTC
    One solution is to move the problem code into a module and include the module conditionally.
Re: Compile time problem
by graff (Chancellor) on Oct 06, 2007 at 16:07 UTC
    I'm sorry, but I don't quite understand what your real goal is here. Do you intend to open the output file only if the perl version is > 5.007, otherwise don't open the output file?

    If so, I'm not sure that the benefit of using a lexically scoped file handle is worth the hassle if the code still has to run on older perl versions. Do you have some compelling reason for needing to use a lexically scoped file handle?

    OTOH, if the goal is "open the file this way if perl is > 5.007 and open it the old way otherwise", that would be a really bad idea, because then you'd have to check the version and use eval every time you write to the file handle. Don't do that.

    UPDATE: Apologies again... I just noticed this bit:

    (Changing the open call to a 2 argument form is not a viable solution :-)

    This seems to answer my first question -- no way you would want to open the file if running under an older version (which does not support 3-arg open). Perhaps you could elaborate on why a 2-arg open is not viable, and how that relates to the need to run under older versions of perl.

    last update: D'oh!! why didn't I notice "\$var" the first time?! Still, I'm curious why you would add this in a script that is supposed to run under older versions...

      I'm curious why you would add this in a script that is supposed to run under older versions

      It's all to do with the FileHandle::Fmode test suite. The module should work ok with perl 5.00505, but offers additional features for later builds of perl. The test suite needs to test the additional features on recent builds of perl - but to not test those features on ancient builds of perl (where such tests break perl).

      Cheers,
      Rob
Re: Compile time problem
by Anonymous Monk on Oct 06, 2007 at 20:53 UTC
    my $mem; my @stuff = \$mem, '>', \$var; open @stuff ..