Solved

Move a Line to end of file

Posted on 2014-04-08
4
318 Views
Last Modified: 2014-04-08
I am very new to Perl.  Here is what I would like to do:

Read a file in.
If File is empty, simply end (this part works)
If file has entries, move the first line to the last line. (this part isn't working).

Error: Global symbol "$first_line" requires explicit package name at ./test_move.pl line 27.

Here is my code. Any assistance is greatly appreciated!!

#!/usr/bin/perl
############################################################################################################
# Test Reading File
############################################################################################################

############################################################################################################
# Variables Section
############################################################################################################
use strict;
use warnings;
my $filename = 'filein.txt';

if ( -z "$filename" ) {
   print "File has zero size\n";

} else {

open(FH, '<', $filename)  or  die "Failed to read file $filename : $! \n";
my $firstline = <FN>;
my @remaining_lines = <FN>;
chomp($firstline);
print "The first line is $firstline\n";
close(FN);

open(FH, '>', $filename)  or  die "Failed to read file $filename : $! \n";
print FN @remaining_lines;
print FN $first_line;
close(FN);
}


Contents of Filein.txt:
Orange
Blue
Green

Would like it to end up:
Blue
Green
Orange
0
Comment
Question by:PAH_in_MI
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39985933
perl -Mdiagnostics -w test_move.pl
Global symbol "$first_line" requires explicit package name at test_move.pl line 27.
Execution of test_move.pl aborted due to compilation errors (#1)
    (F) You've said "use strict" or "use strict vars", which indicates
    that all variables must either be lexically scoped (using "my" or "state"),
    declared beforehand using "our", or explicitly qualified to say
    which package the global variable is in (using "::").
   
Uncaught exception from user code:
      Global symbol "$first_line" requires explicit package name at test_move.pl line 27.
      Execution of test_move.pl aborted due to compilation errors.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39985936
Did you mean $firstline?

And did you mean FH when you said FN?
0
 

Author Comment

by:PAH_in_MI
ID: 39985977
Good gosh.......a dang typo.  :(

Thanks for catching that.......I must need new glasses!
0
 

Author Closing Comment

by:PAH_in_MI
ID: 39985982
Save me hours of frustration....all because I can't tell the different between an H and an N.

Thanks for catching it!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question