in reply to Re: split one file into many on finding a new line
in thread split one file into many on finding a new line

Hi Thanks for replying...I wrote the following script,
#!/sw/perl5/1.1/bin/perl use lib ("/fbcapps/buc/perl_lib"); use strict; my $filename = "/home/smarter/alltables"; local $/ = ""; while (<DATA>) { chomp; ++$filename; print "**** Open file $filename here\n"; print "$_\n"; print "**** Close file $filename here\n"; }
It doesn't work however.It just exits.Please help.

Replies are listed 'Best First'.
Re^3: split one file into many on finding a new line
by kyle (Abbot) on Sep 12, 2008 at 15:20 UTC

    The DATA filehandle is special; it reads from whatever comes after a __DATA__ line in your program. If you want to read from the file specified by $filename instead, you have to open it specifically.

    open my $filehandle, '<', $filename or die "Can't read '$filename': $!"; while ( <$filehandle> ) { # etc. } close $filehandle or die "Fail on close '$filename': $!";
Re^3: split one file into many on finding a new line
by GrandFather (Saint) on Sep 12, 2008 at 21:33 UTC

    In addition to kyle's comment, you should note that ++$filename uses Perl magic to generate a sequence of file names. Unless you add a few digits to the end of the 'seed' file name the result may be other than you expect. Try running the following:

    my $fn = 'xxx0'; print ++$fn, ' ' for 1 .. 11; print "\n"; $fn = 'xx00'; print ++$fn, ' ' for 1 .. 11;

    Perl reduces RSI - it saves typing