in reply to Re: opening a file passed into sub as ref
in thread opening a file passed into sub as ref

the correct code is yes w/out $$name rather using $name i always use strict and warnings. its there in the code at the top.
sub _ce { print "IN CE SUB\n"; die "no parameter!\n" unless @_; my ($ce_ref) = @_; for my $name (@{ $ce_ref }) { @asof = +(split("/", $name, 0))[-3,-2,-4]; $lpname = +(split("/", $name, 0))[3]; print $csv "$lpname\n"; print $csv '*' x length($lpname),"\n"; print '*' x length($lpname),"\n"; print "$name\n"; open (my $i, "+<", $name) or warn $!; ### FAILS HERE ### while (<$i>) { chomp $i; next if $i =~ /$regexp_2_skip/; next if $i !~ /$regexp_2_get/; ($i) =~ s/:/,/g; ($i) =~ s/\s+//g; print $csv "$i,\n"; print "$i,\n"; } } print $csv "\n\nHost,Entitled CPU and RAM as of @asof,\n\n"; return; } __OUT__ SCALAR NPLPARS 30 SCALAR lparstat: 30 IN CE SUB ******** /opt/r2configs/edidev00/2018/03/13/lparstat-i.out No such file or directory at get_nonprod_AIX_resrcs_used.plx line 454, + <$lus> line 131. readline() on closed filehandle $i at get_nonprod_AIX_resrcs_used.plx +line 455. ********** /opt/r2configs/ediperqa00/2018/03/13/lparstat-i.out # ls -l /opt/r2configs/ediperqa00/2018/03/13/lparstat-i.out -rw-r--r-- 1 root system 2219 Mar 13 21:00 /opt/r2confi +gs/ediperqa00/2018/03/13/lparstat-i.out

Replies are listed 'Best First'.
Re^3: opening a file passed into sub as ref
by poj (Abbot) on Mar 15, 2018 at 19:02 UTC

    $i is not a good name for a filehandle, it's easy to get confused with the file contents.

    open (my $i, "+<", $name) or warn $!; ### FAILS HERE ### while (<$i>) { chomp $i; next if $i =~ /$regexp_2_skip/; next if $i !~ /$regexp_2_get/; ($i) =~ s/:/,/g; ($i) =~ s/\s+//g; print $csv "$i,\n"; print "$i,\n"; }

    as Toolic said, you probably have line endings on $name from find. Try

    sub _ce { print "IN CE SUB\n"; die "no parameter!\n" unless @_; my ($ce_ref) = @_; for my $name (@{ $ce_ref }) { chomp $name; @asof = +(split("/", $name, 0))[-3,-2,-4]; $lpname = +(split("/", $name, 0))[3]; print $csv "$lpname\n"; print $csv '*' x length($lpname),"\n"; print '*' x length($lpname),"\n"; print "$name\n"; open (my $fh, "+<", $name) or die "Could not open '$name' $!"; + ### FAILS HERE ### while (my $line = <$fh>) { chomp $line; next if $line =~ /$regexp_2_skip/; next if $line !~ /$regexp_2_get/; $line =~ s/:/,/g; $line =~ s/\s+//g; print $csv "$line,\n"; print "$line,\n"; } } print $csv "\n\nHost,Entitled CPU and RAM as of @asof,\n\n"; return; }
    poj