in reply to file not found with OPEN

Some tips, use Path::Tiny so you don't have to readdir among other things, like this
use Path::Tiny qw/ path /; if( not path( $direct, $yr )->exists ) { path( $direct, $yr )->make_path; } @Old = path( $direct, $yr )->children;

Path::Tiny dies when it can't make_path or children or openread

my $INPUT = path( $inddirect, "company$qtr$yr.idx" )->openr; my $OUTPUT; if( $yr == $startyear && $qtr == $startqtr ) { $OUTPUT = path( $outfil )->openw; ## clobber } else { $OUTPUT = path( $outfil )->opena; ## append }

Also, no need for that giant if/else block, use an array

my @FormGetRes = ( '(10-K )', '(10-K405 )', '(10KSB )', '(10-KSB )', '(10KSB40 )', '(10-KT )', '(10KT405 )', '(10-Q )', '(10QSB )', '(10-QSB )', '(10-QT )', ); @FormGetRes = map { qr{^$_(?!/)}s } @FormGetRes; RELOOP: for my $re ( @FormGetRes ) { if( $form_type =~ $re ) { print OUTPUT "$fullfilename\n"; $count++; last RELOOP; } }

Replies are listed 'Best First'.
Re^2: file not found with OPEN (scope)
by Anonymous Monk on Jan 06, 2015 at 20:59 UTC
    Another tip, this one about subroutines, they should return values not work on global variables, meaning
    my $ftp = ftpsignin();; sub ftpsignin { use Net::FTP; my $ftp = Net::FTP->new( "ftp.sec.gov", Debug => 0, Passive => 1 ) or die "Cannot connect to some.host.name: $@"; $ftp->login( "anonymous", '-anonymous@' ) or next; #die "Cannot login ", $ftp->message; $ftp->binary(); # set binary mode return $ftp; ### RETURN VALUE } ## end sub ftpsignin
    Why? coping with scoping explains
      Thank you for the help!!! I am grateful. On another note, I thought I had thanked everyone for their help with this issue. However, I somehow missed thanking RLAURENT, and think I ruffled his feathers. But I wanna make it right. How do I reply to his last message?
        Don't worry about that, it only appears that you most probably simply missed the post where I told you to to add the "C:" volume information to your data path (and, yes, I was the first one to do so). It was here: Re^5: file not found with OPEN. I was just a bit saddened by the fact that you did not see it, nothing more, and perhaps overreacted to the fact that you thanked others and not me. But I have absolutely no problem with you, I think you simply overlooked the post, perhaps it was only too deeply nested for your PM settings, and I am absolutely ready to help you another time if needed and if I can. ;-)
Re^2: file not found with OPEN (names subs)
by Anonymous Monk on Jan 06, 2015 at 21:24 UTC

    Another tip, make more subs, give them good names, give variables good names, ... maybe BeeWork( $indexStore, $filingsStore, $filingsFile, $startyear, $endyear, $startqtr, $endqtr  ); ??? :D

    If you make subs your loops can shrink

    ## yearloop { ## quarterloop { ... BeeIO( $infile, $outfile , \@FormGetRes ); #~ my $downloads = path( $filingsStore, $yr ); my $dlyear = path( $direct, $yr); my @aonly = BeeAonly( $dlyear, $outfile ); BeeFtpGet( \@aonly , $dlyear ); } }

    Why subs? If there is a problem in BeeAOnly, you only have to debug BeeAOnly, you don't have to debug all of BeeWork

Re^2: file not found with OPEN (reafactor)
by Anonymous Monk on Jan 06, 2015 at 21:31 UTC

    Another tip is format/annotate your code with perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while for " -otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*"

    it produces output like

    #end of qtr loop } ## end for( $qtr = $startqtr ;...) #end of year loop } ## end for( $yr = $startyear ;...) } ## end sub ftpsignin

    While these comments can help, if you keep your loops/subs relatively small, you won't rely on them as much :)

    I commonly use

    ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while for " +-otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*"