On 29/11/2010 12:33, Manish Jain wrote:

I want to do this :

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

while ($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.

Hello Manish

The form

  print FILEHANDLE LIST

is actually 'indirect object' syntax for
calling the print method of the IO::Handle object FILEHANDLE. This is from 'perldoc perlobj':

  The other way to invoke a method is by using the so-called "indirect
  object" notation. This syntax was available in Perl 4 long before
  objects were introduced, and is still used with filehandles like
  this:

       print STDERR "help!!!\n";

So you can, if you like, use the more usual arrow syntax for calling the method. Your program would look like this:

  use strict;
  use warnings;

  use IO::Handle;

  open my $hndr, '<', '/home/manjain/.bash_profile' or die $!;
  open my $hndw, '>', '/home/manjain/bashrc.copy' or die $!;

  while (my $nextline = <$hndr>) {
    $nextline =~ s/manjain/\$USER/g;
    $hndw->print($nextline);
  }

HTH,

- Rob

--
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