Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Subroutine to delete one file and copy one file to another

by pme (Prior)
on Jul 20, 2015 at 21:12 UTC ( #1135494=note: print w/replies, xml ) Need Help??


in reply to Re^2: Subroutine to delete one file and copy one file to another
in thread Subroutine to delete one file and copy one file to another

Hi shroh,

You can add 'my' to the variable declarations wherever the error messages indicates.

Then you will get an appropriate error message about undefined subroutine because 'e' is missing in your sub name.

... sub deletTemp { ...

Replies are listed 'Best First'.
Re^4: Subroutine to delete one file and copy one file to another
by ww (Archbishop) on Jul 21, 2015 at 02:38 UTC

    Right on (in para 2 of pme's Re^3: Subroutine to delete one file and copy one file to another) re the sub,
      ...but uhmm, not so much re:
            "... add 'my' to the variable declarations wherever (emphasis supplied) the error messages indicates."

    Not a good idea: $YEAR gets an error message at lines 20 & 22 (and possibly some others; TL, DR) while $year and several others also have multiple instances.

    In this case, the $YEAR in Ln 22 is supposed to contain or represent the same value as that in Ln 20, so the one at 20 (in fact the whole list of vars there) should get a my.

    But adding a my to the $YEAR in Ln 22 would break the process.

    It may be that some of the vars in OP's code are instances (bad practice) of using the same name in different scopes. Simply iterating thru an edit/test cycle will probably not cure any problem of that character, but it's probable, IMHO, that some variables identified MORE THAN ONCE in the error messages are intended to be global.

    In that case, adding a my before the first open parenthesis on Ln 20 will make many of Perl's help messages (because that's what error messages and warnings are)"go away". Adding a my before the first instance of each var cited in an error message may help OP to find most of the problems in the code shown. But marking all of them with mys will probably break the code and confuse the Seeker even further.


    Precise language is the GodFather of precise code!

Re^4: Subroutine to delete one file and copy one file to another
by shroh (Acolyte) on Jul 20, 2015 at 21:46 UTC
    Most of the errors are now resolved after putting the "my" before each variable.
    i have few concerns: 1) If the temp file doesnt exist , i need to create one and copy the c +ontents of serverlist.txt in the newly created temp file. 2) If it already exists, i need to delete the contents of that temp fi +le and copy the contents of server.txt into the existing temp file. How this is possible? Any help is highly appreciated. Thanks
      If I understand your problem correctly, then there's no need for the "delete and copy" dance.
      Simply open your tempfile for output and it will automatically get created or truncated.
      You already populated your @outagenodes from the data read from your serverlist. So why don't you simply:
      open(my $tempfh, ">", $tempfile) or die "couldn't open $tempfile: $!"; print$tempfh @outagenodes or die "error when writing: $!"; close $tempfh or die "huh?? - $!";
      BTW I deliberately leave off the space in print$filehandle to remind myself that there's no comma between the file handle and the list...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1135494]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-01-21 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?