Manish Jain wrote:

Hi all,

Hello,

I want to do this :

open(hndr, "/home/manjain/.bash_profile");
open(hndw, ">", "/home/manjain/bashrc.copy");

You should *always* verify that the files opened correctly before trying to use the filehandles. Something like:

open hndr, '<', '/home/manjain/.bash_profile'
    or die "Cannot open /home/manjain/.bash_profile because: $!";
open hndw, '>', '/home/manjain/bashrc.copy'
    or die "Cannot open /home/manjain/bashrc.copy because: $!";


while ($nextline =<hndr>)

You should lessen the scope of $nextline by using a lexical variable:

while ( my $nextline = <hndr> )


{
     $nextline =~ s/manjain/\$USER/g;
     print(hndw, $nextline);                  #problem here
}

But perl refuses to take a comma between hndw and $nextline, and
consequently I have to rewrite it as : print hndw $nextline;

But this is much less intuitive to me as a C programmer. Perl's
syntactical rules in general leave a lot to be desired : the syntax
actually is overly flexible and can confound people familiar with
structured code. This particular case of print is an example of
something that should work by logic but does not.                               
        

First, the filehandle is optional, so if you put a comma at the beginning of the list with no filehandle would that make sense?

print , $nextline;

Second, the syntax is consistent with say, sort, map and grep, where the filehandle or subroutine name or code block is optional and doesn't require a comma before the list, which is why this works as well:

print { hndw } $nextline;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to