Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Remote Pipe

by Robert (Initiate)
on May 31, 2000 at 05:08 UTC ( #15547=perlquestion: print w/replies, xml ) Need Help??

Robert has asked for the wisdom of the Perl Monks concerning the following question:

Folks, I'm trying to execute this simple script to change a file in a remote server as part of a much bigger script:
$file = 'Win.ini';\n $tmp = "$file.tmp";\n chdir ("\\\\servername\\winnt") || die "$!\n";\n open (FL, "$file") || die "$!\n";\n open (TMP, ">$tmp") || die "$!\n";\n while (<FL>){\n $_ =~ s/als/ald/g;\n print TMP $_;\n }\n close FL || die "$!\n"; \n close TMP || die "$!\n";\n rename ($file,$file.old) || die "$!\n";\n rename ($file.tmp,$file) || die "$!\n";\n
I'm running this with admin. privileges on a few servers. Every time I run the script, it says it cannot create the TMP file handle.(File or directory does not exist). Could someone give me a hand? Thanks, Robert

Replies are listed 'Best First'.
Re: Remote Pipe
by plaid (Chaplain) on May 31, 2000 at 05:21 UTC
    Your problem seems to be with the renaming part. You are trying to rename $file.tmp to $file, but since there are no quotes around $file.tmp, the . is acting like the string concatenation operator, so it is looking for Win.initmp instead of Win.ini.tmp. Just put the $file.old and $file.tmp parts of the rename inside double quotes and it should work fine.
Re: Remote Pipe
by Robert (Initiate) on May 31, 2000 at 05:39 UTC
    Ok Folks, now it looks like real code...  $file = 'Win.ini';  $tmp = "$file.tmp";  chdir ("\\\\servername\\winnt") || die "$!\n";  open (FL, "$file") || die "$!\n";  open (TMP,">$tmp") || die "$!\n";  while (<FL>){     $_ =~ s/als/ald/g;     print TMP $_;  }  close FL || die "$!\n";  close TMP || die "$!\n";  rename ($file,"$file\.old") || die "$!\n";  rename ("$file\.tmp",$file) || die "$!\n"; I'm getting the error when I try to create the TMP filehandle.
      [ if you are using the latest version of Perl (5.6) ] try opening TMP like this: open(TMP, '>', $tmp) || die "Couldn't open $tmp: $!"; note, die adds a line break on its own so it is not necessary to put one in your message.

      p.s. a pox upon the houses of those who would unduly censure me with their '--' votes. was it because my sub-par code didn't work in your version of Perl? or because i do not fully grasp the intricacies of 'die'? die $haters if $haters->dont_know_jack();

        Umm ,actually not really. See, die is pretty funky and looks at the end of the message. If there is no newline, it adds the new line along with a whole bunch of other stuff ( like the line number, etc ).

        Sometimes, though, that information is not desired. Especially when it may confuse the poor end user. So, if you do add the newline, the only thing that shows up is the message you sent.

        Mik - probably going on far longer than anybody really needs

        Probably just a typo on your part, but I think this is what threw the OP when he tried your code. The second comma: you probably meant that to be a concatenation operator, yes?

        You want

        open TMP, ">" . $tmp or die "Couldn't open $tmp: $!";
        This is what I've got: Too many arguments for open at line 5, near "$tmp) " Execution of aborted due to compilation errors.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://15547]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-06-28 08:45 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (90 votes). Check out past polls.