Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

My sub does not work twice

by fernandes (Monk)
on Aug 07, 2007 at 22:16 UTC ( [id://631170]=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

I have a sub that reads a part p1 of a file and produces the expected output o1. When I change one variable in the code, it reads the part p2 and produces the expected output o2. Everything happens as expected. However, when I am trying to produce o1 and o2, in the same execution, it does not work. This ad hoc quasi-code resumes:
#!/usr/bin/perl -w use strict; our $select_job; our $filehandle_in; our $filehandle_out; sub Job_selection{ $select_job = shift; open (our $filehandle_in, "<", "input.txt") || die "Can't read inp +ut.txt: $!"; open (our $filehandle_out, ">", "output_$select_job.txt") || die " +Can't write output_$select_job.txt: $!"; # #job is done here # close $filehandle_in; close $filehandle_out; } Job_selection("1"); Job_selection("2");
So, to be clear as fresh water: if I run this code changing for #Job_selection("1"); Xor #Job_selection("2"); it works; but it does not work when I call both
Job_selection("1"); Job_selection("2");
at the same execution.

Best regards!

Replies are listed 'Best First'.
Re: My sub does not work twice
by GrandFather (Saint) on Aug 07, 2007 at 22:34 UTC

    For what value of "doesn't work"?

    Note that you should not be using our to "declare" variables. Use my instead and make the declarations as local to the variables' use as possible.

    use strict; use warnings; open OUT, '>', 'input.txt'; print OUT <<OUT; contents of input.txt OUT close OUT; sub Job_selection{ my $select_job = shift; open (my $filehandle_in, "<", "input.txt") || die "Can't read inpu +t.txt: $!"; open (my $filehandle_out, ">", "output_$select_job.txt") || die "C +an't write output_$select_job.txt: $!"; print $filehandle_out (<$filehandle_in>); close $filehandle_in; close $filehandle_out; } Job_selection("1"); Job_selection("2");

    generates files called 'output_1.txt' and 'output_2.txt' containing the text 'contents of input.txt' as I would expect. What would you expect?


    DWIM is Perl's answer to Gödel
Re: My sub does not work twice
by wind (Priest) on Aug 07, 2007 at 22:25 UTC
    Try localizing your variables by using my instead of our.
    my $select_job = shift; my $infile = 'input.txt'; my $outfile = "output_$select_job.txt"; open(my $filehandle_in, "<", $infile) or die "Can't open $infile: +$!"; open(my $filehandle_out, ">", $outfile) or die "Can't open $outfil +e: $!";
    - Miller
Re: My sub does not work twice
by johnnywang (Priest) on Aug 07, 2007 at 22:31 UTC
    It looks like each of your run of job_selection() is overwriting the output file (">" in your open). Is that what you mean by "does not work"? also you are using "our" in your code, which does not seem necessary, "my" should be enough, and you don't need to declare them at the top.

    update ok, didn't see the output file name contains a variable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found