Solved

Move a Line to end of file

Posted on 2014-04-08
4
311 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now