in reply to Can't write to the 2nd created excel workbook upon changing directory (Excel::Writer::XLSX)

Is there any reason why I'm not getting a good output the 2nd time I run the script as part of the while loop?

Hi,

Its because you're not Coping with Scoping and your subroutines do not accept arguments and have unclear purpose

using empty prototype

sub UE_Log is declared inside a while loop

sub Beam_Hystersis is declared inside while loop

Try to pass arguments while disabling prototype checking  &Beam_Hystersis($filename); but sub Beam_Hystersis accepts no arguments (also has no prototype, but most subs should have no prototype)

Download Modern Perl , it explains the same stuff as Coping with Scoping and perlintro, and try writing like this

Each time you add a new subroutine create a new file moresubs1184355-001.pl

#!/usr/bin/perl -- use strict; use warnings; use Getopt::Long qw/ GetOptions /; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { my %opts; GetOptions( \%opts, 'carriers|c|C=s', 'num_tp|t|T|TP=i', 'Help|h|H', 'bs|b|B|BS|bs', 'version|v|V', 'legend|l|L=s', 'altitude|a|A=i', 'actdir|d|D=s', ); dd( \%opts ); } __END__ $ perl moresubs1184355-001.pl -c=1 -t=2 -v=3 -h=4 -l=5 -a=6 -d=7 Option v does not take an argument Option h does not take an argument { actdir => 7, altitude => 6, carriers => 1, legend => 5, num_tp => 2 +}

Next is moresubs1184355-002.pl

#!/usr/bin/perl -- use strict; use warnings; use Getopt::Long qw/ GetOptions /; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { my %opts; GetOptions( \%opts, 'carriers|c|C=s', 'num_tp|t|T|TP=i', 'Help|h|H', 'bs|b|B|BS|bs', 'version|v|V', 'legend|l|L=s', 'altitude|a|A=i', 'actdir|d|D=s', ); dd( \%opts ); WorkTheWorkbook( \%opts ); } sub WorkTheWorkbook { my( $optsRef ) = @_; dd( $optsRef ); print "altitude=$optsRef->{altitude}\n"; use autodie qw/ open close /; open my($fh), '<', '$optsRef->{altitude}'; # fake return; } __END__ $ perl moresubs1184355-002.pl -c=1 -t=2 -v=3 -h=4 -l=5 -a=6 -d=7 Option v does not take an argument Option h does not take an argument { actdir => 7, altitude => 6, carriers => 1, legend => 5, num_tp => 2 +} { actdir => 7, altitude => 6, carriers => 1, legend => 5, num_tp => 2 +} altitude=6 Can't open '$optsRef->{altitude}' for reading: 'Invalid argument' at m +oresubs1184355-002.pl line 29

See how it runs? See how one you pass a variable between subroutines? How autodie dies for you when open cant open a file?

I can't help you with the next part, as the purpose of UE_Log and Beam_Hystersis is a mystery to me

But you would probably write

Beam_Hystersis( $workbook, $filename, $optsRef ); ... sub Beam_Hystersis { my( $workbook, $filename , $optsRef ) = @_; use autodie; ## stick this next to use strict so you dont have to +keep repeating open my($fh), '<', $filename; ...; $worksheet->write( ... ); ...; }

See how you pass args between subs?

  • Comment on Re: Can't write to the 2nd created excel workbook upon changing directory (Excel::Writer::XLSX)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Can't write to the 2nd created excel workbook upon changing directory (Excel::Writer::XLSX)
by m_jaser (Novice) on Mar 14, 2017 at 03:25 UTC

    Thanks a lot for the recommendations...! I'll restructure the code to comply to better comply with Coping & Scoping.

    Best Regards