If I understand you correctly, the END block can make use of lexical variables defined in the file before the END block itself. This would obviate the need for using a BEGIN block to test for the presence/absence on disk of a given file. It would also mean that I could place use strict; in its customary position.

The following little script should illustrate the point in various ways depending on whether the file present is indeed present and depending on whether the croak call is live or commented-out.

#!/usr/local/bin/perl use strict; use warnings; use File::Copy; use Carp; use Test::More (qw|no_plan|); my $test = "/Users/jimk/present"; my $hide = $test . '.hidden'; my $status; if (-f $test) { $status = 1; move($test, $hide); ok(! -f $test, "original file no longer present"); ok( -f $hide, "hidden file present"); } else { open FH, ">$test"; print FH "1\n"; close FH; ok(-f $test, "file created where it didn't previously exist"); ok(1, "test not relevant"); } my $trick = 0; if (! $trick) { croak "Trick is false"; } END { if ($status) { move($hide, $test); ok(! -f $hide, "hidden file no longer present"); ok( -f $test, "original file once again present"); } else { unlink $test; ok(! -f $test, "specially created file has been removed"); ok(1, "test not relevant"); } }

Update: While the solution above guarantees that the testing process is (more) non-destructive, it does have one drawback, AFACIT. When the croak occurs earlier in the file, (1) the total number of tests run is reduced by the quantity lying between the croak and the END block and (2) the numbers associated with the tests inside the END block are no longer at the end of the range predicted by the plan.

Revised example:

#!/usr/local/bin/perl use strict; use warnings; use File::Copy; use Carp; use Test::More tests => 7; # (qw|no_plan|); my $test = "/Users/jimk/present"; my $hide = $test . '.hidden'; my $status; my $trick = 0; if (-f $test) { $status = 1; move($test, $hide); ok(! -f $test, "original file no longer present"); ok( -f $hide, "hidden file present"); } else { open FH, ">$test"; print FH "1\n"; close FH; ok(-f $test, "file created where it didn't previously exist"); ok(1, "test not relevant"); } croak "Trick is false" unless $trick; ok('alpha', "alpha reached"); ok('beta', "beta reached"); ok('gamma', "gamma reached"); # croak "Trick is false" unless $trick; END { if ($status) { move($hide, $test); ok(! -f $hide, "hidden file no longer present"); ok( -f $test, "original file once again present"); } else { unlink $test; ok(! -f $test, "specially created file has been removed"); ok(1, "test not relevant"); } }

... produces (assuming that present was indeed present at the outset):

1..7 ok 1 - original file no longer present ok 2 - hidden file present Trick is false at end.pl line 28 ok 3 - hidden file no longer present ok 4 - original file once again present # Looks like you planned 7 tests but only ran 4. # Looks like your test died just after 4.
Tests 3 and 4 above are not the third and fourth as you would intuitively predict from eyeballing the file. You become more dependent on the croak message in locating the error within the file.

Thanks!

Jim Keenan


In reply to Re^2: BEGIN and END blocks, use strict and scoping by jkeenan1
in thread BEGIN and END blocks, use strict and scoping by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.