Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Conditional creation of files - global or scoped filehandle

by Anonymous Monk
on May 06, 2016 at 10:05 UTC ( [id://1162341]=perlquestion: print w/replies, xml ) Need Help??

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 (Bishop) on May 06, 2016 at 10:09 UTC
    Should the filename be declared outside of the loop first?

    Yes, that's the obvious approach IMO. (you did mean filehandle there of course)

Re: Conditional creation of files - global or scoped filehandle
by stevieb (Canon) on May 06, 2016 at 12:33 UTC

    Can you explain what you're trying to do? You want to open a handle in a condition, then use the handle globally elsewhere. What if you don't open the file? Do you have code like this?

    if (...){ open FH, '>', $file or die $!; } ... if (...){ print FH ...; } ... if (...){ print FH ...; }

    In other words, if the 'Example' if wasn't true, that file won't be open, and every call to the handle will need to be wrapped in a conditional as well.

    Understanding what you're trying to do overall will help us help you sort out your logic.

      Yes that is correct. I was mostly interested in the best way to tackle such an issue but as pointed out in the first reply - declaring the filehandles outside of the IF-statement is probably the easiest solution.
Re: Conditional creation of files - global or scoped filehandle
by Anonymous Monk on May 06, 2016 at 10:13 UTC

    sure, its called functions

    Main( @ARGV ); exit( 0 ); sub Main { my( $command, @args ) = @_; my %commands = ( example => \&Example, help => \&Help ); my $action = $commands{lc $command} || \&Help; $action->( @args ); } sub Example { my( $infile, $outfile ) = @_; use autodie; open my $out, '>', $outfile ; ... }
    As you can see everything the Example option would do is done in a sub/function called Example
Re: Conditional creation of files - global or scoped filehandle
by anonymized user 468275 (Curate) on May 06, 2016 at 18:59 UTC
    What I always do is roughly the following although you can switch this to global instead of object if you are physically compelled ;)
    my $obj = MyPackage::new(); # sets $obj->{FH}='' if ('some condition') { 'do something about that'; if ('another nested block') { open my $lh, 'etc.'; $obj->getset('FH', $lh); # or maybe $obj->{FH}=$lh; } }
    but if objects are illegal in your organisation: my $fh = '' at global scope level and update it with $lh where applicable.

    One world, one people

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1162341]
Approved by Discipulus
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-25 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found