Skip navigation.
Your source for Perl tips, howto's, faq and tutorials
( categories: )

I created a file testRead_line.txt that has only one character on line 1. My Perl script reads in the character. If the character is 1, the script stops and exit. otherwise, the script runs and at end, write the "0" to replace "1" in the TestRead_line.txt. I wrote some code like below. but it does not work. can someone help? thanks.

#!/usr//bin/perl -w
use Net::SFTP::Foreign;
open(F, 'testRead_line.txt');
while () {
print "line = $_ \n";
if ($_ =~ m/^\1/) {
print "The script has already been running. Exit. \n";
exit (0);
else {
print "start running the script. \n";

Use 'substr' instead of a

Use 'substr' instead of a regular expression to get the first character of a string:

open INPUT, "<testRead_line.txt";
$line = <INPUT>;

if ( substr($line, 0, 1) eq "1" )
print "The script has already been running\n";
print "Start running the script\n";