Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Wherever possible I try to utilise a three-argument open and avoid bareword filehandles, such as:
open my $out, '>', "$outfile" or die "$!"However there are cases when the creation of a subset of files in my script is dependent upon arguments provided by the user:
if ($ARGV[0] eq "Example") { open my $out, '>', "$outfile" or die "$!" }
As a side note: i'm not sure that this is an appropriate means of conditionally opening files.
This presents the problem that the filehandle is no longer global and thus inaccessible later on in the script. Is there a way around this or is this a situation where using a bareword filename is acceptable? Should the filename be declared outside of the loop first?
Data being printed to $out is being created within a loop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Conditional creation of files - global or scoped filehandle
by hippo (Archbishop) on May 06, 2016 at 10:09 UTC | |
|
Re: Conditional creation of files - global or scoped filehandle
by stevieb (Canon) on May 06, 2016 at 12:33 UTC | |
by Anonymous Monk on May 06, 2016 at 13:47 UTC | |
|
Re: Conditional creation of files - global or scoped filehandle
by Anonymous Monk on May 06, 2016 at 10:13 UTC | |
|
Re: Conditional creation of files - global or scoped filehandle
by anonymized user 468275 (Curate) on May 06, 2016 at 18:59 UTC |